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

Welcome Forums General PowerShell Q&A 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

 
Participant
3 years, 10 months ago.

  • Author
    Posts
  • #23711

    Participant
    Points: 0
    Rank: Member

    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 "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 😉

  • #23718

    Participant
    Points: 60
    Rank: Member

    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

    Member
    Points: 0
    Rank: Member

    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

    Keymaster
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    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

    Participant
    Points: 0
    Rank: Member

    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] 

    -> 1
    (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]

    -> 1

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

    thanks again for your quick reactions.

  • #23737

    Member
    Points: 0
    Rank: Member

    Couple of options:

    $OldIPAddress.GetType()
    
    Get-Member -InputObject $OldIPAddress
    
  • #23806

    Participant
    Points: 0
    Rank: Member

    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()

    IsPublic IsSerial Name BaseType
    ——– ——– —- ——–
    True True String System.Object

    and after:

    $OldIPaddress = "$OldIPaddress"

    it is the stays the same result:

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

    IsPublic IsSerial Name BaseType
    ——– ——– —- ——–
    True True String System.Object

    same here with:

     Get-Member -InputObject $OldIPaddress

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

  • #23812

    Member
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    Thanks for your seggestion.

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

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

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

  • #23856

    Member
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Member
    Points: 0
    Rank: Member

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

  • #23868

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

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

The topic ‘to string or not to string - 19061B LAB A in Module 10 $OldIpaddress’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort