I have a PowerShell script StartWatching.ps1 which monitors a folder using FileSystemWatcher:
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\ScheduledTasks\PlanUpdates"
$watcher.Filter = "*.sql"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER A EVENT IS DETECTED
$action = {
$path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), Processed, $path"
Add-content "C:\ScheduledTasks\PlanUpdates\log.txt" -value $logline
cmd.exe /c 'C:\ScheduledTasks\PlanUpdates\planUpdate.bat'
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY
$created = Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 1}
The script launches a .bat-file planUpdate.bat whenever an .sql-file is created in the folder. The .bat-file executes the contents of the .sql-file on a specific SQL server (database is stated in .sql-file), and then moves the file to sub-folder "Old":
for %%f in (.\*.sql) do sqlcmd -S <server> -i %%f & move "%%f" .\Old
The PowerShell script works flawlessly when I launch it by right-clicking and selecting "Run with PowerShell".
I need to make the script work when executed from cmd before it can work as a Scheduled Task. But it doesn't work when I launch it from cmd with:
powershell -noexit -file C:\ScheduledTasks\PlanUpdates\StartWatching.ps1
It opens a command-window as if the script is running, but FileSystemWatcher doesn't register any events
I hope you can help as I have been trying to solve this all day.
Here's the task xml:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2016-04-25T12:57:08.5588603</Date>
<Author>CRB\IUSR</Author>
</RegistrationInfo>
<Triggers>
<BootTrigger>
<Enabled>true</Enabled>
</BootTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>CBB\IUSR</UserId>
<LogonType>Password</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>Queue</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command>
<Arguments>-Noexit -File C:\ScheduledTasks\PlanUpdates\StartWatching.ps1</Arguments>
</Exec>
</Actions>
</Task>
schtasks.exe
command. – majkinetor