try Something like this:
$array1=import-csv "C:\temp\log.csv"
$array2=import-csv "C:\temp\log2.csv"
#modify founded and output not founded
$toadd=$array2 | %{
$current=$_
$founded=$array1 | where pc -eq $current.pc | %{$_.date=$current.date;$_}
if ($founded -eq $null)
{
$current.city='UNKNOW'
$current
}
}
#output of $array1 modified and elements to add
$array1, $toadd
Answer from Esperento57 on Stack Overflowtry Something like this:
$array1=import-csv "C:\temp\log.csv"
$array2=import-csv "C:\temp\log2.csv"
#modify founded and output not founded
$toadd=$array2 | %{
$current=$_
$founded=$array1 | where pc -eq $current.pc | %{$_.date=$current.date;$_}
if ($founded -eq $null)
{
$current.city='UNKNOW'
$current
}
}
#output of $array1 modified and elements to add
$array1, $toadd
Here is a sample I created that might help. Note: I use List types instead of ArrayList ones. Also, it assumes only one possible matching PC name in the data to be updated. You'll have to alter it to update the file since it merely updates the first List variable. Let me know how it goes.
๏ปฟ[PSCustomObject]
{
[string] $pc,
[string] $name,
[string] $date,
[string] $city
}
[System.Collections.Generic.List[PSCustomObject]] $list1 = Import-Csv "C:\SOSamples\log.csv";
[System.Collections.Generic.List[PSCustomObject]] $list2 = Import-Csv "C:\SOSamples\log2.csv";
[PSCustomObject] $record = $null;
[PSCustomObject] $match = $null;
foreach($record in $list2)
{
# NOTE: This only retrieves the FIRST MATCHING item using a CASE-INSENSITIVE comparison
$match = $list1 | Where { $_.pc.ToLower() -eq $record.pc.ToLower() } | Select -First 1;
if($match -eq $null)
{
Write-Host "Not Found!";
$list1.Add($record);
}
else
{
Write-Host "Found!";
$match.date = $record.date;
}
}
Write-Host "--------------------------------------------------------------------"
foreach($record in $list1)
{
Write-Host $record
}
powershell - PS: Get index in an array list - Stack Overflow
Finding Arraylist Object properties
Powershell Array list
When to use array vs array list vs list
If you know that the value occurs only once in the array, the [array]::IndexOf() method is a pretty good way to go:
$array = 'A','B','C'
$item = 'B'
$ndx = [array]::IndexOf($array, $item)
Besides being terse and to the point, if the array is very large the performance of this approach is quite a bit better than using a PowerShell cmdlet like Where-Object. Still, it will only find the first occurrence of the specified item. But you can use the other overload of IndexOf to find the next occurrence:
$ndx = [array]::IndexOf($array, $item, $ndx+1)
$ndx will be -1 if the item isn't found.
Use a for loop (or a foreach loop that iterates over the array index...same difference). I don't know of any system variable that holds the current array index inside a foreach loop, and I don't think one exists.
# example array
$array = "A", "B", "C"
$item = "B"
0..($array.Count - 1) | Where { $array[
item }
I am looping on a folder of output generated by the command "net localgroup administrators". Within the output is a list of security groups that belong to the local administrator group on a PC. My goal is to gather all security groups then create a matrix of PC names and these security groups. It would look something like this:
| Asset | SG1 | SG2 | SG3 | SG4 | SG5 | SG6 |
|---|---|---|---|---|---|---|
| PC1 | x | x | x | |||
| PC2 | x | x | x | x | ||
| PC3 | x | x |
Here's what I have so far for code:
$Matrix = New-Object System.Collections.ArrayList
$Matrix | Add-Member -MemberType NoteProperty -Name "Asset" -Value "" -Force
$Files = Get-ChildItem -Recurse -Path "C:\PathToFiles\"
$Files | % {
# Create a new object that'll get added to the Matrix arraylist.
$Obj = New-Object System.Object
$Obj | Add-Member -MemberType NoteProperty -Name "Asset" -Value ($_.Name).Split(' ')[0]
# Get the name of the asset from the filename.
# $Asset = ($_.Name).Split(' ')[0]
$Extract = $false
$Lines = Get-Content -Path $_.FullName
Foreach ($Line in $Lines) {
If ($Extract -eq $false) {
If ($Line -match "-{2,}") {
$Extract = $true
}
}
Elseif ($Line -eq "The command completed successfully.") {
$Extract = $false
}
Else {
$Obj | Add-Member -MemberType NoteProperty -Name $Line -Value "x"
}
}
$Matrix += $Obj
}
$Matrix | ft -AutoSizeI'm not sure if it's the way I'm creating the Matrix array or something else entirely but outputting $Matrix to the screen using format-table or exporting to CSV I'm missing several columns. Doing $Matrix[0] and $Matrix[1] shows me all the data I expect...
PS C:\Scripting> $Matrix[0] | ft
| Asset | Airport_Admin | COSAdmin | D1LogRythm Services | D2Domain Admins | D2IT-WorkstationAdmins | MJ |
|---|---|---|---|---|---|---|
| Machine 1 | x | x | x | x | x | x |
PS C:\Scripting> $Matrix[1] | ft
| Asset | D3IT-WorkstationAdmins | D1LogRythm Services | D2CSPD-PowerUsers-Systems | D2Domain Admins | D2IT-WorkstationAdmins | D2MJRCRIME | MJ |
|---|---|---|---|---|---|---|---|
| Machine 2 | x | x | x | x | x | x | x |
but
$Matrix | Export-CSV "C:\Temp\Output.csv" -NTI
Only gives me the columns present in $Matrix[0].
What silly mistake am I making? Why can't I get an output with all my columns?