locked
Execute powershell commands on a VM from the host. RRS feed

  • Question

  • I’m wondering if it’s possible to execute PowerShell commands from the host that will be execute on a virtual machine.

    I have around 10 VM and I need to run the same command on all of it and I need to do the same in other 15 host.   None of the VM belong to the same domain.

    Friday, January 30, 2015 3:05 PM

Answers

  • Use PowerShell Remoting.

    It would be a security risk to be able to push commands into a VM from the hypervisor.  But you could push files into a VM from the hypervisor, and then use PowerShell Remoting to run those files.


    Brian Ehlert
    http://ITProctology.blogspot.com
    Learn. Apply. Repeat.

    • Proposed as answer by ThmsRynr Friday, January 30, 2015 7:35 PM
    • Marked as answer by Elton_Ji Sunday, February 8, 2015 1:54 AM
    Friday, January 30, 2015 3:43 PM
  • Totally agree with Brian - use PowerShell.  I do a lot of physical and virtual machine management via remote PowerShell.

    "None of the VM belong to the same domain."  This will make remote PowerShell a little more challenging due to getting the security right.  But it can be done.  And, the security challenge is not unique to PowerShell.  All remote management uses the same underlying framework.


    . : | : . : | : . tim


    • Edited by Tim CerlingMVP Friday, January 30, 2015 9:16 PM
    • Proposed as answer by Elton_Ji Monday, February 2, 2015 2:00 AM
    • Marked as answer by Elton_Ji Sunday, February 8, 2015 1:54 AM
    Friday, January 30, 2015 9:15 PM
  • Yes, it's possible. 

    It's not recommended to do any administrative tasks from the host. You should remove the GUI and all roles from the host except things related to Hyper-V and used in your environment like Failover Clustering and MPIO. 

    As a best practice, set a Windows 8.1 VM as your management station and install RSAT tools on it including Hyper-V Manager. It also comes with Powershell modules you need to run Powershell commands and scripts against all your hosts and VMs.

    On the machines to be managed by Powershell you need to enable Powershell Remoting. In Server 2012 and above and Windows 8 and above this is enabled out of the box (even core versions). In older versions of Windows enable Powershell Remoting manually as shown in this post.

    On the management Win 8.1 VM that has RSAT installed, if the managed machines belong to the same domain as the managing station, you're good to go. If not, run this command on the managing station:

    winrm s winrm/config/client "@{TrustedHosts=""My2003Server,host2,host3,vm4""}"

    Execute your scripts against multiple machines by using Invoke-Command as in:

    'Computer1','Computer2','Computer3' | % {
        $Result = Invoke-Command -Computer $_ -ScriptBlock {
            Get-Process
        }
        $Result
    }

    This example, will execute the commands in the scriptblock on each of the 3 computers in line1, and return the result.

    For more information see Don Jones' Secrets of Powershell Remoting eBook.


    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable) _________________________________________________________________________________ Powershell: Learn it before it's an emergency http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    • Proposed as answer by Elton_Ji Monday, February 2, 2015 2:01 AM
    • Marked as answer by Elton_Ji Sunday, February 8, 2015 1:54 AM
    Saturday, January 31, 2015 11:58 PM

All replies

  • Use PowerShell Remoting.

    It would be a security risk to be able to push commands into a VM from the hypervisor.  But you could push files into a VM from the hypervisor, and then use PowerShell Remoting to run those files.


    Brian Ehlert
    http://ITProctology.blogspot.com
    Learn. Apply. Repeat.

    • Proposed as answer by ThmsRynr Friday, January 30, 2015 7:35 PM
    • Marked as answer by Elton_Ji Sunday, February 8, 2015 1:54 AM
    Friday, January 30, 2015 3:43 PM
  • Totally agree with Brian - use PowerShell.  I do a lot of physical and virtual machine management via remote PowerShell.

    "None of the VM belong to the same domain."  This will make remote PowerShell a little more challenging due to getting the security right.  But it can be done.  And, the security challenge is not unique to PowerShell.  All remote management uses the same underlying framework.


    . : | : . : | : . tim


    • Edited by Tim CerlingMVP Friday, January 30, 2015 9:16 PM
    • Proposed as answer by Elton_Ji Monday, February 2, 2015 2:00 AM
    • Marked as answer by Elton_Ji Sunday, February 8, 2015 1:54 AM
    Friday, January 30, 2015 9:15 PM
  • Yes, it's possible. 

    It's not recommended to do any administrative tasks from the host. You should remove the GUI and all roles from the host except things related to Hyper-V and used in your environment like Failover Clustering and MPIO. 

    As a best practice, set a Windows 8.1 VM as your management station and install RSAT tools on it including Hyper-V Manager. It also comes with Powershell modules you need to run Powershell commands and scripts against all your hosts and VMs.

    On the machines to be managed by Powershell you need to enable Powershell Remoting. In Server 2012 and above and Windows 8 and above this is enabled out of the box (even core versions). In older versions of Windows enable Powershell Remoting manually as shown in this post.

    On the management Win 8.1 VM that has RSAT installed, if the managed machines belong to the same domain as the managing station, you're good to go. If not, run this command on the managing station:

    winrm s winrm/config/client "@{TrustedHosts=""My2003Server,host2,host3,vm4""}"

    Execute your scripts against multiple machines by using Invoke-Command as in:

    'Computer1','Computer2','Computer3' | % {
        $Result = Invoke-Command -Computer $_ -ScriptBlock {
            Get-Process
        }
        $Result
    }

    This example, will execute the commands in the scriptblock on each of the 3 computers in line1, and return the result.

    For more information see Don Jones' Secrets of Powershell Remoting eBook.


    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable) _________________________________________________________________________________ Powershell: Learn it before it's an emergency http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    • Proposed as answer by Elton_Ji Monday, February 2, 2015 2:01 AM
    • Marked as answer by Elton_Ji Sunday, February 8, 2015 1:54 AM
    Saturday, January 31, 2015 11:58 PM
  • Hi Sir,

    I would like to check if you need further assistance .

    Best Regards,

    Elton JI


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com .

    Monday, February 2, 2015 2:00 AM