PowerCLI: Document the ESX Hostname of the vCenter VM


image

I was reading Duncan Epping his post: http://www.yellow-bricks.com/2009/10/09/best-practices-running-vcenter-virtual-vsphere/ about Running vCenter virtual. The most of the steps described, you only have to do once but step 5 needs to be documented once in a while

5. Write a procedure to boot the vCenter / AD / DNS / SQL manually in case of a complete power outage occurs.

Nobody likes to document this thing so we will let PowerCLI do this job for us.

First you need to now the VMs. In most cases this will be your Domain Controller, Database Server and of course the vCenter VM.

$vms =  Get-VM "DC01", "DB01", "VC01" | Sort Name
$vms | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}}, `
@{N="VMHost";E={Get-VMHost -VM $_}} 

The one-liner above will return the VM name, Cluster Name and ESX Host name:

 image

Now you are able to document where your VMs are. But you still need to put this information somewhere. So I created a simple script which will export the information displayed above to a CSV file. The script will also remove files older than 7 days.

You can change the variable if you want.

$now = Get-Date
$days = "7"
$targetFolder = "C:\vCenter"

if (Test-Path $targetFolder)
{
    Write-Host $targetFolder "Already exists"
}
else
{
    New-Item $targetFolder -type directory
    Write-Host $targetFolder "Created"
}

$lastWrite = $now.AddDays(-$days)
$files = get-childitem $targetFolder -include *.csv -recurse `
    | Where {$_.LastWriteTime -le "$lastWrite"} 

if (($files | Measure-Object).count -gt 0){
foreach ($file in $files)
{write-host "Deleting File $File" -foregroundcolor "Red"; `
    Remove-Item $file | out-null}
}

$filename = "C:\vCenter\" + (Get-Date -format  'yyyy-MM-dd hh-mm-ss') + '.csv'
$vms =  Get-VM "DC01", "DB01", "VC01" | Sort Name 
$vms | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}}, `
@{N="VMHost";E={Get-VMHost -VM $_}} | `
Export-Csv -NoTypeInformation $filename

The script will generate a CSV file:

image

The CSV file will look like this:

"Name","Cluster","VMHost"

"DB01","Cluster_01","esx1.ict-freak.local"

"DC01","Cluster_01","esx1.ict-freak.local"

"VC01","Cluster_01","esx1.ict-freak.local"

You can schedule this script on a VM that runs on another cluster or maybe better, schedule the script on a physical box. If you want to know how to schedule a Powershell/CLI script, go check out this post from Alan Renouf: http://www.virtu-al.net/2009/07/10/running-a-powercli-scheduled-task/

Now you are able to track the most important VMs in your environment.

Advertisements

3 thoughts on “PowerCLI: Document the ESX Hostname of the vCenter VM

  1. Pingback: Tweets die vermelden PowerCLI: Document the ESX Hostname of the vCenter VM « ICT-Freak.nl -- Topsy.com

  2. LucD

    Great script Arne.
    You could also trigger the script with an event alarm the moment one of your critical guests are vMotioned.

    Reply
  3. Rudolf Kleijwegt

    Concider the following alternative to exporting a list of important VMs: directly connect to multiple ESX hosts simultaneously. Starting PowerCLI 4.0 U1 you can now register more than one vCenter or ESX server session.

    Check out the following example from get-admin (http://get-admin.com/blog/?p=923):

    # Set PowerCLI to multiple
    Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false
    # Connect to every vSphere host in the cluster that contains vCenter
    Connect-VIServer -User root -Password password -Server esx1,esx2,esx3,esx4,esx5,esx6,esx7,esx8
    # Start vCenter
    Get-VM vCenter01 | Start-VM

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s