通过 powershell 更改远程 IP 后断开连接

通过 powershell 更改远程 IP 后断开连接

使用以下 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>

我怎样才能避免这种情况?

答案1

发送命令而不等待任何结果

如果您只是想向远程计算机发送命令而不想等待任何结果,只需使用-InDisconnectedSessionInvoke-Command参数。

Write-Host('Modifying IP... ')
Invoke-Command -ComputerName $RemoteMachine -Credential $Cred -InDisconnectedSession -ScriptBlock { 
    $adapter = Get-WmiObject win32_NetworkAdapterConfiguration -filter 'Index = 7'
    $adapter.EnableStatic($Using:NewIP, '255.255.255.0')
} 

发送命令而不等待结果立即地

如果您需要一段时间后从远程计算机获得结果,但又不想立即获得结果,那么您必须使用远程 PSSession 并同时使用上面提到的参数。

$newIP         = '172.168.0.1'
# a friendly name for the session. Useful for 'Get-PSSession'
$psSessionName = 'ipAddressChange'

$invokeCommandParam = @{
    SessionName           = $psSessionName
    ComputerName          = $RemoteMachine
    Credential            = $Cred
    InDisconnectedSession = $true
    ScriptBlock           = {
        $adapter = Get-WmiObject win32_NetworkAdapterConfiguration -filter 'Index = 7'
        $adapter.EnableStatic($Using:NewIP, '255.255.255.0')
    }
}

$psSession = Invoke-Command @invokeCommandParam 

# wait some seconds for the remote machine to execute the command
Start-Sleep -Seconds 7

Write-Host 'Waiting for remote machine ' -NoNewline
while (!(Test-Connection $RemoteMachine -IPv4 -Quiet -TimeoutSeconds 2)) {
    Write-Host '.' -NoNewline
}

# receive output from remote machine
$result = Receive-PSSession -Session $pSSession
$result

<# 
If Receive-PSSession above does not work due to IP address change, try to get the PSSession again:
    $psSession = Get-PSSession -ComputerName $RemoteMachine -Credential $Cred -Name $psSessionName
    $result    = Receive-PSSession -Session $psSession
    $result
#>

更多信息请参阅:

答案2

您可以使用 $null 或 [void],这样就完全看不到响应,但它仍然会发生,或者,如果您不想看到它,则需要使用 try/catch 或 if /then 来硬退出脚本,或者具体来说,您需要编写代码来表示您想要多少个响应。所以这个...

ping /n 2 /l 1000 localhost

或这个...

Test-Connection -Count 1 -ComputerName localhost

相关内容