Invoke-command fails until I run winrm quickconfig on the remote server

I am trying to run a batch script package on a remote server via powershell. Pretty straightforward:

$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 be a random error error error

Image link here

On the remote computer, running the powershell winrm quickconfig to configure the remote control service tells me that it is already configured to accept requests / remote control.

Only after running this command invoke-command will pass correctly. How? I didn’t even set anything up. How can i fix this?

+4
source share
2 answers

Check out the help for winrm (by the way, this is Windows exe, not the 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) 

Your remote computers may have a subset of 4 steps, but not all of them at once until you run the utility. In particular, the configuration of the listener has given me problems in the past. You can check the listener configuration before / after using below (run as administrator in a remote box):

 dir wsman:\localhost\listener 

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

+4
source

I came across this before and it turned out to be a script execution policy. Use the Set-ExecutionPolicy cmdlet for affected hosts. For testing purposes, use this command parameter:

 Set-ExecutionPolicy Unrestricted 

I highly recommend that you do not leave the parameter in Unrestricted, but it is useful for identifying possible causes.

For reference:

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

+2
source

Source: https://habr.com/ru/post/1497061/


All Articles