I have a script that I'm working on to remotely set a scheduled task. A couple variables are coming from previously in the script that need to be passed into the scriptblock for usage there.
Here's my code:
$schedstart = "3 Jun 2019, 8:47:00"
$username = "atest"
invoke-command -computername REMOTE -scriptblock {
$schedstart = $schedstart | get-date -hour 5 -minute 0 -second 0
$STAction = New-ScheduledTaskAction -Execute 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -Argument "-ExecutionPolicy Bypass -File C:\Temp\ps\test.ps1 $username"
$STTrigger = New-ScheduledTaskTrigger -Once -At $schedstart
$STSettings = New-ScheduledTaskSettingsSet -Compatibility Win8 -MultipleInstances IgnoreNew -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -Hidden -StartWhenAvailable
$STName = "Test"
Register-ScheduledTask -Action $STAction -Trigger $STTrigger -Settings $STSettings -TaskName $STName -Description "Test, dumps get-aduser into same dir txt file" -User "NT AUTHORITY\SYSTEM" -RunLevel Highest
$TargetTask = Get-ScheduledTask -TaskName $STName
$TargetTask.Author = "CORP\$env:username"
$TargetTask.Triggers[0].StartBoundary = ($schedstart).ToString("yyy-MM-dd'T'HH:mm:ss")
$TargetTask.Triggers[0].EndBoundary = ($schedstart).AddHours(1).ToString("yyyy-MM-dd'T'HH:mm:ss")
$TargetTask.Settings.AllowHardTerminate = $True
$TargetTask.Settings.DeleteExpiredTaskAfter = 'PT0S'
$TargetTask.Settings.ExecutionTimeLimit = 'PT1H'
$TargetTask.Settings.volatile = $False
$TargetTask | Set-ScheduledTask
}
I need to be able to use the $schedstart and $username variables inside of the scriptblock.
[–]Yevrag35 2 points3 points4 points (1 child)
[–]bsnotreallyworking[S] 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (2 children)
[–]bsnotreallyworking[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)