question

NicolaVenosta-4478 avatar image
0 Votes"
NicolaVenosta-4478 asked NicolaVenosta-4478 action

powershell script lastlogon office 365 and remove licenses

Hello, I am trying this script powershell to work; it remove Office 365 licenses from a csv file I added the rows marked in bold, which should check if the user have had not access in the last 30 days, but the script goes wrong The rows implicated in the error are the bold ones, because without them the script goes well ***

Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -Credential $credential
Connect-AzureAD -Credential $credential
$list = Import-CSV Utenti_da_Disabilitare.csv -delimiter ";"
forEach ($item in $list) {
$userUPN = $item.mail
$userList = Get-AzureADUser -ObjectID $userUPN
$LastLogonTime = Get-EXOMailboxStatistics -Identity $userUPN -Properties LastLogonTime
$OneMonthAgo = (Get-Date).adddays(-30)
if ($LastLogonTime -lt $OneMonthAgo)
{
$Skus = $userList | Select -ExpandProperty AssignedLicenses | Select SkuID
if($userList.Count -ne 0) {
if($Skus -is [array]) {
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
for ($i=0; $i -lt $Skus.Count; $i++) {
$Licenses.RemoveLicenses += (Get-AzureADSubscribedSku | Where-Object -Property SkuID -Value $Skus[$i].SkuId -EQ).SkuID
}
Set-AzureADUserLicense -ObjectId $userUPN -AssignedLicenses $licenses
}
else{
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$Licenses.RemoveLicenses = (Get-AzureADSubscribedSku | Where-Object -Property SkuID -Value $Skus.SkuId -EQ).SkuID
Set-AzureADUserLicense -ObjectId $userUPN -AssignedLicenses $licenses
}
}
}
}
















windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers