Last week I attended a Magirus course on administrating you VMware environment using PowerCLI, and below is some of the small scripts I created.

These code sniplets will help you manage your VMware environment and give you some ideas of how powerful the VMware PowerCLI really is.

I’m sure VMware will add even more CMDLETS to the PowerCLI in the feature.

Get the latest PowerCLI here.
A function to load different PSSnapins.
Put it in the beginning of all you Powershell scripts,
to load the different PSSnapin you need.


function LoadSnapin{
  param($PSSnapinName)
  if (!(Get-PSSnapin | where {$_.Name   -eq $PSSnapinName})){
    Add-pssnapin -name $PSSnapinName
  }
}
LoadSnapin -PSSnapinName   "VMware.VimAutomation.Core"

Clone a VM to template.


$VMToClone = "vm_name"
$TemplateName = "TemplateName"
$Datacenter = "Training"
get-vm $VMToClone| stop-vm
New-Template -VM $VMToClone -Name $TemplateName 
   -Location $(Get-Datacenter $Datacenter)

Convert Template to VM – without changing the name.


$TemplateName = "TemplateName"
Set-Template -Template $(get-template $TemplateName) -ToVM

Convert VM to Template – without changing the name.


$VMtoTemplate = "vm_name"
$vm = Get-VM $VMtoTemplate | Get-View
$vm.MarkAsTemplate()

Deploying a VM from template.


$strNewVMName = "NewVM_01"
$strTemplate = "TemplateName"
$strDestinationHost = "ESX01"
New-VM -Name $strNewVMName -Template $(get-template   $strTemplate) 
   -VMHost $(Get-VMHost $strDestinationHost)

Deploying a VM from template using a Customization Specification and using Thin provisioning.
Make sure the CustomSpec has been created beforehand.


$strNewVMName = "NewVM_01"
$strTemplate = "TemplateName"
$strDestinationHost = "ESX01"
$strCustomSpec = "TEST-CustomSpec"
New-VM -Name $strNewVMName -Template $(get-template $strTemplate) 
   -VMHost $(Get-VMHost $strDestinationHost) -DiskStorageFormat 
   Thin -OSCustomizationSpec $(Get-OSCustomizationSpec $strCustomSpec)

Moving a VM to a specific folder.


$strDistinationFolder = "MyFolder"
$strDatacenter = "Training"
$VMToMove = "MyVM"
move-vm -VM $(get-vm $VMToMove) -Destination $(Get-Folder 
    -Name $strDistinationFolder -Location $(Get-Datacenter $strDatacenter))

Copying a file to a Windows VM (With or without network access)
Requires VMware tools to be running.


$VM = get-vm -name "myVM"
$target = "C:\MY_DIR\"
$source = "C:\MY_DIR\test.txt"
Copy-VMGuestFile -Source $source -Destination $target -vm $VM 
   -LocalToGuest -HostUser "root" -HostPassword "password" 
   -GuestUser "myVM\administrator" -GuestPassword "password" 
   -Force:$true

Copying a file from a Windows VM (With or without network access)
Requires VMware tools to be running.


$VM = get-vm -name "myVM"
$target = "C:\MY_DIR\"
$source = "C:\MY_DIR\test.txt"
Copy-VMGuestFile -Source $source -Destination $target -vm $VM 
   -GuestToLocal -HostUser "root" -HostPassword "password" 
   -GuestUser "myVM\administrator" -GuestPassword "password" 
   -Force:$true

Listing the content of “C:\Windows\System32” from a VM – remotely


$VM = get-vm -name "myVM"
Invoke-VMScript -VM $VM -ScriptText "dir" -HostUser "root" 
   -HostPassword "password" -GuestUser "myVM\administrator" 
   -GuestPassword "password"

Run msinfo32 on a guest VM and pipe the output to a TXT file – Using PowerShell.


$VM = get-vm -name "myVM"
$script = '&"$env:ProgramFiles\Common Files\Microsoft Shared\
   MSInfo\msinfo32.exe" /report "$env:Tmp\inforeport.txt"'
Invoke-VMScript -VM $VM -ScriptText $script -HostUser "root" 
   -HostPassword "password" -GuestUser "myVM\administrator" 
   -GuestPassword "password"

Open the above output file in the guest VM – Using PowerShell.


$VM = get-vm -name "myVM"
$script = '&"notepad.exe" "$env:Tmp\inforeport.txt"'
Invoke-VMScript -VM $VM -ScriptText $script -HostUser "root" 
   -HostPassword "password" -GuestUser "myVM\administrator" 
   -GuestPassword "password" -ScriptType Powershell

Run msinfo32 on a guest VM and pipe the output to a TXT file – Using batch commands.


$VM = get-vm -name "myVM"
$script = '&"%programfiles%\Common Files\Microsoft Shared\
   MSInfo\msinfo32.exe" /report "%tmp%\inforeport.txt"'
Invoke-VMScript -VM $VM -ScriptText $script -HostUser "root" 
   -HostPassword "password" -GuestUser "myVM\administrator" 
   -GuestPassword "password" -ScriptType Bat

Open the above output file in the guest VM – Using batch commands.


$VM = get-vm -name "myVM"
$script = '"notepad.exe"   "%Tmp%\inforeport.txt"'
Invoke-VMScript -VM $VM -ScriptText $script -HostUser "root" 
   -HostPassword "password" -GuestUser "myVM\administrator" 
   -GuestPassword "password" -ScriptType Bat