我有一個包含以下標題名稱的 CSV 檔案:
姓名、日期、所有者
我想使用 PowerShell
- 匯入包含標題值“名稱”、“日期”、“所有者”、“額外”的 File1.csv
- 閱讀“所有者”列(包含 samaccountnames)并使用它來查詢 AD 以查找與每個用戶關聯的“部門、部門”屬性。
- 創建兩個名為“Department”和“Division”的新列,然后將 CSV 匯出到新的 CSV (file2.csv)
這是我到目前為止的代碼:
$file1 = Import-csv -path "C:\temp\File1.csv"
ForEach ($record in $file1) {
$getAttributes = Get-ADUser -Filter $record.Owner | Select-Object division,department
$record | Add-Member -MemberType NoteProperty -Name "Division" -Value $getAttributes.Division
$record | Add-Member -MemberType NoteProperty -Name "Department" -Value $getAttributes.Department
$record | Export-Csv -Path C:\temp\file2.csv -Encoding UTF8 -NoTypeInformation -Append
}
我嘗試了不同的變化,到目前為止沒有任何東西對我有用。任何援助將不勝感激!
uj5u.com熱心網友回復:
您的代碼的兩個主要問題是:
-Filter $record.Owner
不是ActiveDirectory Filter的有效語法。Get-ADUser
不回傳具有屬性 Department 和 Division 的物件,除非您特別要求查詢它們 (-Properties Department, Division
)。
Import-csv -Path "C:\temp\File1.csv" | ForEach-Object {
$usr = Get-ADUser -LDAPFilter "(samaccountname=$($_.Owner))" -Properties Department, Division
if(-not $usr) {
# if the user could not be found skip it
Write-Warning "'$($_.Owner)' could not be found..."
return
}
# recreate this object (`$_`) with 2 new properties, `Division` and `Department`
$_ | Select-Object *, @{N='Division';E={$usr.Division}}, @{N='Department';E={$usr.Department}}
} | Export-Csv -Path C:\temp\file2.csv -Encoding UTF8 -NoTypeInformation
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/475899.html