[英]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.