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 = $
IP = $resultarray[0]
DNS1 = $resultarray[1]
DNS2 = $resultarray[2]


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