Invoke-command in PowerShell is not working with Hyper-V to run a script in VMs

Viewing 3 reply threads
  • Author
    Posts
    • #1555793
      Bruno Bernardi
      Participant
      Member Points: 364
      Rank: 2

      Hello.

      I am using a Hyper-V server on a Windows 10 PC.

      This server as several VMs with following operating systems:

      – Windows 10 32 and 64 bit

      – Windows 7 32 and 64 bit

      – Windows 8.1 32 and 64 bit

      My objective is to automate the antivirus update. I manage to do this with Invoke-Command on Windows 10 VMs, but not in Windows 7.

      I have the following error :

      PS C:\Windows\system32> Invoke-Command -VMName "RDITS_win7_32_en" -Script {start "C:\Program Files\McAfee\VirusScan Enterprise\mcupdate.exe"} -Credential (Get-Credential "EUR\SESA105951")
      
      Invoke-Command : L’ordinateur virtuel RDITS_win7_32_en ne présente pas l’état en cours d’exécution. Au caractère Ligne:1 : 1 + Invoke-Command -VMName "RDITS_win7_32_en" -Script {start "C:\Program ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument : (:) [Invoke-Command], ArgumentException + FullyQualifiedErrorId : InvalidVMState,Microsoft.PowerShell.Commands.InvokeCommandCommand

      The Powershell version on Windows 7 VM is 5.1. Could you tell me how execute a remote command on Windows 7 VM ?

      Thanks a lot and Best Regards.

    • #1555808
      Leos Marek
      Moderator
      Member Points: 23,163
      Author of Year 2020Author of the Year 2021
      Rank: 4

      Hello,

      I would use Invoke-command with parameter -ComputerName “hostname” instead of -VMName. That should work, if you have Powershell remoting enabled.

      Cheers

    • #1555909
      Bruno Bernardi
      Participant
      Member Points: 364
      Rank: 2

      Hello Leos.

      Thank you for your help.

      I try to activate Powershell Remoting before to use Invoke-Commad with -ComputerName parameter, but it’s still not working.

      I have this error :

      PS C:\Windows\system32> Enable-PSRemoting -SkipNetworkProfileCheck -Force
      
      PS C:\Windows\system32> Enable-PSRemoting -Force
      
      PS C:\Windows\system32> $cred
      
      UserName                           Password
      --------                           --------
      EUR\SESA105951 System.Security.SecureString
      
      
      
      PS C:\Windows\system32> Invoke-Command -ComputerName "RDITS_win7_32_en" -Script {start "C:\Program Files\McAfee\VirusScan Enterprise\mcupdate.exe"} -Credential $cred
      [RDITS_win7_32_en] La connexion au serveur distant RDITS_win7_32_en a échoué avec le message d’erreur suivant: Le client WinRM ne peut pas traiter la demande car le nom du serveur ne peut pas être résolu. Pour 
      plus d'informations, voir la rubrique d'aide about_Remote_Troubleshooting.
          + CategoryInfo          : OpenError: (RDITS_win7_32_en:String) [], PSRemotingTransportException
          + FullyQualifiedErrorId : ComputerNotFound,PSSessionStateBroken
      
      PS C:\Windows\system32>

      When I want to activate Powershell Remoting on “RDITS__win7_32_en” virtual Machine i have this error :

      PS C:\Windows\system32> Enable-PSRemoting -SkipNetworkProfileCheck -Force
      WinRM is already set up to receive requests on this computer.
      WinRM is already set up for remote management on this computer.
      PS C:\Windows\system32> Enable-PSRemoting -Force
      WinRM is already set up to receive requests on this computer.
      Set-WSManQuickConfig : <f:WSManFault xmlns:f="http://schemas.microsoft.com/wbem/wsman/1/wsmanfault" Code="2150859113"
      Machine="localhost"><f:Message><f:ProviderFault provider="Config provider"
      path="%systemroot%\system32\WsmSvc.dll"><f:WSManFault xmlns:f="http://schemas.microsoft.com/wbem/wsman/1/wsmanfault"
      Code="2150859113" Machine="RDITS7EN32VM.eur.gad.schneider-electric.com"><f:Message>WinRM firewall exception will not
      work since one of the network connection types on this machine is set to Public. Change the network connection type to
      either Domain or Private and try again. </f:Message></f:WSManFault></f:ProviderFault></f:Message></f:WSManFault>
      At line:116 char:17
      +                 Set-WSManQuickConfig -force
      +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidOperation: (:) [Set-WSManQuickConfig], InvalidOperationException
          + FullyQualifiedErrorId : WsManError,Microsoft.WSMan.Management.SetWSManQuickConfigCommand

      I have to ask Firewall authorisations to the company administrator. Do you know what they are ?

      Regards.

    • #1555930
      Michael Pietroforte
      Keymaster
      Member Points: 32,173
      Author of the year 2018
      Rank: 4

      It seems the first issue was caused because the remote machine couldn’t be reached. Maybe the computer was offline or it is a firewall problem.

      On the second machine, the first command worked just fine. Only when you omitted -SkipNetworkProfileCheck the second command failed. For more information, please read this article.

      avatar
Viewing 3 reply threads
  • You must be logged in to reply to this topic.
© 4sysops 2006 - 2022

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account