Mike Rockwell (Progress Software) asked a question.

Restart Windows Computer (PowerShell)

This uses the credential assigned to the machine and the PowerShell restart is set to force. Because if anyone is logged in without the -force, the script will stop.

 

  1. #Get the device information
  2. $ip = $Context.GetProperty("Address");
  3. $DnsEntry = [System.Net.DNS]::GetHostByAddress($ip);
  4. $DnsName = [string]$DnsEntry.HostName;
  5.  
  6. # Get the Windows credentials
  7. $WinUser = $Context.GetProperty("CredWindows:DomainAndUserid");
  8. $WinPass = $Context.GetProperty("CredWindows:Password");
  9. $pwd = convertto-securestring $WinPass -asplaintext -force;
  10. $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $WinUser,$pwd;
  11.  
  12. if(Restart-Computer -ComputerName $DnsName -Credential $cred -Force)
  13. {
  14. $Context.SetResult(1,"Failed to restart %Device.DisplayName");
  15. }
  16. else
  17. {
  18. $Context.SetResult(0, "%Device.DisplayName was successfully restarted");
  19. }

 


  • OIS Infrastructure OIS (Wisconsin State Laboratory of Hygiene)

    Would it be possible to use PowerShell in an Action to restart a remote computer and provide a reason for the reboot (perhaps using "Write-EventLog")? I have been investigating this for some time with no luck.

    • Mike Rockwell (Progress Software)

      Write it to the device's event log? Yes, you can write to the Event log with PowerShell, you would just have to modify the same script to update the EV then reboot it.

       

      If you are using an Action Policy, just have the action policy fire a PS script to update the device's EV, then fire the reboot. Or if you want to write it to the WUG EV, then there's an action for that.