BobLobLaw's answer has an interesting idea, but is unfortunately far too incomplete to be of actual use. I see 2 issues with it:
Import-Csv
produces an array of custom objects, whereas the OP's $SitesFilter
is an array of strings. Compare-Object
will always report every item as different unless the compared items are of the same type.
Compare-Object
will report differences on both sides, i.e. sites that don't exist in AD as well as sites that do exist in AD but are not listed in the CSV. If the OP wants only sites that don't already exist in AD he'll have to filter the output by SideIndicator
. However, you can't expand InputObject
and SideIndicator
at the same time with select -Expand
.
Something like this might do the trick:
$csv = Import-Csv "c:\sites.csv" -Header "Site" | % { $_.Site }
compare $csv $SitesFilter | ? {
(select -Input $_ -Expand SideIndicator) -eq "<="
} | select -Expand InputObject
For reporting which site does or doesn't exist something like this might do:
compare $csv $SitesFilter | % {
if ( (select -Input $_ -Expand SideIndicator) -eq "<=" ) {
"Site '{0}' doesn't exist." -f (select -Input $_ -Expand InputObject)
} else {
"Site '{0}' already exists." -f (select -Input $_ -Expand InputObject)
}
}
Or one could select
the information into custom objects:
$site = { select -Input $_ -Expand InputObject }
$exists = { (select -Input $_ -Expand SideIndicator) -ne "<=" }
compare $csv $SitesFilter `
| select @{n='Site';e=$site},@{n='Exists';e=$exists} `
| % {
if ( $_.Exists ) {
"Site '{0}' exists." -f $_.Site
} else {
"Site '{0}' doesn't exist." -f $_.Site
}
}