[英]Get-AdUser no acepting variable
我试图提取有关我的域管理员的所有信息
#set domains we are going to query
$domains = 'mydomainname.com'
#first 这里我带来了山姆账户的名字
Foreach ($domain in $domains)
{
$OUTPUT =Get-AdGroupMember -identity “Domain Admins” -recursive -server $domain |
Select-Object -Property samAccountName|
Select samAccountName;
$Outputs +=$OUTPUT;
$OUTPUT |Export-CSV "C:\File\$($domain).csv" -NoTypeInformation ;
}
$OUTPUT #this print the sam accounts
#here is the problem
Foreach ($user in $OUTPUT)
{
$Users2 =Get-ADUser -Filter "SamAccountName -like '$OUTPUT'" -Properties *
$USER3 +=$Users2;
$Users2 |Export-CSV "C:\File\$($domain)Userpop.csv" -NoTypeInformation ;
}
我认为这是您的过滤器的问题。 尝试按如下方式更改该行:
Get-ADUser -Filter " SamAccountName -like `"$($user.samaccountname)`" " -Properties *
它不能是$OUTPUT
因为那是一个数组。 你的循环变量是$user
,它是一个 object 所以你需要.samaccountname
属性。 过滤器需要一个带有-like
的字符串与带引号的字符串匹配,因此您需要 `" 将引号传递给最终字符串。
您的$user2
的 CSV output 可能不是您所期望的,因为每个 object 与 DB 文件相同。 也许您的意思是有一个-Append
或将它们写入不同的文件?
您可能还想重置$user3
。 也许在该循环之前添加$user3 = @()
。
您应该始终尽量避免使用$array += $something
添加到 arrays ,因为这意味着整个阵列将在 memory 中重建,这会耗费时间和资源。
另外,我建议使用更具描述性的变量名称,这样代码在一段时间后仍然可以理解。
然后,因为您从不同的域获取信息,所以将域名与 samaccount 名称一起存储在第一个循环中很重要,因此您可以在Get-ADUser
的第二个循环中将这些用作-Server
参数
尝试
#set domains we are going to query
$domains = @('mydomainname.com') # since there is only one domain listed, use @() to force it into an array
$domainsAndAdmins = foreach ($domain in $domains) {
# store the SamAccountNames for this domain as objects in an array
$admins = Get-AdGroupMember -Identity 'Domain Admins' -Recursive -Server $domain |
Select-Object -Property SamAccountName
# export this to csv file
$outFile = 'C:\File\{0}.csv' -f $domain
$admins | Export-Csv $outFile -NoTypeInformation
# output an object with both the domain and the array of SamAccountNames
# this will be captured in variable $domainsAndAdmins
[PsCustomObject]@{Domain = $domain; Admins = $admins.SamAccountName }
}
# output on screen
$domainsAndAdmins | Format-Table -AutoSize
# will result in something like
#
# Domain Admins
# ------ ------
# mydomainname.com {jdoe, jbloggs, mpimentel}
# myseconddomainname.com {jdoe, mpimentel}
# next get ALL (?) properties from the users we found
$domainsAndAdmins | ForEach-Object {
$domain = $_.Domain
$result = foreach ($user in $_.Admins) {
Get-ADUser -Filter "SamAccountName -like '$user'" -Server $domain -Properties * -ErrorAction SilentlyContinue
}
$outFile = 'C:\File\{0}_Userpop.csv' -f $domain
$result | Export-Csv $outFile -NoTypeInformation
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.