I have the following in a script:
$Loc = Read-Host "Please select a location number"
$ImpCSV = Import-Csv -Delimiter ";" -Path "file.csv" -Header loc,add | Where-Object {$_.loc -eq "$Loc"}
Foreach ($CIM in $ImpCSV) {$LocInfo = $CIM}
This seems to create some sort of hash/array/something variable for $Loc=1. If I enter:
$LocInfo.add
It gives me the value of that key(?) as it should in a hash table, but if I do:
$LocInfo
I get:
loc : [1]
add : [some value]
Which is not the usual hash output. This is what one should look like:
Name Value
---- -----
loc [1]
add [some value]
The issue this is first causing me is that if I try to edit an entry using $LocInfo.Set_Item("add", "HI"), it does nothing. Also, I created a table to display those variables creating new-objects, but the new -property command won't take $LocInfo.add, returning a "@{loc=1; add=some value}" if not giving me a syntax error. What I had to do was:
Set-Variable -Name add -Value $LocInfo.add
This sets add to [some value] and then I can put that in the new-object field without any issue.
This is additionally problematic because my script loops so they can add another location (loc) number (let's say 2) and pull up that location's data as necessary. If the new location they are searching has say the add (Address) missing, instead of creating a blank $LocInfo.add and then a blank $add, it skips it, meaning when the new-object is created again, it uses the $add from the previous run, [some value]; ie both loc 1 and 2 have the same $add value.
What that heck did I create, and how can I do anything with it? Is there a better way to pull variables from a CSV file that won't screw me up like this? Did I break Powershell? I've looked all over and I can't find this kind of variable arrangement.
This is the error I get when I try to do a Set_Item:
Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named 'Set_Item'.
At line:1 char:1
+ $LocInfo.Set_Item("add", "HI")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Set_Item:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
I've created objects using New-Object, and even those display like a hash table, ie with a header and multiple columns depending on the object size.