Search code examples
powershellwmipowershell-remoting

Quickest way to enable PSRemoting after a reboot? Long delay with WMI Method


In our environment I often need to reboot a computer and then send commands to it via Invoke-Command. For this, I obviously need to have PSRemoting enabled on the target machine.

It is enabled by default on all our machines, however after a reboot it takes a while to kick in by itself.

I'm using Invoke-WmiMethod to force enable it as soon as the computer pings instead of waiting.

That works well but it sometimes takes over 30-40 seconds to kick in. It's still faster than the 2-5 minutes it can take if I just let it start by itself.

The command I'm using is:

Invoke-WmiMethod -ComputerName $poste -Path Win32_Process -Name create -ArgumentList "powershell.exe -command Enable-PSRemoting -SkipNetworkProfileCheck -Force"

My questions are:

  1. Why does it take so long before it actually is enabled after that command runs?
  2. Is there a better way you would suggest to enable PSRemoting via WMI or something else?

Solution

  • tldr;

    All you are really doing when running Enable-PSRemoting is starting the WinRM service, everything else that the command does is skipped as you've already configured it.

    The WinRM service is set to Automatic Delayed Start - to make it start faster change this to Automatic.


    The 'delay' you are referring to is the delay in Windows starting the WinRM service.

    Services that start at bootup have two options:

    • Automatic will start the service asap after Windows itself loads
    • Automatic Delayed Start will start the service after a short delay

    The reason for the two types is resource contention - having every service start at the same time will use a huge amount of resources and the end-user will notice this as a slow-down.

    To manage resources effectively Services that are essential to Windows will be set to Automatic and start simultaneously. This consumes lots of system resources, but the user has no choice as these services are generally required for Windows to work properly.

    But for Services that are not essential, Delayed Start is the better option. These start "shortly after boot" when all the Automatic services have started. These are generally service for secondary functionality - eg an updater service.

    The WinRM service is set to Automatic Delayed Start - to make it start faster change this to Automatic. You will take a performance hit for this so test this before making mass changes.


    IMO - don't change the service startup, use code to deal with this delay...

    For example, Restart-Computer with the Wait param will restart a remote computer and wait for connectivity before continuing:

    Restart-Computer -ComputerName "Server01" -Wait -For PowerShell -Timeout 300 -Delay 2