Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell通过CSV搜索广告并报告禁用/启用/不存在的用户_Powershell_Active Directory - Fatal编程技术网

Powershell通过CSV搜索广告并报告禁用/启用/不存在的用户

Powershell通过CSV搜索广告并报告禁用/启用/不存在的用户,powershell,active-directory,Powershell,Active Directory,下面的脚本有一个CSV输入,其中包含一列samaccountname和一个用户列表。运行时,它会生成一个包含3列的CSV报告: 帐户存在 帐户禁用 samaccountname 如果在当前状态下运行,它确实会生成一个报告,说明该帐户是否已禁用以及该帐户是否存在,但是如果它遇到AD中不存在的用户,则不会将其添加到CSV报告中,并为每个用户引发以下错误: 无法索引到空数组中。第4行字符:75 + ... ($account=([adsisearcher]“(samaccountname=$($)

下面的脚本有一个CSV输入,其中包含一列
samaccountname
和一个用户列表。运行时,它会生成一个包含3列的CSV报告:

  • 帐户存在
  • 帐户禁用
  • samaccountname
如果在当前状态下运行,它确实会生成一个报告,说明该帐户是否已禁用以及该帐户是否存在,但是如果它遇到AD中不存在的用户,则不会将其添加到CSV报告中,并为每个用户引发以下错误:

无法索引到空数组中。第4行字符:75 + ... ($account=([adsisearcher]“(samaccountname=$($).samaccountname)))。fin ... + ~~~~~~~~~~~~~~~~~ +CategoryInfo:InvalidOperation:(:)[],运行时异常 +FullyQualifiedErrorId:NullArray

是否可以将不存在的用户添加到CSV报告中的account exists列下,该列的值为
FALSE

Import-CSV C:\ScriptRepository\Users.csv | ForEach-Object {
    New-Object -TypeName PSCustomObject -Property @{
        samaccountname = $_.samaccountname
        AccountExists = [bool]($account=([adsisearcher]"(samaccountname=$($_.samaccountname))").findone()).count
        AccountDisabled = [bool]($account.properties.useraccountcontrol[0] -band 2)
    }
} | Export-Csv C:\ScriptRepository\UsersState.csv -NoTypeInformation

您可以通过将逻辑移到哈希表之前来实现这一点:

Import-CSV C:\ScriptRepository\Users.csv | ForEach-Object {
    $AccountExists = If ( (([adsisearcher]"(samaccountname=$($_.samaccountname))").FindOne()) ) { $true } else { $false }
    If ($AccountExists) { $AccountDisabled = [bool]($AccountExists.properties.useraccountcontrol[0] -band 2) } Else { $AccountDisabled = '' }

    New-Object -TypeName PSCustomObject -Property @{
        samaccountname = $_.samaccountname
        AccountExists = $AccountExists
        AccountDisabled = $AccountDisabled
    }
} | Export-Csv C:\ScriptRepository\UsersState.csv -NoTypeInformation

以下是我的处理方法:

$ADS_UF_ACCOUNTDISABLE = 2

$searcher = [ADSISearcher] ""
$searcher.PropertiesToLoad.AddRange(@("userAccountControl"))

Import-Csv "Users.csv" | ForEach-Object {
  $searcher.Filter = "(sAMAccountName=$($_.sAMAccountName))"
  $account = $searcher.FindOne()
  if ( $account ) {
    $exists = $true
    $disabled = ($account.Properties["useraccountcontrol"][0] -band $ADS_UF_ACCOUNTDISABLE) -ne 0
  }
  else {
    $exists = $false
    $disabled = "N/A"
  }
  [PSCustomObject] @{
    "sAMAccountName"  = $_.sAMAccountName
    "AccountExists"   = $exists
    "AccountDisabled" = $disabled
  }
}

请使用“CSV输入”示例更新您的问题。您不使用AD cmdlet有什么具体原因吗?嗨,比尔,CSV的格式正确,列标题为samaccountname,下面有帐户名。嗨,马克,没有原因,如果可能的话,我们会欢迎它。这只是一个哈希表,因为它们是键->值对,但绝对不是哈希表对象。y是什么您创建的是PSCustomObject。哈希表具有不同的属性和方法,不能像PSCustomObject那样使用传统的PoSh cmdlet(例如select、where等)。请参阅:(新建对象PSObject-Property@{'name'='value'})。GetType();(新建对象PSObject-ArgumentList@{'name'='value'})。GetType()我指的是传递给新对象的-Property参数的哈希表,该参数正在创建PSCustomObject。您好,不幸的是,在运行该参数时,CSV:中的每个帐户现在都会发生上一个错误:无法索引到空数组中。在第4行char:75+…($account=([adsisearcher]”(samaccountname=$($\.samaccountname))).fin…+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+类别信息:无效操作:(:)[],RuntimeException+FullyQualifiedErrorId:NullArrayI我不确定这段代码应该如何工作?$AccountExists最终是一个布尔值,但您将其视为它通过引用$AccountDisabled上的属性返回ADSISearcher对象……即使它没有返回布尔值,您也从中返回整数.count()方法也…???为Bill干杯,它完全按照需要工作,没有错误,并且包含不存在的帐户。非常感谢您的时间。非常感谢。没问题,很高兴它能为您工作。它应该非常快。它确实很快,对于644个用户来说大约6秒。再次感谢Bill。