I have a script that I need to run against a bunch of servers in an RDS-Collection, with the intent being to cleanup bad profiles and registry entries.
I have this setup as a scheduled task, but it isn't running the invoke-command lines when run through the scheduled task, but is working if I run from a command line or in the run dialog box.
Script is below.
$RDCB = "groebrdgw01.groebnerassoc.loc"
Import-Module remotedesktop
$Servers = Get-RDSessionHost -CollectionName "farm1collection" -ConnectionBroker $RDCB | Select SessionHost
# Clean each server of temp directories and registry items.
foreach ($Server in $Servers){
$SessionHost = $Server.sessionhost
Invoke-Command -ComputerName $SessionHost -Filepath "C:\_marcocloud\Sched_Tasks\PreventTempProifle_RDSH\C_UsersCleanup.ps1"
Invoke-Command -ComputerName $SessionHost -Filepath "C:\_marcocloud\Sched_Tasks\PreventTempProifle_RDSH\UPDRegCleanup.ps1"
#Write-host -fore yellow "Cleanup done on $Server"
}
I would note that the task is running under a domain admin level account, and has been delegated logon as a batch rights on all servers. Also task has been tested with "Run with highest privileges".