to string or not to string - 19061B LAB A in Module 10 $OldIpaddress


I’m a MCT and giving Powershell Classes. I’m teaching the Course 10961 for a while now, but I never figured something out, occuring in the LAB from Module 10 (Putting it all together):

in the Lab you get the “Old Ipaddress” via get-DHCPv4ServerLease and the MACAddress of that Server Core instance.

$OldIPaddress = get-DHCPv4Serverlease -Computername LON-DC1 -ScopeID | where-object {$_.ClientID -eq "WA-TH-EV-ER-00"} | select -expandproperty IPAddress | select -expandproperty IPAddressToString

as a result you get the IP adress saved in the Variable $OldIPAddress… if you pipe it in get-member you see its a [System.String]

but nonetheless, you have to “place the $OLDIpaddress variable in double quotationmarks, and assign the resulting string to $OLDIpaddress. This will convert the IP Address to a string Object” (Page 10-9 from the offical MOC Course-Book, Exercise 2, Task2, step number 10)

Alright it works… but I don’t get why you need to do this… because it is already a string!

If you do not do this, Setting the trustedHostsList will fail with “cannot convert System.Object[] to System.String” even though get-Member (or the method gettype()) says that it IS ALREADY a System.String…

Set-Item WSMAN:\localhost\Client\TrustedHosts -Value $OldIPaddress

That really bothers me and I cannot explain this to my Students…

Can anyone help me?

Sorry for bad english… I’m German :wink:

It sounds to me more like it is an array of strings than a string. Get-Member is probably just describing the contents of the array.

What happens if you do $OLDIPAddress[0] with it?

TrustedHosts is a comma-separated string value. If you’ve got an array of IP addresses and want to assign them all to trusted hosts, you can do something like this:

Set-Item WSMAN:\localhost\Client\TrustedHosts -Value ($OldIPAddress -join ',')

The result of

$OldIPaddress = get-DHCPv4Serverlease -Computername LON-DC1 -ScopeID | where-object {$_.ClientID -eq “WA-TH-EV-ER-00”} | select -expandproperty IPAddress | select -expandproperty IPAddressToString

Is a string array, not a string. Using it in double quotes collapses it into a simple string.


thanks for your responses.
I thought of this (beeing an array of strings) too, but I didn’t find a way to show it to my students that $OldIPaddress is an array, before I put it in Quotationmarks…

because if I type:


-> 1
(first digit of

it says the same value (the first digit of the IP) as after the re-assignment of the variable:

$OldIPAddress = "$OldIPAddress"

-> 1

so how can you determine if the variable is an array?

thanks again for your quick reactions.

Couple of options:


Get-Member -InputObject $OldIPAddress

Hey Dave,

thanks for your answer, but I am aware of this technique. Nevertheless this does NOT give me a possibility to distinguishe between an array of strings with one Object and an non-array variable with one Object, in this special case!

PS C:\WINDOWS> $OldIPaddress.gettype()

[i]IsPublic IsSerial Name BaseType

True True String System.Object[/i]

and after:

$OldIPaddress = "$OldIPaddress"

it is the stays the same result:

PS C:\WINDOWS> $OldIPaddress.gettype()

[i]IsPublic IsSerial Name BaseType

True True String System.Object[/i]

same here with:

 Get-Member -InputObject $OldIPaddress

I appreciate your help, but I have no solution so far. :slight_smile:

If $OldIPAddress.GetType() is telling you it’s a string, then you should be able to pass it to WSMan as-is. The problem only comes up when the pipeline starting with “get-DHCPv4Serverlease” returns multiple results, instead of a single record. When that happens, $OldIPAddress will be an Object[] array containing two or more String objects.

If you want to reliably demonstrate this, then you’ll need to make sure you can duplicate the conditions that cause Get-DHCPv4ServerLease to return multiple results.

Thanks for your seggestion.

But I can reproduce the case reliably without Problems. And look:

so I don’t understand whats going on here. :slight_smile:

I appreciate your help and hope we find a solution to this weird stuff :wink:

There’s a big difference between $OldIPAddress.GetType() and (Get-Member -InputObject $OldIPAddress).GetType() . I’m not sure where the latter one came from, in your screenshot.

Sorry, I was accidentially attaching the wrong Picture. I was not sure if I was posting the Picture correctly, so I wanted to to upload the same pic which I also embedded/posted it the text. I will delete the attachment.

Whoa… that is incredibly weird. I’ll see if I can reproduce it.

Dave Wyatt wrote:
Whoa… that is incredibly weird. I'll see if I can reproduce it.

happy to see that other People find it strange too and I not went crazy :slight_smile:

I can recreate this like this:

$OldIPAddress = [ipaddress] "" | select -ExpandProperty IPAddressToString
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $OldIPAddress

IPAddressToString is a ScriptProperty added by the extended type system. Turns out you’ll get the same error if you extend an object yourself:

$Test = "*" | Add-Member -NotePropertyName TestProperty -NotePropertyValue test -PassThru
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $Test  # Fails
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $Test.psobject.BaseObject  # Succeeds

It looks like the WSManConfigProvider really doesn’t like extended objects.

Wow thanks, you made my day. That seems to be the Problem. It is indeed the WSMANConfig-provider. I think this may be a useful information for the publisher of the Powershell-Course MOC 10961. I see to give them a feedback, with this information.

Thanks alot you guys for helping me out. :slight_smile: