Julius
Julius

Reputation: 946

Invoke-Command not executed when user not logged in

My goal is to reset a VM and run some installers on it. This is what I do:

Write-Host "Revert VM to snapshot"
Get-VM -Name $vmName | Restore-VMSnapshot -Name "7Zip" -Confirm:$false

Write-Host "Start VM"
Get-VM -Name $vmName | Start-VM

Write-Host "Wait for VM to be ready"
Wait-VM $vmName
Write-Host "Wait a little bit more..."
Start-Sleep -Seconds 20

Write-Host "Create credetials"
$computer = "W7P-MY-COMPUT"
$username = $computer + "\localadmin"
$password = cat C:\build\mysecurestring.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential `
     -argumentlist $username, $password

Write-Host "Invoke remote script"
Invoke-Command -ComputerName $computer -FilePath c:\build\GetInstallers.ps1 -Credential $cred

The script that is referenced downloads some msi files and installs them. All this works fine if I run all the VM commands, log into the computer manually and then execute the Invoke-Command.

But running all at once gives me this output:

Revert VM to snapshot
Start VM
Wait for VM to be ready
Wait a little bit more...
Create credetials
Invoke remote script
[W7P-SAMOS-WEB-I] Connecting to remote server W7P-SAMOS-WEB-I failed with the following error message : WinRM cannot complete the operation. Verify that the 
specified computer name is valid, that the computer is accessible over the network, and that a firewall exception for the WinRM service is enabled and allows 
access from this computer. By default, the WinRM firewall exception for public profiles limits access to remote computers within the same local subnet. For 
more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo          : OpenError: (W7P-SAMOS-WEB-I:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : WinRMOperationTimeout,PSSessionStateBroken

Upvotes: 1

Views: 1834

Answers (3)

Julius
Julius

Reputation: 946

The winrm service was not running yet when I tried to do Invoke-Command. Waiting longer using the Start-Sleep command (approx 3-4 min) fixed the issue.

Upvotes: 0

Ranadip Dutta
Ranadip Dutta

Reputation: 9133

Check the service status. Make sure its up and running

get-service winrm

For enabling PS remoting, use:

Enable-PSRemoting –force

Add the systems to the trusted hosts list:

winrm s winrm/config/client '@{TrustedHosts="RemoteComputer"}'

Validate it:

winrm quickconfig

Once done run your invoke-command from source to destination and see the result. If error comes, pls post it.

Apart from that request you to double check the firewall and make sure it is disabled.

Or approach this:

Enable-PSRemoting -SkipNetworkProfileCheck -Force
Set-NetFirewallRule -Name "WINRM-HTTP-In-TCP-PUBLIC" -RemoteAddress Any

Upvotes: 1

ikkjo
ikkjo

Reputation: 785

Depending on the OS version of your VM, WinRM might not be configured, you should be fine on Windows Server 2012 and above, still worth checking it is enabled. You can then check discussion here: Invoke-Command failed: WinRM cannot complete the operation which touches the points Ranadip is mentioning in the comment.

Upvotes: 0

Related Questions