Powershell Script – Report all VM hosts and IP’s

December 7, 2009 — 1 Comment

Whilst I am currently learning about Powershell and writing a few scripts I thought I would blog my scripts here. Mainly for my benefit so I can easily find them and maybe some will be of use to someone out there. Also if you look at any of my scripts and cringe that something isn’t right or could be done better please let me know.

First a bit of a disclaimer, I find it easiest to learn by starting out with a script that I know that works and changing it to suit what I am doing. As such a number of my scripts may contain elements that are not my own work.  I will try and credit all third parties where possible, if I have missed you and I am clearly using your code please let me know and I will be happy to credit you.

Finally please remember I am a beginner so before using any scripts please test on a test system first.

The script below will create a very simple HTML Hostname and IP list at c:\IPReport.html which maybe useful when documenting a system. Elements of this script particularly the connection to the VIServer come from scripts found on Alan Renouf’s site www.virtu-al.net I highly recommend his site if you are learning powershell or just looking for scripts to help you with your daily admin tasks.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
param( [string] $VIServer )
#Check for VIServer name and give a friendly error
if ($VIServer -eq ""){
Write-Host
Write-Host "Please specify a vCenter Server name eg…."
Write-Host " powershell.exe VMGuestIPReport.ps1 MYVISERVER "
Write-Host
Write-Host
exit
}
#HTML header for output file
$head = ‘<style> BODY{font-family:Verdana; background-color:white;} TABLE{border-width: 1px;border-style: groove;border-color: black;border-collapse: collapse;} TH{font-size:0.8em; border-width: 1px;padding: 2px;border-style: groove;border-color: black;background-color:darkgrey} TD{font-size:0.8sem; border-width: 1px;padding: 2px;border-style: groove;border-color: black;background-color:lightgrey}</style>’

#Connect to vCenter

Write-Host "Connecting to VI Server"
Connect-VIServer $VIServer

# Get VM Name and IP Addressess

$header = "<H3>VM Guest – IP Report</H3>"
Get-VM | select name, @{ Name = "IP Addresses"; Expression = { $_.Guest.IPAddress }} | Sort-Object name | convertto-html -head $head | out-file c:\IPReport.html

Example output

image

One response to Powershell Script – Report all VM hosts and IP’s

  1. 

    Welcome to PowerCLI, you have taken the first step towards enlightenment 🙂 – Thanks for the plug !

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