Execute powershell commands on a VM from the host.

Im wondering if its 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.

January 30th, 2015 6:05pm

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.

Free Windows Admin Tool Kit Click here and download it now
January 30th, 2015 6:43pm

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 fram

January 31st, 2015 12:15am

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.

Free Windows Admin Tool Kit Click here and download it now
January 31st, 2015 6:59pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics