question

HarikaVinnakota-3845 avatar image
0 Votes"
HarikaVinnakota-3845 asked MotoX80 answered

Rebooting multiple computers at once at a scheduled time using powershell script

Function set-TaskSchedule {
$listpc = Get-Content "C:\Users\hvinnakota\Desktop\listpc.txt"
foreach ($item in $listpc) {

 Invoke-Command -ComputerName $item {Register-ScheduledJob -Name DumpLogs -ScriptBlock {
   try {
     Start-Process C:\Users\hvinnakota\Desktop\listpc.txt -Trigger (New-JobTrigger -Daily -At 3.00 AM) -ErrorAction Stop
     Write-Host "$item is set!"
   }
   catch {
     Write-Host "$item failed!"       
        
     Restart-Computer -ComputerName $listpc -Force -Wait
   }
 }

}
}
}
set-TaskSchedule


Trying to make this script work, But seems to be not working

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MotoX80 avatar image
0 Votes"
MotoX80 answered

It appears to have multiple problems.

Let's start by formatting the code that you posted using the Code Sample and fixing the indentation.

 Function set-TaskSchedule {
     $listpc = Get-Content "C:\Users\hvinnakota\Desktop\listpc.txt"
     foreach ($item in $listpc) {
         Invoke-Command -ComputerName $item {
             Register-ScheduledJob -Name DumpLogs -ScriptBlock {
                 try {
                     Start-Process C:\Users\hvinnakota\Desktop\listpc.txt -Trigger (New-JobTrigger -Daily -At 3.00 AM) -ErrorAction Stop
                     Write-Host "$item is set!"
                 }
                 catch {
                     Write-Host "$item failed!"       
                     Restart-Computer -ComputerName $listpc -Force -Wait
                 }
             }
         }
     }
 }
 set-TaskSchedule

On line 7 you are doing a start-process command. And you specify a Trigger. But there is no Trigger parameter for a start process.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-5.1

Register-ScheduledJob accepts Trigger so you should move that from line 7 to line 5 (outside of the scriptblock).

https://docs.microsoft.com/en-us/powershell/module/psscheduledjob/register-scheduledjob?view=powershell-5.1

The process that you are trying to start is a text file. That is not executable. It might launch notepad via association, but you really should use the name of the .exe. I would assume that you want to start notepad.exe and pass the file name as a parameter. But that code is in an Invoke-Command ScriptBlock. That is an unattended session on the remote PC. You can start notepad and it will run but no one will ever see the window. It's not associated with a user's desktop session. Does that file even exist on ALL machines?

I do not understand what you are trying to accomplish with the Start-Process command.

Also within the scriptblock you are referencing the $item and $listpc variables. You can't do that without telling Powershell that the variables were defined in the part of the script that initiated the Invoke-Command. You have to refer to them as $using:item and $using:listpc.

See example 9 in this link.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-5.1

Finally you are trying to issue a Restart-Computer command, but you are not restarting just one PC, you are using the $listpc variable which would include all PC names. So you would be running a script on ALL PC's, which would in turn try to reboot ALL of the other PC's.





5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.