Script to get VM information

Recently i wanted to get the following information for VM’s in my environment :

  1. VM Name
  2. IP Address
  3. DNS1
  4. DNS2

The script below helped me to get that information by requesting a resource pool name and entering the location where you want to resulting csv file to be saved to .

The script uses the invoke-vmscript cmdlet and a powershell block to get the required information and then saves the information neatly in a csv file.

$rp = Read-Host -Prompt “What Resource Pool do you want to gather information for”
$csv = Read-Host -Prompt “Please give the full path where you want the csv file to be saved”

$vms = get-resourcepool -Name $rp | get-vm | where {$_.Powerstate -eq ‘Poweredon’}

$shownet = @’
$net = get-wmiobject win32_networkadapterconfiguration
“{0}|{1}|{2}” -f @(($net | where{$_.IPaddress} | select -expandproperty IPaddress | where{$_ -notmatch ‘:’}),
@($net | where{$_.dnsserversearchorder} | select -expandproperty dnsserversearchorder)[0],
@($net | where{$_.dnsserversearchorder} | select -expandproperty dnsserversearchorder)[1])
‘@

$report = foreach ($vm in $vms){

$result = invoke-vmscript -vm $vm -ScriptText $shownet -ScriptType powershell | select -ExpandProperty scriptoutput
$resultarray = $result.Trimend(“`r`n”).Split(‘|’)
new-object PSObject -Property @{
vm = $vm.name
IP = $resultarray[0]
DNS1 = $resultarray[1]
DNS2 = $resultarray[2]
}

}

$report | export-csv -Path $csv\report.csv -NoTypeInformation -UseCulture

 

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s