Powershell to Check File *Not* Modified - windows

So I'm a Linux guy, been doing this for a while, and now the boss has asked me to look at something on a Windows 2008 server. Basically, there's a Java app that does something, and occasionally, it hangs. The thing is that unless someone is actually watching it, there is no hint that it has hung.
The application writes to a logfile on the 2008 server, updating it every five minutes. If the application hangs, it (obviously) stops writing to the logfile.
I would like a way to check that file every say, fifteen minutes and if the file has not been modified in the past fifteen minutes, send an alert.
Can this be done? Is it horrendously complicated? Would it be easier to write it in bash/cygwin?
Many thanks.

Create a scheduled task that runs a PowerShell script containing something like this every 5 minutes or so:
$logfile = 'C:\path\to\your.log'
$threshold = (Get-Date).AddMinutes(-15)
if ((Get-Item $logfile).LastWriteTime -lt $threshold) {
# send alert
}

Related

restart program upon file not being modified

I cant find a bat file which will restart a program when a telegram file is not modified for lets say 2 minutes or so? telegram.session. a trade copier program. Like if telegram.session is not modified for 2 minutes restart tradecopier.exe. the tradecopier.exe will be running during this as well as telegram

Schedule script on windows so that it should always be running in background when someone logins

I am a beginner at developing Windows apps. I have created a VB script that executes the EXE file in background. I have referred some articles online and scheduled the script with windows scheduler. Now Every time I log in to the computer, the script starts running in the background normally as expected. The problem occurs when I lock the computer for a long period of time, somehow script gets killed by Windows. If I try to log in again, script(which got killed) doesn't start once again.
I want the script to run every time the computer gets unlocked no matter whether the computer was restarted/power-on/woke up from sleep and it should start running the script. But I am unable to achieve this.
This is what my current trigger looks like . Please tell me if I am doing something wrong

How can I find out, from within a .bat- (or .ps1-) script, if it was started by the task scheduler?

I'm actually interested as the question is stated, but what I'm after is a mechanism for temporarily disable the scheduled run (on multiple servers, whith shared disk), while still being able to run it interactively (if the script can be made to tell if it's started from task-scheduler or not). An external flag (the existence of a file) seemed like a simple approach to do that.
First option was using "%SESSIONNAME%" which seems to be empty when started from the scheduler, although that's also the case when started from powershell (with Start-Process, which might be an option for allowed usage while disabled).
EDIT: A second option, using the whoami cmd-command seems more promising, it reports the user as belonging to the group NT AUTHORITY\BATCH when run from task scheduler (but not when run interactively). Could I shoot myself in the foot using this - can a user be added to that group "permanently" behind my back?
We're on Windows Server 2012 R2. PowerShell solution would be interesting, as a wrapper could probably be used (or even modification/rewrite from .bat to .ps1).
Per #Alex K.: passing a separate argument to signal that we're running in a scheduled context works fine. Simple, reliable techniques that don't require you to know anything about third-party components -- in my neck of the woods we call those "wins" rather than "cheats".
If you really want, you can check for what you're literally asking -- if we're running from Task Scheduler -- by checking the properties of our parent process. From PowerShell this is quite doable, don't know about plain old batch files:
$parentpid = (gwmi "win32_process where (ProcessId = $pid)").parentprocessid
$parentname = (gwmi "win32_process where (ProcessId = $parentpid)").name
if ($parentname -eq 'taskeng.exe') {
"I think this is Task Scheduler, or maybe something else a hacker named taskeng.exe, who knows?"
} else {
"This is not Task Scheduler. Probably."
}
Of course, this might break if one day MS renames the process, or indeed if you want to use something else than TS to run the script automatically, or even if the script invokes another process which in turn invokes a script.
Personally, I would definitely go with the explicit flag. If you forget to apply it, it will be obvious, it can't suddenly break one day, and best of all, you can test if your script works correctly without actually having to schedule it first.

SCCM 2007 Advertisment Run Notification Timer

where (in the WMI?) is the timer located if the SCCM Client Agent 2007 is about to run a specific mandatory Advertisment?
I would like to shorten the 10 Minutes delay to some seconds with a Script or via a C# command. But not generally. Only if it has to be done urgent.
The message says "This program will begin running in xx minutes xx seconds"
I know that there exist a "ADV_RunNotificationCountdownDuration" in the CCM_SoftwareDistribution under the root\CCM\policy\machine\actualConfig, but this field is empty. And i dont know where the Actual Minutes left are stored.
Have you looked in the
System Center Configuration Manager 2007 Software Development Kit ?
You might want to look at IUIResourceMgr::ExecuteProgram Method
It should force the client to run the program immediately.
If you want to reduce the duration of the countdown, you can change the ADV_RunNotificationCountdownDuration value. Unfortunately it only takes effect after re-running the advertisments. Do take note, the value would revert back to your site settings once a policy refresh occurs.

Windows Task Scheduler will run app only once

So my situation is that I am running an app on the Windows Task Scheduler. This app is run once a day at 1pm. the app does some queries and transfers data to an FTP site. All that is working great except on the weekends when i am not here the app is run and the GUI is still displayed for me to review. This seems to make it stop running on the scheduler until I shut down the app. So on Saturday it will run and the app will remain displayed for me to review when I get back on Monday. but on Sunday when the scheduler attempts to run it again it will fail because the app has not been closed down.
First let me confirm that this is how the Task Scheduler is supposed to work. Second, what are my alternatives for scheduling to run every day and keep the GUI displayed so that I can review. The app can run multiple times as each session does not interfere with the other sessions. So if I'm gone for a week on vacation I would expect that when i get back that 7 instances of the app have been run and are waiting for my review.
Thanks
AGP
Your best bet is to eliminate the UI and log messages to the Event Log or a log file. The UI could be spawned from the CLI as a separate process if you prefer, but it should be done so in as its own non-child process.
Alternatively, you could run a batch file instead of the process directly. In the batch file, invoke "START path_to_exe" instead of the EXE. That will cause the batch file to "finish" instantly, and the exe to be run in its own process. This is not a good long term solution, but will give you a temporary solution to your immediate problem.
This is the default behavior of the Scheduled Task system, as it doesn't know that the job is complete until the application actually exits. Therefore, if your application is still open after 24 hours, the next run will simply be skipped because the current run is "still going" as far as the scheduler is concerned.
Personally I would re-visit the way that you handle your job process, as your are setting up a scenario that will be hard to manage long term.
I recommend writing to a log file instead of displaying a UI for any output and/or errors. This way, the application can write, then exit, and you can review the log at your convenience. This is a very common solution for automated processes.

Resources