In C# I am trying to get the IPAddresses of my computer using the following command (the command works fine in PowerShell and, indeed, shows IP addresses):
In PowerShell:
Get-CimInstance Win32_NetworkAdapterConfiguration -Filter "IPEnabled='True'" -ComputerName $env:ComputerName | Select -Property IPAddress
PowerShell Output:
{1.2.3.4}
{5.6.7.8}
When I turn around and attempt to get the value (stored in results) from the PowerShell command in C# like this:
foreach (PSObject obj in results) {
foreach (PSPropertyInfo objProperties in obj.Properties) {
string pName = objProperties.Name.ToString(); // returns "IPAddress"
**string pValue = objProperties.Value.ToString(); // returns "System.String[]" and not an actual IP address**
}
}
pValue has a value of "System.String[]" and not the actual IP address value. But objProperties.Name successfully returns the key-name "IPAddress".
How can I get the actual IP Address instead of "System.String[]"?
The fix in this scenario, as noted in the comments to the question, is:
((string[])objProperties.Value).First()