使用以下 powershell 代码,我可以修改远程 Windows 7 机器的静态 IP。
Write-Host("Modifying IP... ")
Invoke-Command -ComputerName $RemoteMachine -Credential $Cred -ScriptBlock {
& {
$adapter = Get-WmiObject win32_NetworkAdapterConfiguration -filter "Index = 7"
$adapter.EnableStatic($Using:NewIP,"255.255.255.0")
} }
这是我脚本的最后一部分。更改IP后,连接丢失。这是意料之中的。问题是脚本仍然尝试重新连接大约 4 分钟,然后它放弃了。输出如下所示:
Modifying IP...
WARNING: The network connection to 10.1.1.50 has been interrupted. Attempting to reconnect for up to 4 minutes...
WARNING: Attempting to reconnect to 10.1.1.50 ...
WARNING: Attempting to reconnect to 10.1.1.50 ...
WARNING: Attempting to reconnect to 10.1.1.50 ...
WARNING: Attempting to reconnect to 10.1.1.50 ...
WARNING: Attempting to reconnect to 10.1.1.50 ...
WARNING: Attempting to reconnect to 10.1.1.50 ...
WARNING: The reconnection attempt to 10.1.1.50 failed. Attempting to disconnect the session...
WARNING: Reconnection attempt canceled. Please repair the network connection and reconnect using Connect-PSSession or Receive-PSSession.
PS C:\Windows\system32>
我怎样才能避免这种情况?