0

I've got the following code and have been trying to add an IF statement so that if the item is not found, write the text NotFound.

$csv1 = Import-Csv D:\MaintenanceWindow2.csv
$csv2 = Import-Csv D:\ScomAlerts.csv    
$csv1 | Where {$field = $_.Computername;($csv2 | where {$_.Computername -eq $field})}

-Edit, here is what I currently have but it doesn't seem to pick every server.

Import-Csv D:\2.csv | ForEach-Object {
    $table[$_.Computername] = $_.'Collection Name'}

$Global:result = $AlertDataNoDupe | ForEach-Object { [PSCustomObject] @{ 

Server=$_.NetbiosComputerName

MaintenanceWindow=IF($table[$_.NetbiosComputerName]){$table[$_.NetbiosComputerName]}

                ELSE{"Not found!"}
}

-Edit, adding sample data

MaintenanceWindow2.csv:

"Computername","Collection Name"
"Server1","NA - All DA Servers - Patching - Cert - Thu 2:00"
"Server2","NA - All DA Servers - Patching - Cert - Thu 2:00"

ScomAlerts.csv:

ComputerName
Server2
Server3
8
  • Consider using Compare-Object for something like this. It will tell you the differences. Commented Feb 5, 2016 at 17:29
  • I wasn't able to put together anything that accomplished what this did, being a novice. Commented Feb 5, 2016 at 17:32
  • Have a look at the output from Compare-Object $csv1 $csv2 Commented Feb 5, 2016 at 17:33
  • Unclear what you're asking. You never use a hashtable. The sample and text doesn't match. Commented Feb 5, 2016 at 17:47
  • Apologies, that was from sample code I used. I'll edit the question since the $hash = @{} does not seem to be needed. Commented Feb 5, 2016 at 17:50

1 Answer 1

2

The script in your updated question should work fine. The only changes I would make is to use $table.ContainsKey("") and use indents, but that's mostly for readability.

$MaintenanceWindows = @"
"Computername","Collection Name"
"Server1","NA - All DA Servers - Patching - Cert - Thu 2:00"
"Server2","NA - All DA Servers - Patching - Cert - Thu 2:00"
"@ | ConvertFrom-Csv

$ScomAlerts = @"
ComputerName
Server2
Server3
"@ | ConvertFrom-Csv

#Create hashtable
$table = @{}
#Add MWs to hashtable
$MaintenanceWindows | ForEach-Object { $table.Add(($_.ComputerName.Split(".")[0].Trim()), $_.'Collection Name')}

$Global:result = $ScomAlerts | ForEach-Object {
    $computer = $_.ComputerName.Split(".")[0].Trim()

    [PSCustomObject] @{ 
        Server = $computer
        MaintenanceWindow = if($table.ContainsKey($computer)){
            $table[$computer]
        } else{ "Not found!" }
    }

}

$result

Server  MaintenanceWindow                               
------  -----------------                               
Server2 NA - All DA Servers - Patching - Cert - Thu 2:00
Server3 Not found!

or

Compare-Object -ReferenceObject $MaintenanceWindows -DifferenceObject $ScomAlerts -Property Computername -IncludeEqual -PassThru |
Where-Object { $_.SideIndicator -match '==|=>' } |
Select-Object ComputerName, @{n="Collection Name";e={ if($_."Collection Name"){ $_."Collection Name" } else { "Not Found!" } }}

Computername Collection Name                                 
------------ ---------------                                 
Server2      NA - All DA Servers - Patching - Cert - Thu 2:00
Server3      Not Found!
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, might I point out the point of the question was to add text that indicated a match was not found. This command returns 149 matches and the DifferenceObject contains 196.
See updated answer. This will output the difference in both. Do you only want the ones unique to $csv2? Because you sample above would only output $csv1 objects (that match btw)..
Managed to come this far, $diff contains the Compare-Object results, I use two Where filters to create a $total variable. However the Foreach-Object above outputs the results to the ComputerName column. I'm struggling to get that output into the second column.
Now that we've seen what you want, I've provided a tuned edition of your code (yours should have worked btw) and an alternative using Compare-Object. Personally I would have chosen the hashtable-solution considering you have one source with Computername-property and one with NetbiosComputerName which Compare-Object doesn't like. What do you mean it doesn't pick every server? Your code would only display servers from $AlertDataNoDupe which is what you're code is written to do. What's missing from the output?
A spot check revealed items marked as NotFound were in fact present in the CSV containing the MaintenanceWindow data. Also, I tried ContainsKey but didn't get consistent results. Very strange.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.