Category Archives: Uncategorized

PowerCLI v5.1 Guest VM Export script

Just a short post this one, as I noticed that a few of the PowerCLI scripts that I had written originally to run on 4.1.0 ESXi hosts, had stopped working as expected once I had copied them over to a new 5.1 environment.

What seemed to happen was that the script was no longer reading through every ESXi host that I specified with Connect-VIServer, so I rewrote the connect portion of the code to now connect to my vCentre server and then issue “Get-VMHost” and then loop through each ESXi host that vCentre knew about.

At the end the code pushes the report output into a remote subversion repo, which I have HTTP,davSVN,etc running on, this means I can go back in time and look at what stuff looked like yesterday for a given script output.

I have also run:
“svn propset svn:mime-type text/html Matt_local_VMInfo.html” on the remote server (from within my working copy there), to set the MIME types for the files, so that the file will be properly interpreted by my browser.

Anyway, heres the modified code, hope that someone finds it useful!

Add-PSSnapin VMware.VimAutomation.Core
Remove-Item "C:workingcopyMatt_local_VMInfo.html" -Force -ErrorAction SilentlyContinue
disconnect-VIServer * -Confirm:$false
$vcentreserver="vcentre serverIP"

connect-VIServer $vcentreserver -User "username" -Password 
$servers = Get-VMHost| Select $_.Name
$report = @()
ForEach ($svr in $servers){
connect-VIServer $svr -User "user" -Password "password"

$vms = get-vm
ForEach($vm in $vms)
{
$view_datastore=$vm|get-datastore
$view_vmHost=$vm|get-vmhost
$view_HDD=$vm|get-harddisk
$vm_view=$vm|get-view
$row = "" | Select Name, Status, VMHost, CPU, Memory, Notes, Datastore,HDD_Name,HDD_Size
    $row.Name = $vm_view.Name
    $row.Status = $vm_view.Summary.OverallStatus
    $row.VMHost = $view_vmHost.Name
    $row.CPU = $vm_view.Summary.QuickStats.OverallCpuUsage
    $row.Memory = $vm_view.Summary.QuickStats.HostMemoryUsage
    $row.Notes = $vm.Notes
    $row.Datastore = $view_datastore
    $row.HDD_Name = $view_HDD
    $row.HDD_Size = $view_HDD.CapacityKB
    $report += $row   
}
$myStyle = @"
ESXi Infrastructure Report

"@
$report| ConvertTo-Html -Body $body -Head $myStyle|Set-Content "C:workingcopyMatt_local_VMInfo.html"

$svnprogA = "svn.exe"
$svnArgumentsA = @(
'update'
'--username "svnusername"'
'--password "password"'
'c:workingcopy'
)
Set-executionPolicy RemoteSigned
start-process $svnprogA -ArgumentList $svnArgumentsA

$svnprog = "svn.exe"
$svnArguments = @(
'commit -m "committed Infrastructure Estate List from Subversion for Windows"'
'--username "svnusername"'
'--password "password"'
'c:workingcopy'
)
}
$servers = Get-VMHost| Select $_.Name
$report = @()
ForEach ($svr in $servers){
disconnect-VIServer $svr -Confirm:$false
} 

start-process $svnprog -ArgumentList $svnArguments

There’s a few entries at the end of the script which I use to push my report changes into Subversion, but this can obviously be removed and perhaps a function inserted to have the report emailed instead.

(c) Matt Palmer – 21 Jan 2014