I'm working on a test script to get the idea of working with the Task Scheduler in PowerShell.
I don't want to have any credentials within the script for security matters.
My idea was to create a task in PowerShell which runs a script but for some reason it won't execute properly and I don't get why.
My task is created as following:
$taskName = "WeeklyMaintance"
$User = "NT AUTHORITY\SYSTEM"
$Trigger = New-ScheduledTaskTrigger –Daily -At "08:14"
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "D:\Some SW\_Scripts\testing.ps1"
Register-ScheduledTask -TaskName $taskName -Trigger $Trigger -User $User -Action $Action -RunLevel Highest
and the script looks like this
$Logpath = "D:\Some SW\_Scripts"
$logname = "Log.txt"
function Write-Log {
Param ([string]$logstring)
$timestamp = Get-Date -Format "dd.MM.yyyy HH:mm:ss"
$log = "[$timestamp]: $logstring"
Add-Content -Value $log -Path "$Logpath\$logname"
}
Write-Log "My Test for research worked"
The Logfile is already created and this works perfectly fine when executed normaly but for some reasen as I create the task and run it with the task it won't start.
Did I something wrong with the creation of the task or using the system here?
Edit:
I found out it was only the path which must have been the "bad guy".
As I changed the path from "D:\Some SW_Scripts" to "D:\Test"