vCenter and Host Build Numbers
This is based on Carter Shanklin’s PowerShell snippets to query VC and ESX build version numbers.
This script loops through the list of vCenter servers, and gets their version and build info, as well as the version and build info for it’s connected hosts.
# Script to connect to a list of vCenter Servers, and get their version numbers, as well as the version numbers of their hosts # Ben Neise # 02/10/09 # Array of vCenter Servers $arrVCenterServers = @("server1","server2","server3") # Create empty arrays for the results $arrTableVCs = @() $arrTableHosts = @() # Loop through the array of vCenter servers specified above ForEach ($strVCenterServer in $arrVCenterServers){ # Connect to the VC $objVCenterServer = Connect-VIServer $strVCenterServer # Version info about the VC you are connected to $viewVCenterServer = Get-View serviceinstance # Add custom attributes to each VC objects for version and build $objVCenterServer | Add-Member -Name Version -type noteproperty -value ($viewVCenterServer.content.about.Version) -Force $objVCenterServer | Add-Member -Name Build -type noteproperty -value ($viewVCenterServer.content.about.Build) -Force # Add the VC object to the results array $arrTableVCs += $objVCenterServer # When connected to loop through the hosts managed by the VC ForEach ($objHost in (Get-VMhost | Sort-Object)){ # Get the view for the current host $viewHost = $objHost | Get-View # Add custom attributes to the host object for VC server, Host and Version $objHost | Add-Member -Name VCServer -type noteproperty -value $objVCenterServer.Name -Force $objHost | Add-Member -Name Host -type noteproperty -value $viewHost.Name -Force $objHost | Add-Member -Name Version -type noteproperty -value $viewHost.Config.Product.Version -Force # Add the host object to the results array $arrTableHosts += $objHost } # Disconnect from the VC server Disconnect-VIServer -Confirm:$False } # Output the VC results (can be modified to output to a CSV with Export-CSV) $arrTableVCs | Select-Object Name, Version, Build | Sort-Object Name # Output the Host results (can be modified to output to a CSV with Export-CSV) $arrTableHosts | Select-Object VCServer, Host, Version, Build | Format-Table # End of script
PowerShell script to add a hash table full of virtual port groups to vSphere hosts
As part of the migration I’m working on, we needed to add a whole bunch of Virtual Port Groups with associated VLANs to the servers. The following script could do this in a few minutes (although Host Profiles would accomplish much the same thing, we’re not running Enterprise)
# Sets up virtual port groups on all hosts connected to a specific vCenter Server # Name of vCenter Server $strVCenterServer = "your.vCenter.Server" # VLANs and associated VPGs $ArrVLANs = @{ "123" = "Admin"; "456" = "GPO"; "789" = "NAG"; } # Connect to the vCenter Server Connect-VIserver $strVCenterServer # Loop through the VLAN/VPG pairs ForEach($objVLAN in ($ArrVLANs.Keys | Sort-Object)){ # Loop through the hosts ForEach ($objHost in (Get-VMHost | Sort-Object)){ # Create the VPG with the VLAN as specified in the array above, on the switch called "VMSwitch" on the current host # Remove the "-WhatIf" tag from the end of the following line to "arm" the script New-VirtualPortGroup -Name $strNewVPG -VirtualSwitch (Get-Virtualswitch -VMHost $objHost | Where-Object { $_.Name -match "VMswitch" }) -VLanId $strNewVlanTag -WhatIf # Write what we've just done to screen Write-Host "Adding Virtual Port Group" $ArrVLANs[$objVLAN] "with VLAN Tag" $objVLAN "to" $objHost } } # Disconnect the session from the host Disconnect-VIServer -Confirm:$False
Although this isn’t a complicated script, it was the first time I’ve used hash tables (thanks to PowerShell.Com’s excellent page), so I thought I’d share.
Script to add VMX path as a custom attribute
I love it when I have an idea in my head of a script I need to write, and then I run across one that does exactly what I need!
Hugo Peeter’s has written a script to add VMX location as a Custom Attribute:
Add Vmx Path to VI Client using Powershell | PeetersOnline.nl.
Export and import customization profiles using PowerShell
I’m in the middle of preparing for a migration from VI3 to vSphere 4 just now (hence the lack of substantial updating on this site).
As part of this process, I was just about to start writing a script to export our customisation specifications, when Arnim van Lieshout’s post appeared in my VMware Planet V12N RSS feed.
Export and import customization profiles using Powershell | Arnim van Lieshout.
It failed on a couple of customisations, but by adding…
Write-Host Exporting $CustomizationProfile.name
…after the ForEach loop started, it was easy to see that it was customisations with “/” and “*” characters that were causing the errors.
SnapReminder sends e-mails to owners of snapshots
I love this script from Alan Renouf:-
[F]ind the offending snapshot, find the person who created it, get their email address from AD and send them an email reminding them of their mortal sin.
Our IndependentNonPersistent drives here prevent us from using snapshots, but I’m working on a “utilisation checking” script, that should help us cull some neglected machines and I might integrate some of Al’s AD interrogation into that.
VMware Developer CodeCentral
VMware have launched a new community site for code examples, and there’s a section on VMware vSphere PowerCLI (formerly VI Toolkit).
They seem to be picking the best scripts from the regular contributors to the forums, so it’s a good place to have a look for examples. They’re rated by users, and tend to be slightly better documented than the ones posted in the forums (which tend to be appended and amended as the thread progresses).
Xtravirt White Paper on Storage Oversubscription Technologies
Excellent white paper from Xtravirt detailing advantages and disadvantages of linked-clones, thin-provisioning, and data-deduplication in order to pack more VMs onto your storage.
Storage Oversubscription Technologies | Xtravirt (registration required).
Cleaning up Template Names
I wrote this today to remove occurrences of the string “Tmpl” anywhere in the name of the template, and then to re-name the template with “Tmpl” as a prefix. It had a higher purpose than keeping everything nice and neat, but it’s rather specific to our environment so I won’t bother going into the details.
# Get all templates $objTemplates = Get-Template # Loop through the templates ForEach ($objTemplate in $objTemplates){ # Set the $StrInterimTemplateName variable to the template name, replacing the string "Tmpl" with an empty string $StrInterimTemplateName = ($objTemplate.Name -replace("Tmpl","")) # As the string we've just removed might be anywhere in the name, we need to replace double spaces with single $StrInterimTemplateName = ($StrInterimTemplateName -replace(" "," ")) # And also remove trailing spaces from the start, or the end of the string $StrInterimTemplateName = $StrInterimTemplateName.Trim() # Display on screen what we're doing (as the "Set-Template" with -WhatIf isn't very clear Write-Host Changing `[($objTemplate.Name)`] to `[ Tmpl $StrInterimTemplateName `] # Change the Template Name to the $StrInterimTemplateName variable preceeded by "Tmpl", uncomment the #-WhatIf if testing Set-Template -Template $objTemplate -Name "Tmpl $StrInterimTemplateName" #-WhatIf }
Despite being quite specific in it’s current form, this could easily be modified to rename virtual machines (or indeed any other PowerShell object).
While I suspect there’s a more elegant way to do this in fewer steps, it’s not particularly hacky.
VMware PowerCLI 4.0 released
I came back from a week’s holiday this morning to find that VMware PowerCLI 4.0 has been released as the successor to VI Toolkit 1.5. The jump from 1.5 to 4.0 is for version number consolidation, rather than being than indicitive of major changes.
They have however fixed one of the bugs that’s been annoying me, which is the inability to change drives to non-persistent, so I’ll need to revisit some of my old scripts from v1.0 and check that they still work.
You can download it from the community page. I’ll follow up with more information when I get a chance to investigate it fully.
Virtual Machine Blue Screen Detector
Carter Shanklin has used use of Microsoft Office Document Imaging Library (MODI) to improve Eric Sloof’s script to detect Blue Screens of Death (BSoDs); it now captures the errors and converts them to text.
From Eric’s blog:
[F]irst it captures a screenshot of a virtual machine. Secondly it uses the Toolkit Extensions to copy it to the local drive. When the PNG image is saved on the local drive, it’s converted to TIFF. The TIFF image will be used to extract the text using OCR.
It’s the kind of thing that naieve users expect computers to be able to do, but which actually turn out to be rather difficult tasks. It’s an incredible use of the various technologies involved – PowerCLI, the Toolkit Extensions, MODI and PrimalForms.
