I need a Powershell script, that deactivates a bunch of running/ready Tasks on multiple machines, so that we can update them. After we updated the machines i should be able to trigger the next part of the script, that activates specifically the tasks that the first part of the script deactivated.
Automatation and the Update part should not be part of the script, no timed actions needed or anything fancy.
- I trigger Part 1 -> Script deactivates Tasks
- I trigger Part 2 -> Script activates Tasks the Part 1 deactivated
For Part 1 i got this so far:
Invoke-Command -ComputerName xxx-11, xxx-12, xxx-14 -ScriptBlock {Get-ScheduledTask -TaskPath \yyy\ | Where {($_.State -like "running") -or ($_.State -like "ready")} | Disable-ScheduledTask }
I am unable to create a logic, that remembers what tasks were deactivated, so that the Part 2 can activate them again instead of simply activating EVERY task.
CodePudding user response:
I guess what you want is to collect an array of objects storing the computername, the taskpath and the task name for each of the disabled tasks.
Try:
$result = Invoke-Command -ComputerName 'xxx-11', 'xxx-12', 'xxx-14' -ScriptBlock {
Get-ScheduledTask -TaskPath '\yyy\' | Where-Object {'running','ready' -contains $_.State} | ForEach-Object {
$null = $_ | Disable-ScheduledTask
# output an object to be collected in $result
$_ | Select-Object @{Name = 'ComputerName'; Expression = {$env:COMPUTERNAME}}, TaskPath, TaskName
}
}
# remove the extra properties Invoke-Command added
$result = $result | Select-Object * -ExcludeProperty PS*, RunSpaceId
You now have the info you need to reactivate the tasks later in variable $result
If you like you can save this to CSV file for later reference:
$result | Export-Csv -Path 'X:\Somewhere\DisabledTasks.csv' -NoTypeInformation