【问题标题】:how winrm can be enabled using psexec from powershell如何使用 powershell 中的 psexec 启用 winrm
【发布时间】:2017-05-26 04:31:04
【问题描述】:

我编写了一个 powershell 脚本来远程执行一些操作系统验证。但是当远程服务器未启用 winrm 时,我收到以下消息。那么我如何强制 winrm 使用 psexec 远程启用?

**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 \$host -s powershell "Enable-PSRemoting -force" 中添加了一个命令(这里 $host 将给出主机名)这正在执行,同时我收到以下消息也。

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 时才可能创建条件! 如果可能,请告诉我怎么做?

【问题讨论】:

标签: powershell


【解决方案1】:

函数 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。

【讨论】:

  • 假设pstools位于system32路径,是否需要将路径改成c:\windows\system32\pstools\psexec.exe \\$computer -s C:\Windows\System32\winrm .cmd qc -安静
  • 你应该。就我而言,我已将 PStools 文件夹和 Temp 文件夹放在一起,我的脚本所在的位置以及该 computer.txt 文件。如果您在其他文件夹中有 pstools,则应使用该路径。
  • 非常感谢弗拉德米尔
猜你喜欢
  • 2022-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-02
  • 2021-12-02
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
相关资源
最近更新 更多