PowerShell - Delete Devices using LastHeardFrom
Delete Devices using LastHeardFrom.
This cmdlet will delete devices if the LastHeardFrom is less than X days.
$creds = Get-Credential
Connect-TSTMGMTServer -uri "https://thinscale.local/tstmgmt" -Credentials $creds
$date = (get-date).AddDays(-30) #Set date limit
$DeviceID = Get-TSTMGMTDevices | where {$_.LastHeardfrom -lt $date} | select DeviceName, LastHeardfrom, ObjectId #discover devices older than set date
$DeviceID | Out-GridView
Write-Host "Discovering Date and Device..." -ForegroundColor Green
$allData = Get-TSTMGMTAllData
foreach ($device in $allData.Devices | Where-Object {$_.LastHeardfrom -lt $date })
{
Write-Host
$confirmation = Read-Host "Are you Sure You Want To Delete The Device " $device.DeviceName "(ProductVersion: " $device.ProductVersion ", LastHeardfrom: " $device.LastHeardfrom " )"
if ($confirmation -eq 'y') {
Remove-TSTMGMTObject -objectId $device.ObjectId -objectType 'Device'
}else{
write-Host "No, skip removal." -ForegroundColor Yellow
}
}