Search code examples
powershellwmi

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


In the event you have computers that are unreachable with Invoke-Command either because WinRm is not running or PsRemoting is disabled here is a good, sure way I've found works everytime, in my environement at least:

 $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

Explanation :

-Declare variable of your computer name.
-Run the two commands to enable PsRemoting and setup WinRM via Invoke-WmiMethod.
*Since Invoke-WmiMethod returns instantly without WAITING for the commands to actually be done:
-Make a loop that runs until PsRemoting is enabled (until the test Invoke-Command works).

No more Invoke-Command problems! Enjoy and fine tune to your heart's content.


Solution

  • Question changed to provide answer and useful fix for the community.