Forum Discussion
robertglass
Sep 01, 2021Copper Contributor
MFA for users from ou to csv file
hi i am trying to modify my mfa script to include more than the upn and if MFA is enabled and wanted to include the description and when created date from AD. unfortunately it creates the CSV files w...
robertglass
Sep 02, 2021Copper Contributor
thijoubertold hi thanks for looking at this i made the changes as you suggested but still the CSV file remains empty with none of the user information written did you make any other changes. here is what i have now.
$Users = Get-ADUser -Filter * -SearchBase 'OU=test,DC=Dc,DC=net' -Properties UserPrincipalName,description,whencreated|Select-Object -ExpandProperty UserPrincipalName
$Report = [System.Collections.Generic.List[Object]]::new() # Create output file
Write-Host "Processing" $Users.Count "accounts..."
foreach( $User in $Users ){
$ReportLine = [PSCustomObject] @{
User = $user.UserPrincipalName
Name = $user.DisplayName
created = $user.whencreated
Jobtitle = $user.description
MFAstatus = $user.StrongAuthenticationRequirements.State
}
$Report.Add($ReportLine)
}
$Report | Select User, Name, created, Jobtitle, MFAstatus | Sort Name | Out-GridView
$Report | Sort Name | Export-Csv -Path 'C:\Temp\UK-MFA Report.csv' -NoTypeInformation
thijoubertold
Sep 02, 2021Iron Contributor
Hi robertglass
I just saw that you collected your users from your local AD (Get-ADUser) and not from (Get-MsolUser).
The script below gave me the expected results.
If you want to keep Get-ADuser, you should add a step to request the details of MFA from Azure AD / O365 with a local user.
# $Users = Get-ADUser -Filter * -SearchBase 'OU=test,DC=Dc,DC=net' -Properties UserPrincipalName,description,whencreated|Select-Object -ExpandProperty UserPrincipalName
$Users = Get-MsolUser
$Report = [System.Collections.Generic.List[Object]]::new() # Create output file
Write-Host "Processing" $Users.Count "accounts..."
foreach( $User in $Users ){
$ReportLine = [PSCustomObject] @{
User = $user.UserPrincipalName
Name = $user.DisplayName
created = $user.whencreated
Jobtitle = $user.description
MFAstatus = $user.StrongAuthenticationRequirements.State
}
$Report.Add($ReportLine)
}
$Report | Sort User | Format-Table
# $Report | Select User, Name, created, Jobtitle, MFAstatus | Sort Name | Out-GridView
# $Report | Sort Name | Export-Csv -Path 'C:\Temp\UK-MFA Report.csv' -NoTypeInformation
- robertglassSep 02, 2021Copper Contributorhi thanks for this and it does work well but i really need to be able to include the description and when created attributes from AD also on this report which is not provided using the MSOLuser command alone.
- thijoubertoldSep 02, 2021Iron Contributor
robertglass I added a step line 10 and modified line 17
Is it ok for you?
Users = Get-ADUser -Filter * -SearchBase 'OU=test,DC=Dc,DC=net' -Properties UserPrincipalName,description,whencreated|Select-Object -ExpandProperty UserPrincipalName $Report = [System.Collections.Generic.List[Object]]::new() # Create output file Write-Host "Processing" $Users.Count "accounts..." foreach( $User in $Users ){ # Newline $MsolUser = Get-MsolUser -UserPrincipalName $User.UserPrincipalName $ReportLine = [PSCustomObject] @{ User = $user.UserPrincipalName Name = $user.DisplayName created = $user.whencreated Jobtitle = $user.description MFAstatus = $MsolUser.StrongAuthenticationRequirements.State } $Report.Add($ReportLine) } $Report | Sort User | Format-Table # $Report | Select User, Name, created, Jobtitle, MFAstatus | Sort Name | Out-GridView # $Report | Sort Name | Export-Csv -Path 'C:\Temp\UK-MFA Report.csv' -NoTypeInformation