We help IT Professionals succeed at work.

Help extracting text from a string

ndalmolin_13
ndalmolin_13 asked
on
Hello Powershell Gurus,

Here is my powershell question for the day.  If I run the following command:

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName fs01 | Select-Object dnsserversearchorder

I get the following output (which is correct):

dnsserversearchorder                                                                                
--------------------                                                                                
{172.16.1.19, 172.16.1.20}  

I ran the get-member on the Get-WmiObject -Class Win32_NetworkAdapterConfiguration command and learned that the dnsserversearchorder property is a string.  How can I extract the two IP addresses from the string?

Thanks in advance.

Regards,
Nick
Comment
Watch Question

Commented:
You can use the Split function if it is a string
$string.Split(",")

Split on the comma and you will split the string into (2) IP addresses

Author

Commented:
How would I do this?

I tried the following:

$dns = Get-Wmiobject -Class Win32_NetworkAdapterConfiguration -ComputerName fs01 | Select-Object dnsserversearchorder
$dns.split(",")

I received an error stating:

Method invocation failed because [System.Object[]] doesn't contain a method named 'split'.
At :line:2 char:10
+ $dns.split <<<< (",")
Commented:
Thats because its not returning a string, its returning a custom object.
So lets skip trying to parse out the results, and make powershell do all that work for us.
The following line will output the following:
Ipaddress1
Ipaddress2
ipaddress....etc

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName fs01 | %{$_.DNSServerSearchOrder}

Open in new window

Commented:
Glad I could help!