【问题标题】:Check AD Site Existence检查 AD 站点是否存在
【发布时间】:2013-03-09 11:27:41
【问题描述】:

我想在我的 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 文件中的站点名称进行比较?

【问题讨论】:

    标签: arrays powershell csv foreach sites


    【解决方案1】:

    希望下面的脚本能有所帮助,你应该使用-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" }
    }
    

    【讨论】:

    • 感谢您的帮助,但我收到以下错误。似乎无法识别 -in 参数 --> 表达式或语句中出现意外标记“in”。在 line:9 char:18
    • -in 从 powershell 3.0 开始。
    • @C.B.谢谢,我尽量避免使用新语法,但忘记了这个。
    【解决方案2】:
    $array1 = '1','2','3'
    $array2 = '2','3','4'
    compare $array1 $array2
    get-help compare-object -full
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      BobLobLaw's answer 有一个有趣的想法,但不幸的是,它太不完整而无法实际使用。我发现它有 2 个问题:

      • Import-Csv 产生一个自定义对象数组,而 OP 的 $SitesFilter 是一个字符串数组。 Compare-Object 将始终将每个项目报告为不同的,除非比较的项目属于同一类型。
      • Compare-Object 将报告双方的差异,即 AD 中不存在的站点以及 AD 中存在但未在 CSV 中列出的站点。如果 OP 只想要 AD 中尚不存在的站点,他将不得不按 SideIndicator 过滤输出。但是,您不能将InputObjectSideIndicatorselect -Expand 同时展开。

      这样的事情可能会奏效:

      $csv = Import-Csv "c:\sites.csv" -Header "Site" | % { $_.Site }
      
      compare $csv $SitesFilter | ? {
        (select -Input $_ -Expand SideIndicator) -eq "<="
      } | select -Expand InputObject
      

      为了报告哪个网站存在或不存在,这样的事情可能会做:

      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)
        }
      }
      

      或者可以将select 信息转换为自定义对象:

      $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
          }
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-09
        • 2018-07-26
        • 1970-01-01
        • 1970-01-01
        • 2013-05-22
        • 2013-07-29
        • 1970-01-01
        • 2017-06-03
        相关资源
        最近更新 更多