2014-09-03 17 views
0

我创建了一个脚本基于http://blogs.technet.com/b/bshukla/archive/2011/04/28/how-revert-changes-made-by-enable-psremoting.aspx通过启用,PSRemoting

当我在PowerShell控制台中输入以下命令来恢复被启用,PSRemoting所做的更改做出还原变更,它将按预期工作,也就是说,它禁用PSRemoting

c:\> winrm delete winrm/config/listener?address=*+transport=HTTP 

c:\> Stop-Service winrm 

c:\> Set-Service -Name winrm -StartupType Disabled 

c:\> Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name LocalAccountTokenFilterPolicy -Type dword -Value 0 

当我执行一个脚本

$scriptblock = {winrm delete winrm/config/listener?address=*+transport=HTTP} 

$host_name = (hostname.exe) 

Invoke-Command -ScriptBlock $scriptblock -ComputerName $host_name 

Stop-Service winrm 

Set-Service -Name winrm -StartupType Disabled 

Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name LocalAccountTokenFilterPolicy -Type dword -Value 0 

我收到不需要输出

WARNING: The network connection to local_computer has been interrupted. Attempting to reconnect for up to 4 minutes... 
WARNING: Attempting to reconnect to local_computer ... 
WARNING: Attempting to reconnect to local_computer ... 
WARNING: Attempting to reconnect to local_computer ... 
WARNING: Attempting to reconnect to local_computer ... 
WARNING: Attempting to reconnect to local_computer ... 
WARNING: Attempting to reconnect to local_computer ... 
WARNING: Attempting to reconnect to local_computer ... 
WARNING: The reconnection attempt to local_computer failed. Attempting to disconnect the session... 
WARNING: Computer local_computer has been successfully disconnected. 
Invoke-Command : Network connectivity to local_computer has been lost and the reconnection attempt failed. Please repair the network connection and reconnect using Connect-PSSession or Receive-PSSession. 
At C:\share\ps_disable.ps1:6 char:1 
+ Invoke-Command -ScriptBlock $scriptblock -ComputerName $host_name 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : OperationTimeout: ([PSSession]Session5:PSSession) [Invoke-Command], RuntimeException 
    + FullyQualifiedErrorId : PowerShellNetworkFailedStartDisconnect,Microsoft.PowerShell.Commands.InvokeCommandCommand 

WARNING: Session Session5 with instance ID c7243bdf-bb9f-49c1-a1b0-9798969b0f99 on computer local_computer has been successfully disconnected. 
WARNING: Session Session5 with instance ID c7243bdf-bb9f-49c1-a1b0-9798969b0f99 has been created for reconnection. 
  1. 我如何消除“警告......”
  2. 如何提高执行速度,而不是它取4分钟,即Attempting to reconnect for up to 4 minutes
  3. 如何解决调用命令:网络连接到local_computer丢失,重新连接尝试失败。
+1

您是否尝试过Invoke-Command的-InDisconnectedSession开关?它可能会处理所有三个问题。 – TheMadTechnician 2014-09-03 18:07:07

+0

@TheMadTechnician它的工作原理,谢谢! – Glowie 2014-09-03 18:35:28

回答

1

将其移至答案以便问题得以解决。

使用-InDisconnectedSession开关立即断开运行该命令的会话,因此连接丢失不会导致脚本停顿或导致警告。它将返回一个PSSession对象,稍后您可以使用该对象来获取命令的结果。