Hi Everyone
I am trying to use invoke-vmscript to get the IP address and DNS server addresses of multiple VM's. I am trying to use "netsh interface ipv4 show config" but i only want the IP address / gateway and DNS1 and DNS2 and want to output these to a csv file- i want to filter out the rest of the info.
is this possible ?
I didn't notice, but the back-ticks were converted to single quotes (in the TrimEnd method).
Attached a version that is working for me.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
One way to do that would be to use a RegEx on the output of the netsh command, to extract the IP address.
An easier method is to use a PowerShell cmdlet and only return back the IP address(es).
Something like this for example
Get-WmiObject Win32_NetworkAdapterConfiguration |
Where{$_.IPAddress} |
select -ExpandProperty IPAddress
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks
Changed this slightly to
$wmi = get-wmiobject win32_networkadapterconfiguration -filter "ipenabled = 'true'";
$wmi.ipaddress
$wmi.dnsserversearchorder
This works ok but prints the Ipv6 address also, any idea how to cut this out ? And redirect to a file on the source vm ?
That can be obtained with the same Get-WmiObject.
There are many ways to get that info back, the following is just one example.
Note that this code for example does NOT take into account any exceptions.
For example a host might have no, or only 1 DNS server defined.
Nor does the code take into account that a host might have more than 1 IP address.
Once you have the output as an object, it shouldn't be too hard to do this in a loop, and send the result to an output file.
For example with Export-Csv
$code = @'
$net = Get-WmiObject Win32_NetworkAdapterConfiguration
"{0}|{1}|{2}" -f @(($net | Where{$_.IPAddress} | Select -ExpandProperty IPAddress),
($net | where{$_.DNSServerSearchOrder} | select -ExpandProperty DNSServerSearchOrder)[0],
($net | where{$_.DNSServerSearchOrder} | select -ExpandProperty DNSServerSearchOrder)[1])
'@
$result = Invoke-VMScript -VM MyVM -ScriptText $code -ScriptType Powershell | Select -ExpandProperty ScriptOutput
$resultArray = $result.Split('|')
New-Object PSObject -Property @{
IP = $resultArray[0]
DNS1 = $resultArray[1]
DNS2 = $resultArray[2]
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Eliminating the IPv6 addresses shouldn't be too hard with a Where-clause.
For example, only allow IP addresses that do not have '::' in them.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks very much
One last question . I need to get the VM name also . I am trying to use get-wmiobject win32_computersystem | select-object -expandproperty name
I am trying to run that inside the script block and add it to the array also but its just coming out blank
Do i need to create another array an join them ?
But you have the VM already before you are calling Invoke-VMScript, or not?
You can even get the guest OS hostname without calling Invoke-VMScript.
Or am I missing something?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
well i am currently running it against one VM so yeah i have the VM name hard coded but i need to run this against a resource pool for example and find the following
VM name , IP , Gateway ,DNS1 and DNS2
Apologies for not giving the full info at the start
You could do something like this
$code = @'
$net = Get-WmiObject Win32_NetworkAdapterConfiguration
"{0}|{1}|{2}" -f @(($net | Where{$_.IPAddress} | Select -ExpandProperty IPAddress),
($net | where{$_.DNSServerSearchOrder} | select -ExpandProperty DNSServerSearchOrder)[0],
($net | where{$_.DNSServerSearchOrder} | select -ExpandProperty DNSServerSearchOrder)[1])
'@
$rpName = 'MyPool'
$vms = Get-ResourcePool -Name $rpName | Get-VM | where{$_.PowerState -eq 'PoweredOn'}
$report = foreach($vm in $vms){
$result = Invoke-VMScript -VM $vm -ScriptText $code -ScriptType Powershell | Select -ExpandProperty ScriptOutput
$resultArray = $result.Split('|')
New-Object PSObject -Property @{
VM = $vm.Name
IP = $resultArray[0]
DNS1 = $resultArray[1]
DNS2 = $resultArray[2]
}
}
$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Awesome
WHen i run it like that i get "system object " instead of the IP so i changed it to this
$net = Get-WmiObject Win32_NetworkAdapterConfiguration
"{0}|{1}|{2}" -f @(($net | Where{$_.IPAddress} | Select -ExpandProperty IPAddress[0]),
($net | where{$_.DNSServerSearchOrder} | select -ExpandProperty DNSServerSearchOrder)[1],
($net | where{$_.DNSServerSearchOrder} | select -ExpandProperty DNSServerSearchOrder)[2])
'@
But then i get the IP but not the second dns server 🙂
Ah, there is a <CR><LF> at the end of the returned data, we have to remove that.
Try like this
$code = @'
$net = Get-WmiObject Win32_NetworkAdapterConfiguration
"{0}|{1}|{2}" -f @(($net | Where{$_.IPAddress} | Select -ExpandProperty IPAddress),
($net | where{$_.DNSServerSearchOrder} | select -ExpandProperty DNSServerSearchOrder)[0],
($net | where{$_.DNSServerSearchOrder} | select -ExpandProperty DNSServerSearchOrder)[1])
'@
$rpName = 'MyPool'
$vms = Get-ResourcePool -Name $rpName | Get-VM | where{$_.PowerState -eq 'PoweredOn'}
$report = foreach($vm in $vms){
$result = Invoke-VMScript -VM $vm -ScriptText $code -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 .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
still getting system.object[] for IP address
What exactly is Get-WmiObject Win32_NetworkAdapterConfiguration returning on that host?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Its returning a lot of stuff. Every adapter information
The IP address is in there also
When i change the array values to what i mentioned above and put [0] at end of IP address then i get the IP address in the output but only DNS1 and not DNS2
From what you are saying it looks as if more than 1 IP address is returned.
Does this return more than 1 line?
$code = @'
Get-WmiObject Win32_NetworkAdapterConfiguration |
Where{$_.IPAddress} | Select -ExpandProperty IPAddress
'@
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yep . it returns
192.168.100.115
ipv6 address
Try filtering out the IPv6 addresses
$code = @'
$net = Get-WmiObject Win32_NetworkAdapterConfiguration
"{0}|{1}|{2}" -f @(($net | Where{$_.IPAddress -and $_.IPAddress -notmatch ':'} | Select -ExpandProperty IPAddress),
($net | where{$_.DNSServerSearchOrder} | select -ExpandProperty DNSServerSearchOrder)[0],
($net | where{$_.DNSServerSearchOrder} | select -ExpandProperty DNSServerSearchOrder)[1])
'@
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
no change
All i am getting is vmname and DNS1
DNS2 is blank and IP says system.object[]
The Where-clause was in the wrong place it seems.
Try with this one
$code = @'
$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])
'@
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
so this what i have at the moment (just running on one VM for now)
$vm=get-vm -Name "toolstest2 (42e19988-df7f-45a6-8025-9b48da889e0f)"
$shownet = @'
$net = get-wmiobject win32_networkadapterconfiguration
"{0}|{1}|{2}" -f @(($net | where{$_.IPaddress | select -expandproperty IPAddress | where{$_ -notmatch ':'}),
($net | where{$_.dnsserversearchorder} | select -expandproperty dnsserversearchorder)[1],
($net | where{$_.dnsserversearchorder} | select -expandproperty dnsserversearchorder)[2])
'@
$report
$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]
}
This is the output
NS1 IP DNS2 VM
---- -- ---- --
{1} At line:2 char:33... {2}" -f @(($net toolstest2 (42e19988-df7f-45a6-8025-9b...