我想在我的Active Directory环境中使用包含网站名称的CSV导入网站:
我的CSV输入示例:
New York
Dallas
New Jersey
我想制作一个脚本,在实际创建过程发生之前先检查网站是否存在。但是,我在检查2个数组的输入时遇到了一些麻烦:
#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"
}
}
如何将数组$SitesFilter
的内容与CSV文件中的网站名进行比较?
答案 0 :(得分:2)
希望以下脚本可以提供帮助,您应该使用-in
,这可以判断对象是否在数组中
[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" }
}
答案 1 :(得分:0)
$array1 = '1','2','3'
$array2 = '2','3','4'
compare $array1 $array2
get-help compare-object -full
希望这有帮助