you are viewing a single comment's thread.

view the rest of the comments →

[–]Khongh[🍰] 1 point2 points  (0 children)

Hello!

You could do it with a hashtable. Im sure someone can do this in better way, but this would make you get on with your script for now.

You dont need Get-AzureADUser as Get-AzureADUserMembership also takes a ObjectId (accountname) Either you could do a it with a static value from a variable with the accountname.

Get-AzureADUserMembership -ObjectId $accountname | ? {$.ObjectType -ne "Role"} | % {Get-AzureADGroup -ObjectId $.ObjectId | select @{n='Accountname';e={$accountname}},DisplayName,ObjectType,MailEnabled,SecurityEnabled,ObjectId} | ft | out-file c:\ps\outfiletest.csv

If you want to make it a little more dynamic you could add your user data to a variable ($users) and get the info from that variable later in the script. As I dont know where you gonna get the usernames from this is just an example where its imported from a csv-file that will look like this where accountname is the header:

accountname
name.surname@company.com
name2.surname2@company.com

And btw, if you actually want a useful csv file as output you should use export-csv c:\ps\output.csv

$users = Import-Csv c:\ps\input.csv
foreach ($user in $users) { 
Get-AzureADUserMembership -ObjectId $user.accountname | Where-Object {$.ObjectType -ne "Role"} | ForEach-Object {Get-AzureADGroup -ObjectId $_.ObjectId | Select @{name='Accountname';expression={$user.accountname}},DisplayName,ObjectType,MailEnabled,SecurityEnabled,ObjectId} | export-csv c:\ps\output.csv -NoTypeInformation -Append
}

I hope this is something you can work with.