dpj
1
Hello everyone,
So, my second question.
I have an array say like this:
AAAAA
CCCCC
EEEEE
GGGGG
My csv file looks like this:
A;B
AAAAA; 11111
BBBBB; 22222
CCCCC; 33333
DDDDD; 44444
EEEEE; 55555
FFFFF; 66666
GGGGG; 77777
What is want is to receive the value from column 2
What is allready achieved:
$content = Import-CSV powershellexample.csv -Delimiter “;” -Encoding Default
$Match = Read-Host “give me the value”
$ResultMatch = $content -match $Match
$ResultMatch.B
But this is just for one value, and i want all values from my array.
So i was thinking like this:
foreach ($arrayvalue in $Array)
{
if ($Array -match $content.A) {write-host $content.B}
}
but off course, this isnt working, else i wouldn’t ask this question…
Hope someone can point me again in the right direction.
Yours, Dennis
Olaf
2
For your third question you format your code, error messages and console output as code, please.
There are several approaches you could use to compare objects in PowerShell. I like to use …
Therefor we have to turn your array into a list of proper objects with properties. Easily done with Select-Object
and a calulated property …
$InputArray =
'AAAAA',
'CCCCC',
'EEEEE',
'GGGGG'
$ComparableObjectList =
$InputArray |
Select-Object -Property @{Name = 'A'; Expression = {$_}}
Of course we need your CSV input data:
$CSVData = @'
A;B
AAAAA;11111
BBBBB;22222
CCCCC;33333
DDDDD;44444
EEEEE;55555
FFFFF;66666
GGGGG;77777
'@ |
ConvertFrom-Csv -Delimiter ';'
Now it’s a piece of cake to compare the objects with Compare-Object
and output the property you’re after.
Compare-Object -ReferenceObject $CSVData -DifferenceObject $ComparableObjectList -Property A -PassThru -IncludeEqual -ExcludeDifferent |
Select-Object -Property B
The result looks like this:
B
-
22222
44444
66666
To make the last command easier readable I’d recommend to use splatting.
It looks like this then:
$CompareObjectSplat = @{
ReferenceObject = $CSVData
DifferenceObject = $ComparableObjectList
Property = 'A'
PassThru = $true
IncludeEqual = $true
ExcludeDifferent = $true
}
Compare-Object @CompareObjectSplat |
Select-Object -Property B