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

Harika Vinnakota 1 Reputation point
2021-05-19T18:01:11.877+00:00

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 for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-05-19T20:02:20.713+00:00

    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://learn.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://learn.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://learn.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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.