VMware Cloud Community
scriptermad
Enthusiast
Enthusiast
Jump to solution

Invoke-vmscript (netsh)

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 ?

Reply
0 Kudos
30 Replies
LucD
Leadership
Leadership
Jump to solution

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

scriptermad
Enthusiast
Enthusiast
Jump to solution

Thanks .That works a treat on the VM i am testing with

I then ran it against another VM (same OS and version of tools) which has only one DNS server and it outputted this - is this expected when only one DNS ?

DNS1                                                             IP                                                               DNS2                                                            vm                                                            

----                                                             --                                                               ----                                                            --                                                            

1                                                                192.168.111.12                                                   7...                                                            Correct-name   

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
scriptermad
Enthusiast
Enthusiast
Jump to solution

Thanks. havent had a chance to try this yet. WIll revert once i do

Reply
0 Kudos
scriptermad
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you attach the script you are using as a file?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
scriptermad
Enthusiast
Enthusiast
Jump to solution

Here you go, script attached

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
scriptermad
Enthusiast
Enthusiast
Jump to solution

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 !                            

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
scriptermad
Enthusiast
Enthusiast
Jump to solution

Awesome - thanks very much .

Works perfect

Reply
0 Kudos