简体   繁体   中英

Create a CSV with all AD users to import into Elastix

I'm working in a test environment with about 1000 users and I'm trying to create a CSV with the following headers to be used in Elastix: Display Name, User Extension, Secret, Tech.

The users currently do not have extensions assigned to them and random extensions would be fine. The secret will be "123456" for all of them. The Tech will be "Sip" for all of them.

Currently I have this, but I'm struggling to strip the name off each user in my loop:

$users = get-aduser -filter * | Select Name
$outpath = "C:\scripts\users.csv"
$outputArray =@()
"Display Name, User Extension, Secret, Tech"|out-file $outpath -Force
$ext = 1000
foreach($row in $users)
{
    $outputArray +=  "," + $ext++ + "," + "123456" + "," + "Sip"
}

$outputArray | out-file $outpath -Force

Use can do this using the Select-Object cmdlet with 'calculated properties' and the Export-Csv cmdlet.

(Note: In my original answer I did not specify the script scope for the randomExtension variable when modified in an expression. When trying to fix, I found the solution here: How can I increase the value of a variable in an expression?

$outputPath = "C:\test\users.csv"
$script:randomExtension = 1000
get-aduser -filter * | 
Select @{n='Display Name';e={$_.Name}}, 
   @{n='User Extension';e={$script:randomExtension++; $script:randomExtension}},
   @{n='Secret';e={'123456'}},
   @{n='Tech';e={'Sip'}} |
Export-Csv -Path $outputPath -NoTypeInformation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM