Invoke-WmiMethod - 快速修复 PsRemoting/WinRM 问题(用于 Invoke-Command 用法)

Invoke-WmiMethod - Quickly fix PsRemoting/WinRM problems (for Invoke-Command usage)

如果您的计算机无法使用 Invoke-Command 访问,因为 WinRm 不是 运行 或 PsRemoting 被禁用这里是一个很好的,肯定的方法我发现每次都有效,在我的环境中最少:

 $target_comp = "abc1234"

 Invoke-WmiMethod -ComputerName $target_comp -Path win32_process -Name create -ArgumentList "powershell.exe -command Enable-PSRemoting -SkipNetworkProfileCheck -Force"
 Invoke-WmiMethod -ComputerName $target_comp -Path win32_process -Name create -ArgumentList "powershell.exe -command winrm quickconfig -quiet"


 do {
     $testpsremoting = invoke-command -computername $target_comp -scriptblock {"test"}
    } while (!$testpsremoting) 



    #REST OF CODE

解释:

-声明你的计算机名称的变量。
-运行 通过 Invoke-WmiMethod 启用 PsRemoting 和设置 WinRM 的两个命令。
*自 Invoke-WmiMethod returns 立即无需等待命令实际完成:
- 制作一个循环,直到启用 PsRemoting(直到测试 Invoke-Command 工作)。

不再有 Invoke-Command 问题!随心所欲地享受和微调。

您可以使用Get-wmi直接获取结果

Get-WmiObject Win32_service -ComputerName $poste | select State,Name,DisplayName

问题已更改,以便为社区提供答案和有用的解决方案。