Jan 30 2023 04:59 AM - edited Feb 26 2023 02:26 AM
I need to obtain data from the Microsoft cloud by connecting to the online exchange, AzureAD and Office 365 with the following commands in a single script and to deliver it consolidated in a single report. Can someone give me a hand and tell me what I have wrong in my script or how to improve it to get the data from those 3 different sources. Thank you!!
for example:
My PS script:
---------------------------------------------------------------------------------------------------------------
#Connect-MsolService
#Connect-ExchangeOnline
#Connect-AzureAD
$aux = Get-MsolUser -all
#$aux = Get-azureaduserextension -ObjectId -
#$aux = Get-EXORecipient -PropertySets Custom
$total = ($aux).count
$usuarios = @()
$usuario = New-Object System.Object
$usuario | Add-Member -MemberType NoteProperty -Name "UPN" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "displayname" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "employeeID" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "BU" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "blockcredential" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "isLicenced" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "usagelocation" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "Dominio" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "Pais" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "ATP_ENTERPRISE" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "EMS" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "STANDARDPACK" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "ENTERPRISEPACK" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "ENTERPRISEPREMIUM" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "SPE_F1" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "PROJECTPROFESSIONAL" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "PROJECTPREMIUM" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "PROJECTESSENTIALS" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "POWER_BI_PRO" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "VISIOONLINE_PLAN1" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "VISIOCLIENT" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "AAD_PREMIUM" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "PROJECTCLIENT" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "ENTERPRISEPACKWITHOUTPROPLUS" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "PROJECTONLINE_PLAN_1" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "POWERAPPS_INDIVIDUAL_USER" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "STREAM" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "EMSPREMIUM" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "FLOW_FREE" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "MICROSOFT_BUSINESS_CENTER" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "MCOEV" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "SKU_Dynamics_365_for_HCM_Trial" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "POWERAPPS_VIRAL" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "DYN365_ENTERPRISE_P1_IW" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "DYN365_ENTERPRISE_CUSTOMER_SERVICE" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "POWER_BI_STANDARD" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "ENTERPRISEPREMIUM_NOPSTNCONF" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "ENTERPRISEWITHSCAL" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "POWERAUTOMATE_ATTENDED_RPA" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "POWERAUTOMATE_PER_USER" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "POWERAPPS_PER_APP_IW" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "POWERAPPS_PER_APP" -Value "1"
$usuario | Add-Member -MemberType NoteProperty -Name "POWERAPPS_PER_USER" -Value "1"
$usuarios+= $usuario
foreach ($user in $aux)
{
$u=$user
$i=0
if($u)
{
Write-Progress -Activity "Procesando ($i) de $total Usuarios." -Status "Porcentaje de Avance..." -PercentComplete ((($i)/ $total)*100)
$usuario = New-Object System.Object
$usuario | Add-Member -MemberType NoteProperty -Name "UPN" -Value $u.UserPrincipalName
$usuario | Add-Member -MemberType NoteProperty -Name "displayname" -Value $u.displayname
$usuario | Add-Member -MemberType NoteProperty -Name "employeeID" -Value $u.employeeID #This "employeeID" field is obtained through a "Connect-AzureAD" connection, specifically
#with the command "get-azureaduserextension -objectid "
$usuario | Add-Member -MemberType NoteProperty -Name "BU" -Value $u.CustomAttribute5 #This field "" can be obtained by connecting to "Connect-ExchangeOnline" specifically
#with the command "Get-EXORecipient –Identity -PropertySets Custom"
$usuario | Add-Member -MemberType NoteProperty -Name "blockcredential" -Value $u.BlockCredential
$usuario | Add-Member -MemberType NoteProperty -Name "isLicenced" -Value $u.IsLicensed
$usuario | Add-Member -MemberType NoteProperty -Name "usagelocation" -Value $u.usagelocation
$dominio = $u.UserPrincipalName.Split("@")[1]
$usuario | Add-Member -MemberType NoteProperty -Name "Dominio" -Value $dominio
if($dominio -eq "ACME.com")
{
$usuario | Add-Member -MemberType NoteProperty -Name "Pais" -Value "pe"
}else{
$usuario | Add-Member -MemberType NoteProperty -Name "Pais" -Value $dominio.Split(".")[-1]
}
if($u.Licenses[0])
{
while($u.Licenses[$i].AccountSku.SkuPartNumber)
{
$usuario | Add-Member -MemberType NoteProperty -Name $u.Licenses[$i].AccountSku.SkuPartNumber -Value $u.Licenses[$i].AccountSku.SkuPartNumber
$i++
}
}else{
#unlicenced
}
$usuarios += $usuario
}else{
#not Found
}
}
$usuarios | Export-Csv -NoTypeInformation -Path "C:\scripts\O365\Reportes_O365\LicenciasAsignadas_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
Feb 10 2023 06:50 AM
Hello @Miguel_Alegria,
You can use something like this:
Connect-MsolService
Connect-ExchangeOnline
Connect-AzureAD
$aux = Get-MsolUser -all
$total = ($aux).count
$usuarios = @()
$usuarios= New-Object System.Collections.Generic.List[PSObject]
foreach ($user in $aux)
{
$u=$user
$i=0
if($u)
{
Write-Progress -Activity "Procesando ($i) de $total Usuarios." -Status "Porcentaje de Avance..." -PercentComplete ((($i)/ $total)*100)
$EmployeeID=$(Get-AzureADUserExtension -objectid $u.ObjectId).EmployeeId
$BU=(Get-EXORecipient –Identity $u.UserPrincipalName -PropertySets Custom).CustomAttribute5
$Pais=if($u.UserPrincipalName.Split("@")[1] -eq "ACME.com"){
"pe"
}else{
$u.UserPrincipalName.Split("@")[1].Split(".")[-1]
}
$usuario = $u | Select-Object @{l="UPN";e={$u.UserPrincipalName}},DisplayName,@{l="BU";e={$BU}},BlockCredential,IsLicensed,UsageLocation,@{l="Dominio";e={$u.UserPrincipalName.Split("@")[1]}}, @{l="Pais";e={$Pais}}
if($u.Licenses){
while($u.Licenses[$i].AccountSku.SkuPartNumber)
{
$usuario | Add-Member -MemberType NoteProperty -Name $u.Licenses[$i].AccountSku.SkuPartNumber -Value $u.Licenses[$i].AccountSku.SkuPartNumber
$i++
}
}
$usuarios.Add($usuario)
}
}
$usuarios | Export-Csv -NoTypeInformation -Path "C:\scripts\O365\Reportes_O365\LicenciasAsignadas_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
Hope that helps.
Feb 26 2023 01:56 AM
Hello!! @AndySvints
Thank you very much for your answer (you were the only one who answered me hehehe).
Did you test your proposal or did you write it so that from it I can take some element to build an improvement for my original script?
I tried the one you sent me but I see several messages. If you could shed some light on why these messages appear, I would really appreciate it.
Feb 27 2023 06:57 AM
Hello @Miguel_Alegria,
To be honest, I did not execute the full script. I've tested some bits and pieces of it.
The error that you receiving looks like to be related to the last line and escape character which I've added accidentally. Also, I've noticed that EmployeeID was not added to the object.
Here is the updated code:
Connect-MsolService
Connect-ExchangeOnline
Connect-AzureAD
$aux = Get-MsolUser -all
$total = ($aux).count
$usuarios = @()
$usuarios= New-Object System.Collections.Generic.List[PSObject]
foreach ($user in $aux)
{
$u=$user
$i=0
if($u)
{
Write-Progress -Activity "Procesando ($i) de $total Usuarios." -Status "Porcentaje de Avance..." -PercentComplete ((($i)/ $total)*100)
$EmployeeID=$(Get-AzureADUserExtension -objectid $u.ObjectId).EmployeeId
$BU=(Get-EXORecipient –Identity $u.UserPrincipalName -PropertySets Custom).CustomAttribute5
$Pais=if($u.UserPrincipalName.Split("@")[1] -eq "ACME.com"){
"pe"
}else{
$u.UserPrincipalName.Split("@")[1].Split(".")[-1]
}
$usuario = $u | Select-Object @{l="UPN";e={$u.UserPrincipalName}},DisplayName,@{l="EmployeeID";e={$EmployeeID}},@{l="BU";e={$BU}},BlockCredential,IsLicensed,UsageLocation,@{l="Dominio";e={$u.UserPrincipalName.Split("@")[1]}}, @{l="Pais";e={$Pais}}
if($u.Licenses){
while($u.Licenses[$i].AccountSku.SkuPartNumber)
{
$usuario | Add-Member -MemberType NoteProperty -Name $u.Licenses[$i].AccountSku.SkuPartNumber -Value $u.Licenses[$i].AccountSku.SkuPartNumber
$i++
}
}
$usuarios.Add($usuario)
}
}
$usuarios | Export-Csv -NoTypeInformation -Path "C:\scripts\O365\Reportes_O365\LicenciasAsignadas_$((Get-Date -format yyyy-MMM-dd-ddd hh-mm tt).ToString()).csv"
Hope that helps.