I think the part that is getting messed up in the conversion is from placing Get_MgDevice in this section here:
$Global:LastLogon = datetime.AddDays(- $ThresholdDays)
$Date=(“{0:s}” -f (get-date)).Split(“T”)[0] -replace “-”, “”
$Time=(“{0:s}” -f (get-date)).Split(“T”)[1] -replace “:”, “”
$LastLogin = (“{0:s}” -f ($LastLogon)).Split(“T”)[0] -replace “-”, “”
$WorkSheetName = “AADDevicesOlderthan-” + $LastLogin
if ($Verify){
Write-Host “Verifing stale devices older than”$Global:LastLogon -ForegroundColor Yellow
$FileReport = “AzureADDevicesList_” + $Date + $Time + “.xlsx”
$DeviceReport = Get-MgDevice -All:$true | Where {($.ApproximateLastLogonTimeStamp -le $Global:LastLogon) -and ($.ApproximateLastLogonTimeStamp -ne $Null)} | Select-Object -Property DisplayName, AccountEnabled, ObjectId, DeviceOSType, DeviceOSVersion, DeviceTrustType, ApproximateLastLogonTimestamp
$DeviceReport | Export-Excel -workSheetName $WorkSheetName -path $FileReport -ClearSheet -TableName “AADDevicesTable” -AutoSize
$Global:AffectedDevices = $DeviceReport.Count
Write-Host “Verification Completed.” -ForegroundColor Green -BackgroundColor Black
}elseif ($VerifyDisabledDevices){
Write-Host “Verifing stale disabled devices older than”$Global:LastLogon -ForegroundColor Yellow
$FileReport = “DisabledDevices_” + $Date + $Time + “.xlsx”
$DeviceReport = Get-MgDevice -All:$true | Where {($.ApproximateLastLogonTimeStamp -le $Global:LastLogon) -and ($.ApproximateLastLogonTimeStamp -ne $Null) -and ($.AccountEnabled -eq $false)} | Select-Object -Property DisplayName, AccountEnabled, ObjectId, DeviceOSType, DeviceOSVersion, DeviceTrustType, ApproximateLastLogonTimestamp
$DeviceReport | Export-Excel -workSheetName $WorkSheetName -path $FileReport -ClearSheet -TableName “AADDevicesTable” -AutoSize
$Global:AffectedDevices = $DeviceReport.Count
Write-Host “Task Completed Successfully.” -ForegroundColor Green -BackgroundColor Black
}elseif ($DisableDevices){
Write-Host “Disabling stale devices older than”$Global:LastLogon -ForegroundColor Yellow
$FileReport = "DisabledDevices" + $Date + $Time + “.xlsx”
$DeviceReport = Get-MgDevice -All:$true | Where {($.ApproximateLastLogonTimeStamp -le $Global:LastLogon) -and ($.ApproximateLastLogonTimeStamp -ne $Null) -and ($.AccountEnabled -eq $true)} | Select-Object -Property DisplayName, AccountEnabled, ObjectId, DeviceOSType, DeviceOSVersion, DeviceTrustType, ApproximateLastLogonTimestamp
foreach ($Device in $DeviceReport) {
Update-MgDevice -ObjectId $Device.ObjectId -AccountEnabled $false
}
$DeviceReport | Export-Excel -workSheetName $WorkSheetName -path $FileReport -ClearSheet -TableName “AADDevicesTable” -AutoSize
$Global:AffectedDevices = $DeviceReport.Count
Write-Host “Task Completed Successfully.” -ForegroundColor Green -BackgroundColor Black
}elseif ($CleanDisabledDevices){
Write-Host “Cleaning STALE DISABLED devices older than”$Global:LastLogon -ForegroundColor Yellow
$FileReport = "CleanedDevices" + $Date + $Time + “.xlsx”
$DeviceReport = Get-MgDevice -All:$true | Where {($.ApproximateLastLogonTimeStamp -le $Global:LastLogon) -and ($.ApproximateLastLogonTimeStamp -ne $Null) -and ($_.AccountEnabled -eq $false)} | Select-Object -Property DisplayName, AccountEnabled, ObjectId, DeviceOSType, DeviceOSVersion, DeviceTrustType, ApproximateLastLogonTimestamp
foreach ($Device in $DeviceReport) {
Remove-MgDevice -ObjectId $Device.ObjectId
}
$DeviceReport | Export-Excel -workSheetName $WorkSheetName -path $FileReport -ClearSheet -TableName “AADDevicesTable” -AutoSize
$Global:AffectedDevices = $DeviceReport.Count
Write-Host “Task Completed Successfully.” -ForegroundColor Green -BackgroundColor Black
}elseif ($CleanDevices){
Write-Host “Cleaning STALE devices older than”$Global:LastLogon -ForegroundColor Yellow
$FileReport = “CleanedDevices_” + $Date + $Time + “.xlsx”
$DeviceReport = Get-MgDevice -All:$true | Where {($.ApproximateLastLogonTimeStamp -le $Global:LastLogon) -and ($.ApproximateLastLogonTimeStamp -ne $Null)} | Select-Object -Property DisplayName, AccountEnabled, ObjectId, DeviceOSType, DeviceOSVersion, DeviceTrustType, ApproximateLastLogonTimestamp
foreach ($Device in $DeviceReport) {
Remove-MgDevice -ObjectId $Device.ObjectId
}
$DeviceReport | Export-Excel -workSheetName $WorkSheetName -path $FileReport -ClearSheet -TableName “AADDevicesTable” -AutoSize
$Global:AffectedDevices = $DeviceReport.Count
Write-Host “Task Completed Successfully.” -ForegroundColor Green -BackgroundColor Black
}else{
Write-Host “Operation aborted. You have not select any parameter, please make sure to select any of the following parameters:” -ForegroundColor Red
Write-Host "