2014-09-03 16 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對象,稍後您可以使用該對象來獲取命令的結果。