1

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
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
Julius
  • 946
  • 1
  • 10
  • 26
  • have you configured PSRemoting? added them in the trustedhosts list ? – Ranadip Dutta Apr 26 '17 at 07:34
  • Yes, all workes fine if I just login on the remote machine first. – Julius Apr 26 '17 at 07:40
  • no no.. from source system to destination system has the PSRemoting been configured ? – Ranadip Dutta Apr 26 '17 at 07:42
  • I run everything from the source. Only exception is that i login on the destination, and then run the invoke command from the source. – Julius Apr 26 '17 at 07:43
  • could you pls do a test-wsman from the source system to the destination with port also (5985) – Ranadip Dutta Apr 26 '17 at 07:46
  • PS C:\windows\system32> test-wsman -ComputerName $computer wsmid : http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd ProtocolVersion : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd ProductVendor : Microsoft Corporation ProductVersion : OS: 0.0.0 SP: 0.0 Stack: 2.0 – Julius Apr 26 '17 at 07:52
  • pls check the answer given as reference – Ranadip Dutta Apr 26 '17 at 07:55

3 Answers3

1

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
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
  • Thanks, It looks like I have a firewall problem after adding a second adapter to the VM. I will try to work that out and get back with results. – Julius Apr 26 '17 at 07:59
  • 1
    Sure. If the answer is helpful then accepting this would be appreciable. – Ranadip Dutta Apr 26 '17 at 08:00
  • I still got the issue after verifying that this was setup correctly. I did, however, find out that the WinRM service has a delayed startup. So the service was probably not running yet. I adjusted the Start-Sleep command to wait 4 minuttes and then it is working. – Julius Apr 26 '17 at 09:09
  • I also reverted the VM back to the state I had when asking the question. Waiting longer also solved the issue. So even dough I had some warnings about firewall issues, that was not the problem on this issue. I will answer the question my self. – Julius Apr 26 '17 at 09:18
  • Sure.. Go ahead. Provide screenshots also if possible – Ranadip Dutta Apr 26 '17 at 10:52
0

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.

Community
  • 1
  • 1
ikkjo
  • 735
  • 1
  • 9
  • 18
  • The VM is Win7 and as meantioned, the invoke command runs fine if I manuallly log into the VM before running invoke command. This should indicate that WinRM is setup correctly? – Julius Apr 26 '17 at 07:47
  • no it never states that ps remoting is configured or not. – Ranadip Dutta Apr 26 '17 at 07:48
  • I believe WinRM is not configured by default on Windows 7. See if this helps: https://technet.microsoft.com/en-us/library/ff700227.aspx The problem is that you cannot talk remotely to the VM in order to run the ps script. So it is working if you log in, but executing remote commands which is done via WinRM is not working. Which is what you need to trouble shoot now. – ikkjo Apr 26 '17 at 07:50
  • Thanks, I'll take a look. – Julius Apr 26 '17 at 07:54
0

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.

Julius
  • 946
  • 1
  • 10
  • 26