A. Mikkelsen VMware ESX scripts, commands, tools and other nice to know things that will make your virtualization days easier!!!!

Sunday, January 31, 2010

Playing with PowerCLI

Filed under: PowerShell — Tags: , , , , , , , — A. Mikkelsen @ 21:45 pm

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

9 Comments »

  1. Hi, glad you’re enjoying PowerCLI.

    My preferred way of loading a snapin that may already be loaded is as follows:

    Add-PSSnapin VMware.VimAutomation.Core -ea SilentlyContinue

    Comment by Carter Shanklin — Monday, February 1, 2010 @ 1:18 am

  2. thanks, very useful scripts

    Comment by michael — Monday, February 1, 2010 @ 13:06 pm

  3. Hi Michael,
    Glad you can use them.
    A. Mikkelsen

    Comment by A. Mikkelsen — Monday, February 1, 2010 @ 17:31 pm

  4. Hi Carter,
    I’m loving PowerShell.
    I find it a lot easier to work with than the Perl VI Toolkit.

    I also like your way, but I don’t like to load thinks that are already loaded 🙂
    The function also shows how to check if a PSSnapin is Loaded or not, and it also shows the basic on how to construct a simple function.

    Thanks for you input.

    A. Mikkelsen

    Comment by A. Mikkelsen — Monday, February 1, 2010 @ 17:36 pm

  5. Michael – great to hear you enjoy PowerCLI…What did you think of the course ?

    Regards,
    Pablo Roesch
    VMware
    vSphere SDK / API Product Marketing

    ps. We are running a contest for best ESXi Script (Script-O-Mania) – Chance to win $2500.00 for first prize…

    Comment by Pablo — Monday, February 1, 2010 @ 21:23 pm

  6. Some nice examples here, I didnt know there was an official course for PowerCLI, where was this based ?

    Thanks

    Alan

    Comment by Alan Renouf — Tuesday, February 2, 2010 @ 0:29 am

  7. Hi Pablo (Sorry for the late reply – vacation :-)),
    I thought that the cource was OK for a user starting out using Powershell to administrate his/hers VMware enviromment.
    But for an user experienced in using PS to manage his/hers VMware enviromment the cource lacked a bit.

    This said I still got sometihing out of it.
    I had time to play with the new CMDLETS and test a few things.

    A. Mikkelsen

    Comment by A. Mikkelsen — Monday, February 8, 2010 @ 8:35 am

  8. Thanks,
    I’ll add some more soon.
    The cource was not a official VMware course, but one created by an instructor from Swicherland.
    I think that Magirus also offeres this course in other countries.

    A. Mikkelsen

    Comment by A. Mikkelsen — Monday, February 8, 2010 @ 8:43 am

  9. […] Some good examples courtesy of A. Mikkelson […]

    Pingback by www.vExperienced.co.uk » VCAP study notes – section 8.1, PowerCLI — Wednesday, October 6, 2010 @ 21:06 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress