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
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?
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.