Restart-Computer enhanced in powershell V3

来源:互联网 发布:网络银行的注册号码 编辑:程序博客网 时间:2024/05/17 03:40

In PowerShell v3, Restart-Computer now has a number of useful newparameters. For example, you can restart a remote machine and wait for thereboot process to finish.

-Wait: Halts the script until the machine has rebooted

-Timeout: Seconds to wait for the machine to restart

-For: Considers the computer to have restarted when the specified resourcesare available. Valid values: WMI, WinRM, and PowerShell.

-Delay: Interval in seconds used to query the remote computer to determineits availability specified by -For.


Before the PSv3 introduce, We have to use test-connection to make sure the IP connection already established , use get-service to make sure  some service already running like WinRM.

The old code like:

# Test connectionTest-Connection -ComputerName $hst.ComputerName -Quiet

# Check the WinRM services status.Get-Service -ComputerName $hst.ComputerName -Name WinRM -ErrorAction SilentlyContinue | Where-Object{$_.Status -eq "Running"}

The new code like:

Restart-Computer -ComputerName $hst.ComputerName -For WinRM