Skip to main content

PowerShell Within A Scheduled Task

Summary

This agent procedure is used to create a scheduled task at logon and have it run PowerShell parameters as defined with the variables.

Example Agent Procedure Log

TimeProcedureDescriptionAdmin
9:56:10 am 27-Jan-23Download Folder EmptySuccess THENprovaltech.com/derek.joniak
9:56:10 am 27-Jan-23Download Folder EmptyComment at THEN step 4provaltech.com/derek.joniak
9:56:10 am 27-Jan-23Execute PowerShell CommandSuccess THENprovaltech.com/derek.joniak
9:56:08 am 27-Jan-23Execute PowerShell Command-0011PowerShell command completed!provaltech.com/derek.joniak
9:55:49 am 27-Jan-23Execute PowerShell Command-0011Executing PowerShell `"" -Command ""$taskName = 'EmptyDownloadFolder';if((Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue) -eq $null)\{$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-Command "Get-ChildItem $env:USERPROFILE/DownloadsRemove-Item -Recurse"';$trigger = New-ScheduledTaskTrigger -AtLogon;$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -Hidden -RestartInterval (New-TimeSpan -Minutes 1) -RestartCount 3 -ExecutionTimeLimit (New-TimeSpan -Hours 1);Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Settings $settings;Write-Output "Scheduled task '$taskName' created successfully"}else\{Write-Output "Task Already Exists or Failed To Create Task"}""`
9:55:49 am 27-Jan-23Execute PowerShell Command-0010Not sending output to variable.provaltech.com/derek.joniak
9:55:49 am 27-Jan-23Execute PowerShell Command-0008New command variable is: `-Command ""$taskName = 'EmptyDownloadFolder';if((Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue) -eq $null)\{$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-Command "Get-ChildItem $env:USERPROFILE/DownloadsRemove-Item -Recurse"';$trigger = New-ScheduledTaskTrigger -AtLogon;$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -Hidden -RestartInterval (New-TimeSpan -Minutes 1) -RestartCount 3 -ExecutionTimeLimit (New-TimeSpan -Hours 1);Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Settings $settings;Write-Output "Scheduled task '$taskName' created successfully"}else\{Write-Output "Task Already Exists or Failed To Create Task"}""`
9:55:49 am 27-Jan-23Execute PowerShell Command-0008Custom commands detected as `"$taskName = 'EmptyDownloadFolder';if((Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue) -eq $null)\{$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-Command "Get-ChildItem $env:USERPROFILE/DownloadsRemove-Item -Recurse"';$trigger = New-ScheduledTaskTrigger -AtLogon;$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -Hidden -RestartInterval (New-TimeSpan -Minutes 1) -RestartCount 3 -ExecutionTimeLimit (New-TimeSpan -Hours 1);Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Settings $settings;Write-Output "Scheduled task '$taskName' created successfully"}else\{Write-Output "Task Already Exists or Failed To Create Task"}"`
9:55:40 am 27-Jan-23Execute PowerShell Command-0002PowerShell is present.provaltech.com/derek.joniak
9:55:25 am 27-Jan-23Run Now - Download Folder EmptyAdmin provaltech.com/derek.joniak scheduled procedure Run Now - Download Folder Empty to run at Jan 27 2023 10:55 AMprovaltech.com/derek.joniak

Dependencies

Does this procedure depend on other VSA content? Link the ITGlue article for the content documentation.

Process

Describe how the agent procedure completes its objective.

Output

Procedure log files and other outputs.