0

So I´ve got a powershell script that checks if a folder was created in a specific directory and then does stuff with that folder

Code:

echo "$(Get-Date -Format g) - Script started" >> C:\bitlocker.log

$folder = 'C:\Path\To\Check'

$filter = '*.*'           

$fsw = New-Object IO.FileSystemWatcher $folder, $filter 
$fsw.IncludeSubdirectories = $false              
$fsw.NotifyFilter = [IO.NotifyFilters]'DirectoryName' 

$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 

   $folderNow = $($EventArgs.FullPath)
   <do stuff now>

}

Starting this script manually works perfect but running it with the Task Scheduler is not working.

First I tried starting the script directly which shows that the script was successfully started but nothing that happens -Action { ... } gets executed.

I then tried to make a batch that starts the powershell script which gets started by the Task (as suggested here How to run a PowerShell script from a batch file)

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\PathtoScript\Script.ps1""' -Verb RunAs}"

but the problem still is the same.

The script starts (Wrote some Output to a file to check that) but nothing inside -Action { ... } gets executed.

Edit: Task Schedular is set to run the script as a user with admin privileges as the actions that do not work need those, writing to C:\ works perfectly fine

Community
  • 1
  • 1
architekt
  • 147
  • 11
  • Writing to the root of the C: drive requires elevated privileges. Is the task set to run only when the user is logged on? If not, then you need to set credentials within the task and if the folder you are checking is a network drive, you need to use the UNC path. – Squashman Nov 15 '16 at 14:18
  • Forgot to add this: Task Schedular is set to run the script as a User with admin privileges as the Actions that do not work need those, writing to C:\ works perfectly fine. – architekt Nov 15 '16 at 14:23
  • 1
    Do you aware that `FileSystemWatcher` dies with the process it was created in? – user4003407 Nov 15 '16 at 14:29
  • @PetSerAl - When executed manually the script starts a Background Job "FileCreated" that handles the rest. The script itself stops after starting the Job but the Job runs endlessly (or until manually stopped) and does exactly what I want. Entering Get-Job Shows a not running Job directly after running the script. when a dricetory is written now, the Job changes its state to running and successfully handles the set actions – architekt Nov 15 '16 at 14:31
  • 1
    @MartinFischer What do you mean by *executed manually*? Is it: you start interactive PowerShell **process**, execute script in that interactive **process** and than wait while interactive PowerShell **process** still running? If yes, then what happens, when you close this interactive process? Do you continue to receive events? – user4003407 Nov 15 '16 at 14:45
  • @PetSerAl Thank you very much, I forgot something simple as that. – architekt Nov 16 '16 at 08:04

2 Answers2

0

Scheduled task action:

Program Script (Use the full path):

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments ([NOT]optional):

-NoProfile -NonInteractive -ExecutionPolicy Bypass -File "C:\PathtoScript\Script.ps1" -Any 'other' -arguments 'you need' -should follow
Shawn Esterman
  • 2,292
  • 1
  • 10
  • 15
0

Ok I found my mistake

As @PetSerAl suggested I forgot that the FileSystemWatcher Acts only in scope of that one specific process of the script. The Script has started the Job successfully but after closing the process of the script (aka. the window I started the script in) the Job broke down because it could not refer to FileSystemWatcher anymore.

Adding something simple like:

Do{
    $JobExist = Get-Job 
} while($JobExist -ne '')

worked perfectly fine

architekt
  • 147
  • 11