Вопрос

I would like to import sites in my Active Directory environment with a CSV containing sitenames:

Example of my CSV-input:

New York
Dallas
New Jersey

I want to make a script that checks the existence of the sites first before the actual creation process occurs. However, I'm having some trouble with checking the input of 2 arrays:

#Clear process
$ADsites = ""
$SitesFilter = ""
$CSV = ""

[array] $ADSites = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites 

$csv=Import-Csv c:\sites.csv -header "Site"

#Filtering the Sitenames
Foreach ($ADSite in $ADSites) {
    [array] $SitesFilter += $ADSite.Name
}

$CSV | Foreach-Object {
    if (??? -eq $_.Site) {
        Write-Host "Site" $_.Site "already exists" 
    } else {
        Write-Host "Site" $_.Site "is not found"
    }
}

How can I compare the content of the array $SitesFilter with the sitenames in the CSV-file?

Это было полезно?

Решение

Hope below script could help, you should use -in, which could tell if object is within an array

[array] $ADSites = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites 

$csv=Import-Csv c:\sites.csv -header "Site"

#Get an site name string array
$SitesFilter = @($ADSites | %{"$($_.Name)"}) 

$CSV | Foreach-Object {
    if ($SitesFilter -contains $_.Site ){
                Write-Host "Site $($_.Site) already exists" 
    } Else { Write-Host "Site $($_.Site) is not found" }
}

Другие советы

$array1 = '1','2','3'
$array2 = '2','3','4'
compare $array1 $array2
get-help compare-object -full

Hope this helps

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
    }
  }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top