[英]Powershell CSV ordered Export with specific columns
我有以下問題:
我有一個 Get-ADUser 查詢,並且有特定的字段可以獲取(僅測試查詢)
$AllADUsers = get-aduser -Identity m.mustermann -Properties * | select mail, title, l, GivenName, Surname, company, department, mobile
此外,我必須在 CSV 中添加一些 static 字段如何語言或注釋,我為此采取了一些變量:
$Language = "german"
$Comment = ""
$Link = ""
$gender = ""
$exportto = "C:\Temp\Export01.csv"
比我想導出帶有分隔符“:”的有序 UTF8 CSV 中的所有條目。 我做了一個 foreach 但我總是出錯或不完整的列表 - 我向你展示我的代碼(但我有錯誤)
Foreach ($ADUser in $AllADUsers) {
$MailAddress = $ADUser.mail
$FullName = "$ADUser.GivenName" + "$ADUser.Surname"
$Title = $AdUser.Title
$Location = $ADUser.l
$Comment = $Comment
$Link = $Link
$MobilePhone = $ADUser.mobile
$Language = $Language
$Divison = $ADUser.Department
$GivenName = $ADUser.GivenName
$Surname = $ADUser.Surname
}
$ADUser | Export-CSV -Path $exportto -NoTypeInformation -Delimiter ":" -Encoding UTF8
你能幫助我嗎?
導出必須帶有有序列表,並且沒有“”,並且僅按此順序。
謝謝你。 最大限度
您的代碼中有一些高級問題。
在您的代碼中,您應該將值分配給$AdUser
的特定屬性。 就像是:
foreach ($ADUser in $AllADUsers) {
$ADUser.FullName = "$($ADUser.GivenName) $($ADUser.Surname)"
$ADUser.Comment = $Comment
$ADUser.Link = $Link
}
目前你只導出最后一個條目,你應該 pipe $AllADUsers
到Export-Csv
:
$AllADUsers| Export-CSV -Path $exportto -NoTypeInformation -Delimiter ":" -Encoding UTF8
通常,您可以將代碼縮短為單行代碼,例如:
$AllADUsers | Select-Object @{n="Comment";{$Comment}},@{n="FullName";e={"$($ADUser.GivenName) $($ADUser.Surname)"}} ... | Export-CSV ...
您的代碼不起作用,主要是因為您正在創建一系列最終不使用的變量。
到 output 到 CSV,您需要收集具有屬性的對象,而不是單個變量。 一種非常優雅的方法是使用[PsCustomObject]
語法創建這些對象,同時您設置屬性的順序彌補了最終 output 文件中的順序。
使用冒號作為分隔符是不常見的,當其他應用程序需要閱讀和理解它時可能會給您帶來麻煩,但這取決於您。
另一件事是,您似乎主要使用 LDAP 屬性名稱,其中 PowerShell 大部分映射到更友好的屬性名稱(例如映射到 LDAP 'l')的屬性名稱。
您的代碼已修改:
# Get-ADUSer already returns these properties by default:
# DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
# your hardcoded static variables:
$Language = "german"
$Comment = ""
$Link = ""
$gender = ""
$exportto = "C:\Temp\Export01.csv"
# these are the extra properties you want to fetch
$userprops = 'EmailAddress','Title','Company','City','Department','MobilePhone'
# if you want this for all users, remove "-Identity 'm.mustermann'" and use "-Filter *" instead
$result = Get-ADUser -Identity 'm.mustermann' -Properties $userprops | ForEach-Object {
# output an object to be collected in variable $result
# the order in which you put them also determines the field order in the CSV
[PsCustomObject]@{
MailAddress = $_.EmailAddress
FullName = $_.DisplayName
Title = $_.Title
Company = $_.Company
Location = $_.City
Comment = $Comment # static field
Link = $Link # static field
MobilePhone = $_.MobilePhone
Language = $Language # static field
Department = $_.Department
GivenName = $_.GivenName
Surname = $_.Surname
}
}
$result | Export-CSV -Path $exportto -NoTypeInformation -Delimiter ":" -Encoding UTF8
關於Export-Csv
的說明:
Export-Csv
將始終引用所有內容,無論是 header 還是數據字段。
簡單地嘗試刪除 CSV 文件中的所有引號可能會導致字段順序錯位並使 csv 無法使用。
使用 PowerShell 版本 7,您可以選擇使用參數-UseQuotes AsNeeded
,但對於舊版本,您可以使用我的 function ConvertTo-CsvNoQuotes安全地執行此操作
由於這段代碼都是關於獲取AD 信息的,我不明白你為什么要在這里構造 FullName。 只需像上面的代碼一樣使用在 AD 中設置的 DisplayName 屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.