Hi Everyone,
I am trying to export the output of creating new azure AD users to a csv file.
This is what I’ve come up with so far:
$users = Import-Csv “C:\test\users.csv”
#Create Password
Function Generate-Password(){
Param($max = 1)
For ($i = 0; $i -lt $max; $i++){
$pw = Get-Random -Count 1 -InputObject ((65..72)+(74..75)+(77..78)+(80..90)) | % -begin {$UC=$null} -process {$UC += [char]$_} -end {$UC}
$pw += Get-Random -Count 2 -InputObject (97..122) | % -begin {$LC=$null} -process {$LC += [char]$_} -end {$LC}
$pw += Get-Random -Count 2 -InputObject (48..57) | % -begin {$NB=$null} -process {$NB += [char]$_} -end {$NB}
$pw += Get-Random -Count 3 -InputObject (97..122) | % -begin {$LC=$null} -process {$LC += [char]$_} -end {$LC}
write-output $pw
}
}
#Create new AzureAD User
$users = Import-Csv “C:\test\users.csv”
$mail = ($user.Firstname + “.” + $user.lastname).ToLower()
$PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$password = Generate-Password 1
$PasswordProfile.Password = $password
$users | ForEach-Object {New-AzureADUser -GivenName $user.Firstname `
-MailNickName $mail`
-UserPrincipalName $user.Username`
-DisplayName $user.DisplayName`
-Surname $user.lastname`
-JobTitle $user.JobTitle`
-Department $user.Department`
-AccountEnabled $true `
-PasswordProfile $PasswordProfile} | Export-Csv -Path “C:\test\output.csv” -NoTypeInformation
I did originally try to use a foreach loop but that does not use the pipeline to get the data out. Hence why I’m using the ForEach-Object commandlet. I think my issue is that the variables prior to running the ForEach-Object need to be parsed but don’t think I’m doing it right.
Any help much appreciated.
Darren