2017-04-04 79 views
0

我有寫了一個PowerShell腳本,做一些OS驗證remotely.But當遠程服務器不使用WinRM使我得到以下message.So我怎麼能強迫WinRM的遠程啓用啓用使用psexec?如何WinRM的可使用PSEXEC從PowerShell的

**Connecting to remote server xxxxx.us.oim.com failed with the following error message : The WSMan service could not launch a host process to process the given request. Make sure the WSMan provider host server and proxy are properly registered.** 

我在PowerShell腳本中添加一個命令PsExec.exe \ $主機-s PowerShell的「啓用,PSRemoting -force」(這裏$主機會給主機名)這是執行,並在同一時間,我我也得到下面的消息。

PsExec.exe : Connecting to xxxxxxxxx.us.oim.com... 
At line:72 char:1 
+ PsExec.exe \\$fqdn -s powershell "Enable-PSRemoting -force" 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (Connecting to xxxxxxxxx.us.oim.com...:String) [], RemoteException 
    + FullyQualifiedErrorId : NativeCommandError 

Starting PSEXESVC service on xxxxxxxxx.us.oim.com...Connecting with PsExec service on xxxxxxxxx.us.oim.com...Starting powershell on xxxxxxxxx.us.oim.com... 
powershell exited on xxxxxxxxx.us.oim.com with error code 0 

這有可能創造條件只有當「的WSMan服務無法啓動宿主進程來處理給定的請求」,然後運行命令啓用WinRM的! 如果可能請讓我知道如何?

+0

https://community.spiceworks.com/scripts/show/2703-remotely-enable-winrm-powershell –

回答

0

功能enableWinRM {

$computers = Get-Content "C:\temp\computers.txt" 

foreach ($computer in $computers) { 

$result = winrm id -r:$computer 2> $null 

if ($lastExitCode -eq 0) { 
    Write-Host "WinRM already enabled on" $computer "..." -ForegroundColor green 
} else { 
    Write-Host "Enabling WinRM on" $computer "..." -ForegroundColor red 
    .\pstools\psexec.exe \\$computer -s C:\Windows\System32\winrm.cmd qc -quiet 

     if ($LastExitCode -eq 0) { 
      .\pstools\psservice.exe \\$computer restart WinRM 
      $result = winrm id -r:$computer 2>$null 

      if ($LastExitCode -eq 0) {Write-Host "WinRM successfully enabled!" -ForegroundColor green} 
      else {exit 1} 

     } #end of if 

    } #end of else 
} #end of foreach 

只需調用腳本結束這個功能,那就是它。

請確保您的腳本所在的文件夾中包含pstools。

+0

假設如果pstools位於system32路徑,我們是否需要將路徑更改爲c:\ windows \ system32 \ pstools \ psexec.exe \\ $計算機-s C:\ WINDOWS \ SYSTEM32 \ winrm.cmd QC -quiet – Joe

+0

你應該。在我的情況下,我已經把PStools文件夾放在Temp文件夾中,一起放在我的腳本以及那個computers.txt文件中。如果您在其他文件夾中有pstools,則應使用該路徑。 –

+0

非常感謝弗拉基米爾 – Joe