0

我正在尝试从具有组名(规范名称)列表的 CSV 文件中获取输入,并从中获取专有名称,然后输出到另一个 CSV 文件。编码:

#get input file if passed    
Param($InputFile)

#Set global variable to null
$WasError = $null

#Prompt for file name if not already provided
If ($InputFile -eq $NULL) {
  $InputFile = Read-Host "Enter the name of the input CSV file (file must have header of 'Group')"
}

#Import Active Directory module
Import-Module -Name ActiveDirectory -ErrorAction SilentlyContinue

$DistinguishedNames = Import-Csv -Path $InputFile -Header Group | foreach-Object {
  $GN = $_.Group
  $DN = Get-ADGroup -Identity $GN | Select DistinguishedName
}
$FileName = "RESULT_Get-DistinguishedNames" + ".csv"

#Export list to CSV
$DNarray | Export-Csv -Path $FileName -NoTypeInformation

我尝试了多种解决方案,但似乎都没有奏效。目前,它会引发错误,因为

无法验证参数“身份”上的参数。参数为空。提供一个非空参数并再次尝试该命令。

-Filter也尝试过使用,在之前的尝试中我使用了以下代码:

Param($InputFile)

#Set global variable to null
$WasError = $null

#Prompt for file name if not already provided
If ($InputFile -eq $NULL) {
  $InputFile = Read-Host "Enter the name of the input CSV file(file must have header of 'GroupName')"
}

#Import Active Directory module
Import-Module -Name ActiveDirectory -ErrorAction SilentlyContinue

$DistinguishedNames = Import-Csv -Path $InputFile | foreach {
  $strFilter = "*"

  $Root = [ADSI]"GC://$($objDomain.Name)" 

  $objSearcher = New-Object System.DirectoryServices.DirectorySearcher($root) 
  $objSearcher.Filter = $strFilter 
  $objSearcher.PageSize = 1000
  $objsearcher.PropertiesToLoad.Add("distinguishedname") | Out-Null

  $objcolresults = $objsearcher.FindAll() 
  $objitem = $objcolresults.Properties 
  [string]$objDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
  [string]$DN = $objitem.distinguishedname
  [string]$GN = $objitem.groupname

  #Get group info and add mgr ID and Display Name
  $props = @{'Group Name'= $GN;'Domain' = $objDomain;'Distinguished Name' = $DN;}
  $DNS = New-Object psobject -Property $props 
}
$FileName = "RESULT_Get-DistinguishedNames" + ".csv"

#Export list to CSV
$DistinguishedNames | Sort Name | Export-Csv $FileName -NoTypeInformation

过滤器与我在这里使用的过滤器不同,我找不到我正在使用的过滤器,我目前拥有的是一次失败的尝试。

无论如何,我遇到的主要问题是它会获取组名,但是在错误的域中搜索它(它不包括组织单位),导致找不到它们。当我在 PowerShell 中搜索组时(使用Get-ADGroup ADMIN),它们会显示正确的 DN 和所有内容。任何提示或代码示例表示赞赏。

4

1 回答 1

1

你似乎错过了$variable = cmdlet|foreach {script-block}分配的重点。要分配给的对象$variable应返回(通过脚本块传递),以便以$variable. 您的两个主循环都包含a或call的行$somevar=expectedOutput的结构。对 的赋值禁止输出,因此脚本块没有任何要返回的内容,并且保持为空。要解决此问题,请不要在应该将对象返回到外部变量的调用之前加上赋值。expectedOutputNew-Object psobjectGet-ADGroup$someVar$variable

$DistinguishedNames = Import-Csv -Path $InputFile -Header Group | foreach-Object {
    $GN = $_.Group
    Get-ADGroup -Identity $GN | Select DistinguishedName # drop '$DN=`
}
$DistinguishedNames | Export-CSV -Path $FileName -NoTypeInformation

第二个脚本也有同样的问题。

于 2015-07-30T15:29:58.577 回答