powershell - Add an IF into CSV filtering loop -


i've got following code , have been trying add if statement if item not found, write text notfound.

$csv1 = import-csv d:\maintenancewindow2.csv $csv2 = import-csv d:\scomalerts.csv     $csv1 | {$field = $_.computername;($csv2 | {$_.computername -eq $field})} 

-edit, here have doesn't seem 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 - da servers - patching - cert - thu 2:00" "server2","na - da servers - patching - cert - thu 2:00" 

scomalerts.csv:

computername server2 server3 

the script in updated question should work fine. changes make use $table.containskey("") , use indents, that's readability.

$maintenancewindows = @" "computername","collection name" "server1","na - da servers - patching - cert - thu 2:00" "server2","na - da servers - patching - cert - thu 2:00" "@ | convertfrom-csv  $scomalerts = @" computername server2 server3 "@ | convertfrom-csv  #create hashtable $table = @{} #add mws 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 - 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 - da servers - patching - cert - thu 2:00 server3      not found! 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -