Get-Childitem depending on NodeName

This topic contains 4 replies, has 3 voices, and was last updated by  Richard Siddaway 1 month, 1 week ago.

  • Author
    Posts
  • #89096

    TommyQuality
    Participant

    Hi,
    I can't see where I'm going wrong here so i'm hoping someone can point it out for me.
    I have a HashTable of Nodes and Paths, i need to get the Items of a Path depending on the NodeName.

    $Paths = @(
    @{NodeName = "VMNAME"; Instance = "Internal" ; Path = "C:\Temp\"},
    @{NodeName = "AnotherName" ; Instance = "External" ; Path = "C:\Temp2"},
    @{NodeName = "VMNAME2"; Instance = "Internal" ; Path = "C:\Temp1\"}
    )
    
    If ($ENV:Computername -eq "VMNAME1" -or "VMNAME2"){
        Get-ChildItem $Paths.Where{$Paths.NodeName -eq $ENV:Computername}.Path
    }
    
    

    When i run this it gets the items on the path for the matching Nodename, but it also tries to get the items of all the other paths that don't exist on this node.

    Im sure i'm missing something silly, but just can't see it.

    Thanks

    TommyQ

  • #89108

    Sam Boutros
    Participant
    Get-ChildItem ($Paths|Where{$PSItem.NodeName -eq $ENV:Computername}).Path
    
  • #89111

    Richard Siddaway
    Moderator

    If you replace
    $Paths.Where{$Paths.NodeName -eq $ENV:Computername}.Path

    with

    ($paths | Where NodeName -eq $env:COMPUTERNAME).Path

    it'll work.

    It looks the Where() method isn't unravelling the hash table

  • #89113

    TommyQuality
    Participant

    Thanks guys, I literally just spotted my mistake.

     If ($ENV:Computername -eq "VMNAME1" -or "VMNAME2"){
        Get-ChildItem $Paths.Where{$_.NodeName -eq $ENV:Computername}.Path
    }
    

    Cheers

  • #89119

    Richard Siddaway
    Moderator

    There's also an error in the way you're using -or in the if statement

    Look at this test

    $x = 4
    $y = 5
    
    $z = 4
    if ($z -eq $x -or $y){"$z matches $x or $y"}else{"No match"}
    
    $z = 5
    if ($z -eq $x -or $y){"$z matches $x or $y"}else{"No match"}
    
    $z = 2
    if ($z -eq $x -or $y){"$z matches $x or $y"}else{"No match"}
    

    It gives results of

    4 matches 4 or 5
    5 matches 4 or 5
    2 matches 4 or 5
    

    The last match is clearly wrong.

    the correct coding is

    $x = 4
    $y = 5
    
    $z = 4
    if ($z -eq $x -or $z -eq $y){"$z matches $x or $y"}else{"No match"}
    
    $z = 5
    if ($z -eq $x -or $z -eq $y){"$z matches $x or $y"}else{"No match"}
    
    $z = 2
    if ($z -eq $x -or $z -eq $y){"$z matches $x or $y"}else{"No match"}
    

    which gives

    4 matches 4 or 5
    5 matches 4 or 5
    No match
    

    If you think about the original version

    ($z -eq $x -or $y)

    is equivalent to

    ( ($z -eq $x) -or ($y) )
    $y will always return as true so therefore the if statement will always pass

    the correct version
    ($z -eq $x -or $z -eq $y)

    is equivalent to

    ( ($z -eq $x) -or ($z -eq $y) )
    which gives the possibility of both tests being false and so the whole if statement fails the test

    Check the examples in about_Logical_Operators

You must be logged in to reply to this topic.