Question

I'm trying to run a batch script on a remote server via powershell. Pretty straight forward:

$password = ConvertTo-SecureString "password" -AsPlainText -Force
$cred= New-Object System.Management.Automation.PSCredential ("domain\user", $password)
$sesh= new-pssession -computername "MSSCA" -credential $cred

invoke-command -session $sesh -scriptblock {
    cmd.exe /C "C:\install.bat"
}

Remove-PSSession $sesh

This seems to randomly fail with the following error error

Image link here

On the remote machine, running the powershell command winrm quickconfig to configure the remote management service informs me that it's already set up to receive requests/remote management.

Only AFTER running this command will invoke-command go through properly. How? I didn't even configure anything. How can I fix this?

Was it helpful?

Solution

Check out the help for winrm (btw this is a Windows exe, not a Powershell command):

> winrm help quickconfig
Windows Remote Management Command Line Tool

winrm quickconfig [-quiet] [-transport:VALUE] [-force]

Performs configuration actions to enable this machine for remote management.
Includes:
  1. Start the WinRM service
  2. Set the WinRM service type to auto start
  3. Create a listener to accept request on any IP address
  4. Enable firewall exception for WS-Management traffic (for http only)

Maybe your remote machines have some subset of the 4 steps enabled, but not all of them at once until your run the utility. In particular, the listener config has given me trouble in the past. You can check the listener config before/after using below (run as admin on a remote box):

dir wsman:\localhost\listener

You can also try running Enable-PSRemoting (must run as admin), which will include additional logging.

OTHER TIPS

I ran into this before and it ended up being the script execution policy. Use the Set-ExecutionPolicy cmdlet on the affected hosts. For testing purposes use this command parameter:

Set-ExecutionPolicy Unrestricted

I strongly recommend you don't leave the setting at Unrestricted, but it's useful for determining possible causes.

For reference:

http://technet.microsoft.com/en-us/library/ee176961.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top