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