/ Microsoft / Office 365 / Powershell

Reporte de Office 365 para liberar licencias en desuso

Hola a todos!

Mostraré un script para liberar las licencias en desuso del Office 365. Este mostrará los usuarios que no envíen correo desde hace 30 días. Para esto primero te invito a conectarte al powershell del Office 365. Sino sabes como, entra al siguiente link:

Una vez conectado al powershell de Office 365 se procede a ejecutar el el siguiente script:

Para esto se necesita cambiar el NOMBRE_DE_REPORTE  

###REPORT SEND
$dateEnd = get-date
$dateStart = $dateEnd.AddHours(-720)
$start=get-date
Write-Host "INICIA: $start"
Get-mailbox -id * | Where-Object {(Get-MsolUser -UserPrincipalName $_.UserPrincipalName | select -ExpandProperty IsLicensed -ErrorAction SilentlyContinue ) -eq "true"} | ForEach-Object {
#LOGON TIME
$lgMail=$null
$lgAD=$null
#FLAG LOGON TIME
$lgMailF=$null
$lgADF=$null
$Warn=$null
$tokenRefresh=$null
$tokenRefresh=$_.StsRefreshTokensValidFrom
$email=$_.WindowsEmailAddress
$create=$_.WhenCreated
$country=$_.UsageLocation
$Sent="BAD"
#LICENSE
$license=Get-MsolUser -UserPrincipalName $_.UserPrincipalName | select -ExpandProperty Licenses | select -ExpandProperty AccountSkuId;
$key="null"
if($license -like '*ENTERPRISEWITHSCAL*'){$key="E4"}
if($license -like '*ENTERPRISEPACK*'){$key="E3"}
if($license -like '*STANDARDPACK*'){$key="E1"}
if ($create -le $dateStart){
#GET LAST MESSAGE
$flagSendMsg = Get-MessageTrace -StartDate $dateStart -EndDate $dateEnd -SenderAddress $email | Select-Object Received, SenderAddress, RecipientAddress, Subject, Status, ToIP, FromIP, Size, MessageID, MessageTraceID | Select-Object -Last 1 -ExpandProperty Received
if ($flagSendMsg -ge $dateStart){
$Sent="GOOD"
#Write-Host $Sent
}else{
#Write-Host $Sent
#SHOW RESULT
get-mailboxstatistics -id $_.UserPrincipalName | select DisplayName, LastLogonTime,@{Name="SentMail"; Expression={$Sent}}, @{Name="email"; Expression={$email}},@{Name="created"; Expression={$create}},@{Name="country"; Expression={$country}},@{Name="license"; Expression={$key}}
#LOGGING
$timing=get-date
Write-Host "USUARIO: $email HORA: $timing ESTADO: $Sent"
}
}
} | Export-Csv c:reportsReportSend24.csv -Encoding UTF8
$end=get-date
Write-Host "TERMINA: $end"

  Hasta pronto!