Welcome › Forums › General PowerShell Q&A › GetChilditem depending on NodeName

AuthorPosts

December 13, 2017 at 11:00 am #89096
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"){ GetChildItem $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

December 13, 2017 at 11:13 am #89108
GetChildItem ($PathsWhere{$PSItem.NodeName eq $ENV:Computername}).Path

December 13, 2017 at 11:15 am #89111
If you replace
$Paths.Where{$Paths.NodeName eq $ENV:Computername}.Pathwith
($paths  Where NodeName eq $env:COMPUTERNAME).Path
it'll work.
It looks the Where() method isn't unravelling the hash table

December 13, 2017 at 11:20 am #89113
Thanks guys, I literally just spotted my mistake.
If ($ENV:Computername eq "VMNAME1" or "VMNAME2"){ GetChildItem $Paths.Where{$_.NodeName eq $ENV:Computername}.Path }
Cheers

December 13, 2017 at 1:12 pm #89119
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 passthe 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 testCheck the examples in about_Logical_Operators

AuthorPosts
The topic ‘GetChilditem depending on NodeName’ is closed to new replies.