為基本問題道歉,我喜歡涉足 Powershell,但由于我不經常使用它,所以我忘記了很多!
我有一個用戶員工編號 (employee.csv) 的 csv 串列,位于 AD 中的“extensionAttribute15”下
我想,對于串列中的每個員工編號,將每個帳戶 SamAccountName 及其 AD 組成員身份回傳到 Excel 表中
到目前為止我有
$employee = Import-csv .\Downloads\employee.csv
$employee | ForEach-Object {
Get-ADUser -Credential $Credentials -Filter "extensionAttribute15 -like '*$($_.extensionAttribute15)*'" -Properties * | Select-Object extensionAttribute15, Name, emailAddress, samAccountName, Description, MemberOf | Export-Csv .\Downloads\new_employee.csv
}
這有效,但它不會擴展 AD 組成員資格......
提前致謝!
uj5u.com熱心網友回復:
考慮您希望結果如何顯示。所有組都應該在同一個單元格中,用新行分隔嗎?用分隔符分隔?每個組都顯示在新的行/列中?
這是在同一單元格中顯示每個組的基本方法,用新行分隔
$employee | ForEach-Object {
Get-ADUser -Credential $Credentials -Filter "extensionAttribute15 -like '*$($_.extensionAttribute15)*'" -Properties * |
Select-Object extensionAttribute15, Name, emailAddress, samAccountName, Description, @{name="MemberOf";expression={$_.MemberOf | Out-String}}} |
Export-Csv .\Downloads\new_employee.csv
這是一個鏈接
uj5u.com熱心網友回復:
正如所評論的,Get-ADUser已經回傳具有這些屬性的物件:
DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName.
在您的情況下,您還希望回傳這些屬性:extensionAttribute15, EmailAddress, Description and MemberOf
您的代碼中的一個缺陷是您Export-Csv 在回圈中,因此在每次迭代時它都會被覆寫。您可以通過在此處添加開關來避免這種情況-Append,但隨后您將創建大量磁盤寫入操作。
最好的方法是在回圈結束時將所有內容通過管道傳輸到 Export-Csv ForEach-Object,因此檔案只需要寫入一次。
$employee = Import-csv -Path '.\Downloads\employee.csv'
# an array to hold the extra properties Get-ADUser should include in the results
$propertiesToInclude = 'extensionAttribute15', 'EmailAddress', 'Description', 'MemberOf'
# now loop over the items of the CSV, and pipe the complete collection at the end to Export-Csv
$employee | ForEach-Object {
Get-ADUser -Credential $Credentials -Filter "extensionAttribute15 -like '*$($_.extensionAttribute15)*'" -Properties $propertiesToInclude |
Select-Object extensionAttribute15, Name, emailAddress, samAccountName, Description, MemberOf
} | Export-Csv -Path '.\Downloads\new_employee.csv' -NoTypeInformation
如果您愿意,可以將 MemberOf 屬性的值合并為一列,方法是將Select-Object行更改為
Select-Object extensionAttribute15, Name, emailAddress, samAccountName, Description,
@{Name = 'MemberOf'; Expression = { $_.MemberOf -join '; ' }} -ExcludeProperty MemberOf
uj5u.com熱心網友回復:
Json 支持陣列并為此作業得很好。Convertfrom-json 將 memberof 轉換回陣列。
get-aduser js -property memberof,extensionAttribute15,emailAddress,Description |
select name,samaccountname,memberof,extensionAttribute15,emailAddress,Description |
Convertto-json | set-content new_employee.json
{
"extensionAttribute15": null,
"Name": "js",
"emailAddress": "[email protected]",
"samAccountName": "js",
"Description": "SYSTEM ADMINISTRATOR",
"MemberOf": [
"CN=group1,OU=admins,DC=stackoverflow,DC=com",
"CN=group2,OU=admins,DC=stackoverflow,DC=com",
"CN=group3,OU=admins,DC=stackoverflow,DC=com"
]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/466221.html
