Book: Managing VI with Powershell


image

You can pre-order your book now: http://www.scriptingoutpost.com/

VMware is the king when it comes to virtualization. Windows PowerShell has gotten huge accolades as an extremely powerful tool for automation of IT tasks. These two technologies make a lot of sense together, and VMware is now making that happen with the release of their VI Toolkit for Windows.

In "Managing VMware Infrastructure with PowerShell", you will learn how to perform everything from simple ad-hoc reporting at the command-line ("are any of my virtual machines powered off?") to complex scripts to automate a massive deployment of hundreds of virtual machines. Simple, yet powerful; concise, yet robust; you will enjoy using this new language to solve your old problems using less code than you thought possible.

If you are a system administrator responsible for managing a VMware Virtual Infrastructure (version 2.0 or above), or a standalone ESX Server (version 3.0 or above), then you need this book. Aimed at scripters of every level, the book starts off with a PowerShell primer and continues well into the internals of virtualization on the VMware platform.

Table of Contents
  • Introduction – Why You Need This Book
  1. Windows PowerShell Crash Course
  2. Getting Started
  3. Scripting with Virtual Infrastructure
  4. Inventory and Reporting
  5. Deployment and Configuration
  6. Maintenance and Operations
  7. Troubleshooting and Problem Resolution
  8. Managing VMWare with PowerGUI

VI Toolkit 1.5 + VUM = Error


image

I was experimenting with the VUM Powershell library. I ran the Get-Baseline cmdlet and then the following error occurred:

image

After some digging around at goolge and vmware.com. I found that  the VUM library doesn’t work with the new version of the VI Toolkit. VMware is aware of the problem according to the post from Carter on VMTN and the Release notes:

Carter Shanklin posted this information on VMTN:

Hi everyone,

There is an incompatibility between VI Toolkit 1.5 and the VUM cmdlets. For now if you need to use the VUM cmdlets you will need to do so on a system that has VITK 1.0 installed.
This incompatibility will be resolved by a minor update to VI Toolkit that will be shipped when the next version of VUM ships. The next version of VUM will have cmdlets that support that version as well as the current version.

We’ve updated the release notes to note the incompatibility. Sorry for the inconvenience.

From the release notes:

VI Toolkit (for Windows) 1.5 is not compatible with VMware Update Manager – PowerShell Library 1.0.

windowstoolkit15-200901-releasenotes.html

To fix this “problem” I removed the VI Toolkit v 1.5 and installed the VI Toolkit v 1 again. After the rollback, the VUM library is working again :-).

I hope that VMware will fix this soon.

VI-Toolkit: Enable PXE boot for all VM’s in a particular folder


The following script enables PXE boot for al the VM’s in a particular folder. After running this script, the VM’s are set to boot only from PXE, so all other options are disabled. The original part of the script is created by LucD and posted on de VMTN forums. See the source for more information.

enable-pxeboot-vm.ps1

$VC = Connect-VIServer (Read-Host "Enter vCenter server") 

$folder = (Read-Host "Enter folder name") 

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.extraConfig += New-Object VMware.Vim.OptionValue
$spec.extraConfig[0].key = "bios.bootDeviceClasses"
$spec.extraConfig[0].value = "allow:net"

    $vms = Get-Folder -Name $folder | Get-VM
        foreach ($vm in $vms){
            (get-view ($vm).ID).ReconfigVM_Task($spec)}

Disconnect-VIServer -Confirm:$False

The script in action:image

If you want to return to the default settings, you can run the next script:

disable-pxeboot-vm.ps1

$VC = Connect-VIServer (Read-Host "Enter vCenter server") 

$folder = (Read-Host "Enter folder name") 

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.extraConfig += New-Object VMware.Vim.OptionValue
$spec.extraConfig[0].key = "bios.bootDeviceClasses"
$spec.extraConfig[0].value = "allow:hd,cd,fd"

    $vms = Get-Folder -Name $folder | Get-VM
        foreach ($vm in $vms){
            (get-view ($vm).ID).ReconfigVM_Task($spec)}

Disconnect-VIServer -Confirm:$False

Source: LucD @ http://communities.vmware.com/thread/196651

Free E-Book: Effective Powershell


Keith Hill has released a free E-Book about Powershell.

Introduction
I am a big fan of the “Effective” series of programming books from Effective COM to Effective XML. Without trying to be too presumptuous, I wanted to capture some of the tidbits I have picked up over the last couple of years using Windows PowerShell interactively and writing production build and test scripts. These items were written for PowerShell 1.0. Where appropriate I have added PowerShell 2.0 Update sections to discuss how the item is affected by the upcoming 2.0 release. As a final note, a number of the PowerShell code snippets shown use functionality from the PowerShell Community Extensions which can be downloaded from http://www.codeplex.com/PowerShellCX.

Grab your copy here: http://keithhill.spaces.live.com/blog/

Powershell: Check if KB patch is installed


Ik wilde weten of een bepaald KB nummer was geïnstalleerd op mijn VM’s. Dit wilde ik natuurlijk niet met het handje nakijken en heb hiervoor een script bij elkaar geraapt.

Het onderstaande script kun je gebruiken in een VMware VI omgeving.

#Add-PSSnapIn VMware.VimAutomation.Core

# Connect to vCenter
$VC = Connect-VIServer (Read-Host "Enter vCenter server")

# Enter the KB#
$HotFixID = (Read-Host "Enter KB#")
Write-Host

# Get all VM’s with powerstate = PoweredOn
$VMS = Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Sort Name

ForEach ($VM in $VMS)

{
# Process only Windows Server VM’s
if ($VM.Guest.OSFullName -match "Microsoft Windows Server*")
  { 
 
# Get all the info using WMI
  $results = get-wmiobject -class "Win32_QuickFixEngineering" -namespace "root\CIMV2" -ComputerName $VM 
  
    # Loop through $results and look for a match then output to screen
    foreach ($objItem in $results)
            {
                # Do the match
                if ($objItem.HotFixID -match $HotFixID)
                    {
                    write-host $objItem.CSName
                    write-host "Hotfix "$HotFixID" installed"
                    write-host
                    }
            } 
   }
}
# Disconnect from vCenter
Disconnect-VIServer -Confirm:$False

 

Zodra je het script start, word er gevraagd naar de vCenter server en het KB nummer wat je wilt nakijken.

image

De volgende output wordt gegenereerd:

image

 

Je kunt het onderstaande script gebruiken in een omgeving zonder VI3:

# Get content
$computers = get-content c:\computers.txt

# Get all the info using WMI
$results = get-wmiobject -class “Win32_QuickFixEngineering” -namespace “root\CIMV2″ -computername $computers

# Loop through $results and look for a match then output to screen
foreach ($objItem in $results)
{
    if ($objItem.HotFixID -match “KB932168″)
    {
        write-host $objItem.CSName
        write-host “Hotfix KB932168 installed”
        write-host
    }
}

 

Source: http://techittome.wordpress.com/2007/04/12/windows-powershell-script-to-check-for-specific-hotfix/

VMware: vDiagram script by Alan Renouf


Alan Renouf created a new powershell / vi toolkit script which draws a nice diagram of your VI environment.

Instructions:

You will need:

  • Powershell V1 (I haven’t tried it on V2 yet)
  • The VI Toolkit
  • Microsoft Visio (I have only tried 2003 +)

How to use:

  1. Download the zip file from the bottom of this page
  2. Once extracted copy the  ‘My-VI-Shapes.vss’ file to your ‘My Documents\My Shapes’ folder.  If the folder does not exist create it and copy the file in.
  3. Run the powershell script with the following options:

To diagram the entire Infrastructure:

vDiagram.ps1 -VIServer MYVISERVER

To diagram a specific cluster use the following:

vDiagram.ps1 -VIServer MYVISERVER -Cluster "Production Cluster’

image

More info and the script can be found over here: http://teckinfo.blogspot.com/2009/01/vdiagram-document-your-vi-with-one.html

Alan keep up the good work!

Source:

image

Script: Set-VMVideoAccLevel


Hugo Peeters van www.peetersonline.nl heeft een powershell script gemaakt voor het instellen van de video hardware acceleration level. Zoals je weet staat dit bij een standaard installatie van Windows Server 2003 (en ook andere versies) standaard op none.

image

Via het script van Hugo kun je in een keer deze setting aanpassen op alle VM’s (Windows 2003 servers en deze moeten wel aan staan). Zie de onderstaande quote van Hugo:

I have created a simple, fire-and-forget script that sets the Hardware Video Acceleration Level for all your (Windows Server 2003 and Powered On) VM’s to Full. This way you can always enjoy smooth mouse movements when working in the console!

Het script kun je hier downloaden: http://www.peetersonline.nl/index.php/vmware/setting-video-hardware-acceleration-level/

VI Toolkit: Set VMware Tools Upgrade Policy


 

Tijdens het googlen naar informatie over de VI Toolkit kwam ik het onderstaande script tegen.

Connect-VIServer <VirtualCenterServer>
#  NAME: set-VMautoUpdate.ps1
#  AUTHOR: angoletti1 / LucD
#  SOURCE:  http://communities.vmware.com/message/937094#937094

[Reflection.Assembly]::LoadWithPartialName("vmware.vim")

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"

Get-VM | % { (Get-View $_.ID).ReconfigVM($vmConfigSpec)}

 

Via dit script kun je in één keer alle VM’s aanpassen.

Bron: http://www.blkmtn.org/the-value-of-digging-through-an-SDK

VI Toolkit: Create an Overview Of All cmdlets


 

Het volgende commando genereerd een html file met daarina een overzicht van alle VI cmdlets.

Get-Command -PSSnapin VMware.VimAutomation.Core | ConvertTo-Html –Property CommandType,Name,Definition -title "VIToolkit" > D:\vitoolkit_cmdlets.html

Voer deze code wel  uit als een oneliner!

 

Het volgende overzicht wordt gegenereerd:

image

Nu heb je in een oogopslag een overzicht van alle cmdlets.