The Most Awesome PowerCLI Cmdlet You Aren’t Using

While all of PowerCLI is remarkable in both it’s power and ease of use, there are some cmdlets that do not get as much airtime as they deserve. One of those, is Invoke-VMScript. From Get-Help InvokeVMScript

NAME
    Invoke-VMScript

SYNOPSIS
    Executes the specified PowerShell script in the guest OS of each of the specified virtual machines.

SYNTAX
    Invoke-VMScript [-ScriptText] <String> [-VM] <VirtualMachine[]> [-HostCredential <PSCredential>] [-HostUser <String>] [-HostPassword <SecureString>] [-GuestCredential <PSCredential>] [-GuestUser <String>] [-GuestPassword <SecureString>] [-ToolsWaitSecs <Int32>] [-Server <VIServer[]>] [-WhatIf] [-Confirm] [<CommonParameters>]

DETAILED DESCRIPTION
    Executes the specified PowerShell script in the guest OS of each of the specified virtual machines. The virtual machines must be powered on and have PowerShell and VM Tools installed. In order to authenticate with the host or the guest OS, one of the HostUser/HostPassword (GuestUser/GuestPassword) pair and HostCredential (GuestCredential) parameters must be provided.

Take a close look at the synopsis line, in fact, read it again. I’ll wait. Done? Good. Invoke-VMScript executes a specified PowerShell script INSIDE the guest OS. Starting to feel the power? No? Fine, how bout a use case? It’s yearly inventory time. You know… that time. Your boss wants a list of all VM Names, build numbers, serials, versions, and some other random things. Now, one of two things happens, either you procrastinate and then need to extend your deadline so you can log into every box, OR you use the following one-liner:

get-vm | select -first 1 | %{ $_.Name; $_ | Invoke-VMScript -GuestUser "adminUser" -GuestPassword "urPass" -ScriptText "get-wmiobjectwin32_operatingsystem" -HostUser "root" -hostpassword "rootPass" }

That one liner (yes it is all one line) works kinda like this: Get a list of all my VMs, select the first one, then give me the name, and the output of get-wmiobjectwin32_operatingsystem. In turn, it produces the following output:

web.professionalvmware.com

SystemDirectory : C:\WINDOWS\system32
Organization    : ProfessionalVMware
BuildNumber     : 3790
RegisteredUser  : ProfessionalVMware
SerialNumber    : Your Serial Number here
Version         : 5.2.3790

Not convinced of Invoke-VMScript’s World Dominatingly Awesome Power? Well, let me know in the comments.

8 thoughts on “The Most Awesome PowerCLI Cmdlet You Aren’t Using

Comments are closed.