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 think you missed a curly brace in the code (after the where{$_.IPAddress)
$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])
'@
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No, we have to force the DnsServerSearchOrder property to be an array.
Try like this
$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])
'@
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks. havent had a chance to try this yet. WIll revert once i do
ok so now this is what i have
$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)
[0],
@($net | where{$_.dnsserversearchorder} | select -expandproperty dnsserversearchorder)
[1])
'@
$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]
}
And this is the output
DNS1 IP DNS2 vm
---- -- ---- --
At line:4 char:3... toolstest2 (42e19988-df7f-45a6-8025-9b4
Can you attach the script you are using as a file?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That's what I suspected, your copy/paste is incorrect.
Find the script as I intended it attached.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Copied and ran that script and still getting errors..Here is the output running on two different VM's
PS C:\Users\> E:\getdns\test.ps1
DNS1 IP DNS2 vm
---- -- ---- --
At line:4 char:92... toolstest2
PS C:\Users\> E:\getdns\test.ps1
WARNING: The version of VMware Tools on VM 'dtmat11-testa1 (e59ff307-ba94-4742-91ff-842fddb6d2fc)' is out of date and may cause Invoke-VMScript to work improperly.
DNS1 IP DNS2 vm
---- -- ---- --
where{$_.dnsserversearchorder} Unexpected token '1' in expression or statement.... select -expandproperty dnsserve...
I know this is dragging on but its very close and i would love to get it working !
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
Awesome - thanks very much .
Works perfect