Using Powershell to Manage Hyper-V Integration Services in Server 2012 R2

 

A while back, Eric Siron, one of my fellow Altaro.com bloggers, did an excellent write-up regarding Hyper-V integration services. Said post did a great job explaining the need for integration services, what they are, and how they can help System Admins do their jobs.

As of the Windows Server 2012 R2 release, not much has changed in the realm of Hyper-V Integration Services, but some new functionality has been added, such as the new guest services integration, which allows files to be copied from host to guest via the console connection. No virtual networking in the guest is needed to complete said file transfer. As long as you have an active console session to a defined VM, you can copy a file directly to it from the host system via the GUI.

This is certainly a useful feature, but what happens when you need to copy a file via this methodology, but you don’t have access to a machine with Hyper-V Manager or Failover Cluster Manager? The answer, of course, lies with Powershell!

In this post I will be covering the Powershell cmdlet that provides the file transfer functionality, as well as the cmdlets previously available in Windows Server 2012 (non-R2) that facilitated the management of the various Hyper-V Integration Services features.

PLEASE NOTE: In the below examples I have used the Enter-PSSession cmdlet to Powershell into a Hyper-V host remotely This is in no way a requirement as these commands could easily be executed locally or via RDP on the target system as well. However, I’ve demoed it this way due to the amount of time it would take to run said commands locally on several hosts or guest VMs at once, which is a likely scenario when dealing with Integration Services en-mass across an entire environment. Powershell’s remote session and remote targeting capabilities help cut down the time needed for redundant tasks. Additionally, you’ll see use of the Invoke-Command cmdlet later in this post, which I have utilized for very much the same reason.

Eric will be covering Hyper-V management with Powershell’s remote management features in more depth in an upcoming post for those interested.

Integration Service Management

There are 3 cmdlets provided in the Hyper-V powershell module that gives us what we need to start, stop and review the status of existing Hyper-V Integration Services.

First off, we need to list what exactly those command are. Knowing they are Hyper-V functions, and therefore present in the Hyper-V Powershell module, I would issue:

Get-Command –Module Hyper-V -Name *integration*

Which will return the below:

IntegrationServices_Get_Command_Rev2

With that in mind, If I want to return a listing of the current Integration Service status on several VMs that reside on the current host, I would just run:

Get-VMIntegrationService –VMName <VM Names in comma separated list>

HyperVIntegration_Powershell_Screen2

As you can see in the example above, I’ve queried two virtual machines for Integration Services status. From this screenshot we can see several items.

  • All Integration Services are enabled on both VMs with the exception of the Guest Service Interface, which is the new Server 2012 R2 service that provides the console based file transfers. We’ll enable this shortly.
  • I can also see, that all services are healthy with the exception of the 3 labeled No Contact. This is usually caused by a windows service being stopped on the target guest machine.

I can run the below command to get a status of the associated Windows services that correspond to the 3 Integrations we suspect to be in a stopped state above:

Invoke-Command –ComputerName <Hostname> -Credential <Domain><Username> -Scriptblock {Get-Service -Name vmi*}

IntegrationServices_Get_Service_Rev2

We see that, as suspected, the needed services are not in a running state. We can utilize a very similar command to start them:

Invoke-Command –ComputerName <Hostname> -Credential <Domain><Username> -Scriptblock {Start-Service -Name <comma separated service names>}

IntegrationServices_Start_Service_Rev2

Once I’ve started the services again, running the below will verify whether they are running or not:

Invoke-Command –ComputerName <Hostname> -Credential <Domain><Username> -Scriptblock {Get-Service -Name vmi*}

Followed then by:

Get-VMIntegrationService –VMName <VM Names in comma separated list>

IntegrationServices_VMInt_Rev2

To show us that all integrations are now healthy and functioning normally. (Shown above)

In addition, I’ve found that another good use of the Get-VMIntegrationService cmdlet is to use it to report what Integration services are out of date on a particular VM. This can be done via:

Get-VMIntegrationService –VMName <VM names in comma separated list> | Where-Object {$_.SecondaryOperationalStatus –eq 'ProtocolMismatch'}

This can be especially useful when you need to determine which VMs, out of a vast number of guests, need to have the integrations services package updated.

Going forward if I want to disable one of the integration services I would utilize the Disable-VMIntegrationService cmdlet with the below syntax

Disable-VMIntegrationService –Name <Service Name> -VMName <Target Guest VM Name>

HyperVIntegration_Powershell_Screen5

We can see that in the above example that once disabled, the Get-VMIntegrationService cmdlet returns the status of that particular service as false.

To re-enable we use the, yup….  You guessed it, Enable-VMIntegrationService cmdlet!

Enable-VMIntegrationService –Name <Service Name> -VMName <Target Guest VM Name>

HyperVIntegration_Powershell_Screen6

And we’re back in business on the Heartbeat service!

Copying Files with Copy-VMFile

So, thus far we’ve seen how to view and manage the various Hyper-V integration services, how do we utilize the new Copy-VMFile cmdlet that was introduced in Windows Server 2012 R2?

First off, we need to enable that particular service. As we can see above the Guest Service Interface service is in a disabled state.

HyperVIntegration_Powershell_Screen7

Now that the mentioned service is enabled we can transfer files from host to guest with some relatively easy syntax.

Copy-VMFile “<Name of target Guest VM>” –SourcePath “<Path to file on host system>” –DestinationPath “<Path to new location on guest system>” –CreateFullPath –FileSource Host

HyperVIntegration_Powershell_Screen8

Then we browse to the destination and verify that the new directory and file exists!

HyperVIntegration_Powershell_Screen9

Summary

As you can see, managing and working with Hyper-V Integration Services via Powershell is no major feat. It’s actually quite simple and intuitive if you have any working knowledge of Powershell. In addition to manual use, these concepts could be incorporated into a script for mass Integration Services maintenance of entire virtual infrastructures, making a seemingly daunting task feel like a walk on easy street.

Thanks for reading!

 

Altaro Hyper-V Backup
Share this post

Not a DOJO Member yet?

Join thousands of other IT pros and receive a weekly roundup email with the latest content & updates!

17 thoughts on "Using Powershell to Manage Hyper-V Integration Services in Server 2012 R2"

  • Irfan says:

    Is it possible to Reset Guest OS Administrator password From Host.

    • In the case of a lost guest administrative password, you would treat that much the same as a physical box. You’ll want to load up a password recovery tool by mounting an ISO to the affected guest VM and boot to it. Otherwise, if you know the current administrative password you would simply log into the guest and change it, or use PowerShell remoting.

      Hope this helps!

  • Irfan says:

    Thanks!

    I need powershell/WMI command that reset pasword Guest OS from Host.

    Thanks Agian

  • Irfan says:

    Thanks!

    I need powershell/WMI command that reset pasword Guest OS from Host.

    Thanks Agian

  • Dave Patrick says:

    What is the meaning of PrimaryStatusDescription SecondaryStatusDescription fields?

  • JOEL BLOCH says:

    Hello,

    I am new to both Powershell and Hyper-V and I use extensively your posts on it, which are extremely helpful. So first of all : THANKS !
    I am trying to automate VM provisioning with Powershell.
    In order to copy files to a new created VM, I try to enable the Guest Service Interface” as you describe, but I guet the following error:

    PS D:VMsscripts> Enable-VMIntegrationService -Name “Guest Service Interface” -VMName “WebServer”
    Enable-VMIntegrationService : No integration component with the given name could be found.
    At line:1 char:1
    Enable-VMIntegrationService -Name “Guest Service Interface” -VMName ” …
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CategoryInfo : InvalidArgument: (:) [Enable-VMIntegrationService], VirtualizationException
    FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.EnableVMIntegrationService

    Do you have any idea what I do wrong? thanks!

Leave a comment or ask a question

Your email address will not be published. Required fields are marked *

Your email address will not be published.

Notify me of follow-up replies via email

Yes, I would like to receive new blog posts by email

What is the color of grass?

Please note: If you’re not already a member on the Dojo Forums you will create a new account and receive an activation email.