[英]Get-AdUser no acepting variable
im trying to pull out all the information regarding my domain admin adminsitrators我试图提取有关我的域管理员的所有信息
#set domains we are going to query
$domains = 'mydomainname.com'
#first here i bring the sam accounts names #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 ;
}
I think this is a problem with your filter.我认为这是您的过滤器的问题。 Try changing that line as follows:
尝试按如下方式更改该行:
Get-ADUser -Filter " SamAccountName -like `"$($user.samaccountname)`" " -Properties *
It can't be $OUTPUT
as that's an array.它不能是
$OUTPUT
因为那是一个数组。 Your loop variable is $user
which is an object so you need the .samaccountname
property.你的循环变量是
$user
,它是一个 object 所以你需要.samaccountname
属性。 The filter needs a string with the -like
matching a quoted string, so you need `" to pass the quote through to the final string.过滤器需要一个带有
-like
的字符串与带引号的字符串匹配,因此您需要 `" 将引号传递给最终字符串。
Your CSV output of $user2
may not be what you expect either as each object is output to the same file.您的
$user2
的 CSV output 可能不是您所期望的,因为每个 object 与 DB 文件相同。 Perhaps you mean to have a -Append
or write them to different files?也许您的意思是有一个
-Append
或将它们写入不同的文件?
You'll probably want to reset $user3
as well.您可能还想重置
$user3
。 Perhaps add $user3 = @()
before that loop.也许在该循环之前添加
$user3 = @()
。
You should always try to avoid adding to arrays with $array += $something
, because that means the entire array gets rebuild in memory, costing time and resources.您应该始终尽量避免使用
$array += $something
添加到 arrays ,因为这意味着整个阵列将在 memory 中重建,这会耗费时间和资源。
Also, I would advise using more descriptive variable names, so the code will still be understandable after some time.另外,我建议使用更具描述性的变量名称,这样代码在一段时间后仍然可以理解。
Then, because you are getting info from different domains, it is important to store the domain name in the first loop together with the samaccount names, so you can use these as -Server
parameter in the second loop on Get-ADUser
然后,因为您从不同的域获取信息,所以将域名与 samaccount 名称一起存储在第一个循环中很重要,因此您可以在
Get-ADUser
的第二个循环中将这些用作-Server
参数
Try尝试
#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.