![](/img/trans.png)
[英]Searching Active Directory and exporting a list of names in mulitpule groups to a txt file for powershell
[英]Active Directory Powershell to fixed width txt file
嘗試將4個對象從Ad導出到不帶標題的固定寬度txt文件中。
我需要以下幾欄為隨后的寬度。
Employee ID 10
Work Phone 10
Work Phone Extension 5
Work Email Address 50
User ID 20
以下內容為我提供了最好的輸出,但是並沒有按照我需要的方式調整列的大小。 我一直在挖掘,認為我需要的東西超出了我所能適應的范圍。
我不確定是否需要使用export-csv導出,然后將其導入重新格式化,或者是否可以直接進行文件外輸出。
$DateTime = Get-Date -f "yyyyMMdd"
#// Set CSV file name
$CSVFile = "d:\scripts\workday\int002_"+$DateTime+".txt"
Get-ADGroup -Filter {(name -like "*Group Name*")} `
| Get-ADGroupMember -Recursive | Where { $_.objectClass -eq "user" } `
| Get-ADUser -properties * | where {$_.enabled -eq $true} `
| select employeeid,telephoneNumber,mail,sAMAccountName -unique | FT employeeid,telephoneNumber,mail,sAMAccountName -hidetableheaders -autosize | out-file $CSVFile
樣本輸出:
8855 2122445710 xxxry.michalsen@companydomain.com michalsenm
您可能需要手動執行...
$result = foreach($user in $users) {
$user.employeeid.PadRight(10),
$user.telephoneNumber.PadRight(10),
$user.mail.PadRight(50),
$user.sAMAccountName.PadRight(20) -join ' '
}
$result | Out-File $CSVFile
如果屬性不是字符串,也可以使用的修訂版本:
$result = foreach($user in $users) {
'{0,-10}{1,-10}{2,-50}{3,-20}' -f
$user.employeeid,
$user.telephoneNumber,
$user.mail,
$user.sAMAccountName
}
$result | Out-File $CSVFile
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.