Jason Crawford
asked on
PowerShell - Match an item in an array to an item in a larger array then do stuff
I have two variables:
$a = 1, 2, 3, 4, 5
$b = 1, 1, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5
How can I tell PowerShell to iterate through the $a array and match any applicable items in the $b array? Here is my poor attempt:
$a = 1, 2, 3, 4, 5
$b = 1, 1, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5
How can I tell PowerShell to iterate through the $a array and match any applicable items in the $b array? Here is my poor attempt:
$a | ForEach-Object {
if ($b -contains $_) {
Do-Stuff
}
}
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Man I don't know what it is, but I've seen that same for loop format before and it confuses the hell out of me. What does this mean - $b[$i]. What are you doing there?
You probably want to use the -contains operator.
Example:
Example:
$a = 1, 2, 3, 4, 5
$b = 1, 6, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5
$b |
%{
if ($a -contains $_){
write-host "Doing something with $_"
}
}
ASKER
Ok yea that makes sense. So here's what I'm actually doing. An application generates a .txt file containing several hundred one line values with a server name in the value. For example 'text;SERVER1-moretextyada yada' and then on the next line 'differenttext;SERVER1-dif ferentyada yada'. However, not all lines contain the same server name. The task I've been assigned is to create a new file for each unique server found in the source .txt file, then in that file add every line containing that server's name. In the example I just gave above, the file would be like the one I'm attaching. Here's what I've done so far:
So basically all I've done is selected all unique instances of all servers in the source file and created new, empty .txt files. Now all I need to do is iterate through the $files variable and, if the name matches the string in the source file, add that content to the '{0}\desktop\test\{1}.txt' file created previously.
function Separate-Files {
param
(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, HelpMessage='Data to process')]
[Object]$InputObject
)
process {
New-Item -ItemType file -Path ('{0}\desktop\test\{1}.txt' -f $env:USERPROFILE, $InputObject)
}
}
try {
Set-Location C:\Users\jcrawford1216\Desktop -ErrorAction Stop
}
catch [IO.IOException], [Microsoft.PowerShell.Commands.NewItemCommand] {
[Management.Automation.ErrorRecord]$e = $_
$info = [PSCustomObject]@{
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Script = $e.InvocationInfo.ScriptName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
$info
}
$dump = Import-Csv .\develop.csv
$server = $dump.'server name'
$serverunique = $server | Select-Object -Unique
$serverunique | Separate-Files
$files = Get-ChildItem C:\Users\jcrawford1216\Desktop\Test | Select-Object BaseName
So basically all I've done is selected all unique instances of all servers in the source file and created new, empty .txt files. Now all I need to do is iterate through the $files variable and, if the name matches the string in the source file, add that content to the '{0}\desktop\test\{1}.txt'
Did my comment help you tweak your script or do you need more help?
ASKER
The way I asked it was vague but you were still able to help. Thank you very much.
you were still able to help. Thank you very much.Did you mean to close the question in the manner that you did? Seems a bit inconsistent to not give any credit/points after thanking me for helping.
ASKER
I was being polite. The code you provided is almost identical to the one I included in my opening question. Thank you for the uninvited criticism though. Take care.
I thought you might have reversed your $a and $b look-ups. No criticism intended.
$a = 1, 2, 3, 4, 5
$b = 1, 1, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5
$count = $a.count
for ($i=0;$i -lt $count; $i++)
{
if ($b[$i] -eq $a[$i]) { Write-host "Equal"}
}