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 "