VMware vShield Driver Installation through PowerCLI

While deploying vShield I have found the easiest way to automate the installation of the additional vShield Driver (Now called Guest Introspection Driver) is to use PowerCLI and complete through the invoke-vmscript command.

I will presume that you have PowerCLI installed, else it can be obtained from VMware.com.

*If using an x64 OS you will need to use the x86 version of PowerCLI*

Connect to your vCenter Server where *vCenterServer* is the name of your vCenter Server, I use the $Credential variable to store the credential for this session but you can skip that and use the -user and -password parameters after the connect-viserver if preferred;

$Credential = Get-Credential
Connect-Viserver *vCenterServer* -Credential $Credential

The $Name variable stores the name that is input when the script is run, if you specify a wildcard * for the name this will run against all servers so be warned! In turn you can run against VM1 and VM2 with VM*.

The name is then checked using the Get-VM command to see if the server exists displaying “VM FOUND 🙂” if true or “VM NOT FOUND 🙁” if false. Until the correct name is entered the script will keep prompting for a name.

#Get Name of VM and check against vCenter
DO {
      $Name = Read-Host "Enter the Name or Names using wildcard * of the VM e.g VMName*"
            IF (Get-VM $Name){
                               $Isname = $True
                               Write-Output "VM FOUND :)" -Foreground GREEN
                              } ELSE {
                                       $Isname = $False
                                       Write-Warning "VM NOT FOUND! :(" -Background RED -Foreground BLACK
} UNTIL($Isname = $True)

The below code is only required if the hostname differs from the name of the VM.

#Only required if the name of the VM isn't the hostname (and you use AD)
#Check entered name against AD and list if found

$ComputerNames = Get-ADComputer -LDAPFilter "(name=$Name)" | Select Name

FOREACH ($Computer in $ComputerNames) {
	                                Write-Output $Computer.Name
                                        $VMName = Get-VM -Name $Name 
                                        Write-Host "Attempting to Mount Tools...." 
                                             $Mounttools = Mount-Tools -VM $VMName -ErrorAction Stop                   }CATCH{ 
                                             Write-Warning "UNABLE TO MOUNT TOOLS" 

Next we attempt to mount the VM tools to the VM

Get the drive letter of the VM and store in the $DriveLetter variable (I have seen this fail if the VM has WMI issues).

$DriveLetter = Get-WmiObject Win32_CDROMDrive -Credential $Credential -ComputerName $Computer.Name | Where-Object {$_.VolumeName -match "VMware Tools"} | Select-Object -ExpandProperty Drive

Storing the entire script in $ScriptText to run in the Invoke-VMScript command, replace Administrator and *Password* with your local admin account credentials.

Write-Output "Running Script..."
$ScriptText = "$DriveLetter\setup64.exe /S /v ""%TEMP%\vmmsi.log""""/qn REBOOT=R ADDLOCAL=ALL REMOVE=Hgfs,WYSE"""

#Attempt to run the script with local administrator and password
            	Invoke-VMScript -VM $VMName -ScriptText $ScriptText -ScriptType bat -GuestUser Administrator -GuestPassword *Password* 
        	} CATCH {
            		write-error "UNABLE TO RUN GUEST SCRIPT" -Background RED -Foreground Black

Entire Script available below with additional menu option, split into Functions as I attempt to learn Powershell!
I apologise to any programmers/script writers as my script writing has a lot to be improved.