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

This topic contains 14 replies, has 5 voices, and was last updated by Profile photo of Sebastian Reitter Sebastian Reitter 1 year, 8 months ago.

  • Author
    Posts
  • #23711
    Profile photo of Sebastian Reitter
    Sebastian Reitter
    Participant

    Hi,

    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 10.0.0.0 | 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 [i]"cannot convert System.Object[] to System.String"[/i] 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 😉

  • #23718
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    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?

  • #23721
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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 ',')
    
  • #23726
    Profile photo of Don Jones
    Don Jones
    Keymaster

    The result of

    $OldIPaddress = get-DHCPv4Serverlease -Computername LON-DC1 -ScopeID 10.0.0.0 | 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.

  • #23736
    Profile photo of Sebastian Reitter
    Sebastian Reitter
    Participant

    Hello,

    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:

    $oldipaddress[0] 

    -> [i]1[/i]
    (first digit of 10.0.0.101)

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

    $OldIPAddress = "$OldIPAddress"
    $OldIPAddress[0]

    -> [i]1[/i]

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

    thanks again for your quick reactions.

  • #23737
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Couple of options:

    $OldIPAddress.GetType()
    
    Get-Member -InputObject $OldIPAddress
    
  • #23806
    Profile photo of Sebastian Reitter
    Sebastian Reitter
    Participant

    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. 🙂

    [img]http://www.shizarium.ru/wp-content/gallery/animals5/34520-114759-ba1d04b3a7e17d10d0a272533ca71316.jpg[/img]

  • #23812
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.

  • #23854
    Profile photo of Sebastian Reitter
    Sebastian Reitter
    Participant

    Thanks for your seggestion.

    But I can reproduce the case reliably without Problems. And look:
    [img]https://social.technet.microsoft.com/Forums/getfile/640488[/img]

    so I don't understand whats going on here. 🙂

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

  • #23856
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.

  • #23861
    Profile photo of Sebastian Reitter
    Sebastian Reitter
    Participant

    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.

  • #23863
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

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

  • #23868
    Profile photo of Sebastian Reitter
    Sebastian Reitter
    Participant

    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 🙂

  • #23919
    Profile photo of Rohn Edwards
    Rohn Edwards
    Participant

    I can recreate this like this:

    $OldIPAddress = [ipaddress] "192.168.0.1" | 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.

  • #24095
    Profile photo of Sebastian Reitter
    Sebastian Reitter
    Participant

    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. 🙂

You must be logged in to reply to this topic.