Delete Device using a Date and a ProductType

Delete Device using a Date and a ProductType

Written by Giuseppe

Last published at: November 4th, 2021

This cmdlet will delete devices if the LastUsed is less than X days and the ProductType matches the ValidationTool

Import-Module ThinScaleManagement
$creds = Get-Credential

Connect-TSTMGMTServer -uri "https://thinscale.com/tstmgmt" -Credentials $creds

$date = (get-date).AddDays(-1)
$DeviceID = Get-TSTMGMTDevices | where {$_.LastUsed -lt $date} | select DeviceName, LastHeardFrom, LastUsed, ObjectId, ProductType
$DeviceID | Out-GridView


Write-Host "Discovering Date and Device..." -ForegroundColor Green

$allData = Get-TSTMGMTAllData

foreach ($device in $allData.Devices | Where-Object {$_.LastUsed -lt $date -and ($_.ProductType -match 'ValidationTool')})
{
Write-Host
    $confirmation = Read-Host "Are you Sure You Want To Delete The DeviceName:"  $device.DeviceName", ProductVersion:" $device.ProductVersion ", ProductType:" $device.ProductType ""
    if ($confirmation -eq 'y') {
      Remove-TSTMGMTObject -objectId $device.ObjectId -objectType 'Device'
    }else{
    write-Host "No, skip removal." -ForegroundColor Yellow
    }
}