Forum Discussion
Miguel_Alegria
Jan 30, 2023Copper Contributor
How to get from 3 different sources in the Microsoft cloud (ExchangeOnline, AzureAD and O365)
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 r...
Miguel_Alegria
Feb 26, 2023Copper Contributor
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.
AndySvints
Feb 27, 2023Iron Contributor
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.