PowerShell - Move devices into Disabled and Uninstalled folders using LastHeardFrom

Move devices into two different folders using LastHeardFrom.

Written by Giuseppe

Last published at: January 27th, 2023

Script Events:

  1. Creates one folder on the Thinscale server that disables devices, and one that uninstalls the SRW or TK software. The folders are created in the root of the Devices folder.
  2. discovers all devices that hadn't been reported to the server for more than 14 days
  3. exports the list of discovered devices and their details into a previously specified destination as txt file
  4. moves those devices into the Disabled folder
  5. discovers all devices in the Disabled folder that hadn't reported to the server for more than 30 days
  6. exports the list of discovered devices and their details into a previously specified destination as txt file
  7. moves those devices into the Uninstall folder


Log file examples:


Script:


#parameters

$OriginalDest = (Get-TSTMGMTFolder -FolderName '\Devices\SRW').ObjectId    #Target destination
$DisabledF = "\Devices\Disabled"
$UninstalledF = "\Devices\Uninstalled"
$LogDest = "C:\Temp\ThinScale"                    #Path to where the log files are saved with list of disabled and deleted machines
$dateDisable = (get-date).AddDays(-14)             #specify date limit for disabling machines
$dateUninstall = (get-date).AddDays(-30)           #specify date limit for removing machines

### connect to Mgmt Server
$creds = Get-Credential
Connect-TSTMGMTServer -Uri "https://thinscale.com/TSTMgmt" -Credentials $creds

### create Disabled device folder
New-TSTMGMTFolder -FolderName $DisabledF -DeviceState Disabled


### create Uninstall device folder
New-TSTMGMTFolder -FolderName $UninstalledF -DeviceState Uninstall


### move devices to Disabled folder
    ### discover devices: LastHeardFrom more than 14 days

$DevicesDisable = Get-TSTMGMTDevices | where {($_.LastHeardFrom -lt $dateDisable)}


    ### create logs in C:\Temp\ThinScale
    New-Item -Path $LogDest -ItemType Directory -Force -Confirm:$false
    $log = $DevicesDisable | select DeviceName, LastUser, ObjectId, @{n='FolderName';e={(Get-TSTMGMTFolder -FolderId $_.FolderId).FolderName}}, ProductType, ProductVersion, @{n='ActiveProfile';
    e={(Get-TSTMGMTProfile -ProfileId $_.ActiveProfileId).ProfileName}}, LastBootTime,LastHeardFrom,LastUsed
    $log | ft | Out-File $LogDest/Disabled_$(get-date -Format dd-MM-yyyy--hh-mm).txt


    ### move devices

    $DevicesDisable | Move-TSTMGMTObject -DestinationFolderName $DisabledF


### move Disabled devices to Uninstalled folder
    ### discover devices: LastHeardFrom more than 30 days

$DisabledFId = (Get-TSTMGMTFolder -FolderName $DisabledF).ObjectId
$DevicesUninstall = Get-TSTMGMTDevices | where {($_.LastHeardFrom -lt $dateUninstall) -and ($_.FolderId -eq $DisabledFId)}


    ### create logs in C:\Temp\ThinScale
    New-Item -Path $LogDest -ItemType Directory -Force -Confirm:$false
    $log = $DevicesUninstall | select DeviceName, LastUser, ObjectId, @{n='FolderName';e={(Get-TSTMGMTFolder -FolderId $_.FolderId).FolderName}}, ProductType, ProductVersion, @{n='ActiveProfile';
    e={(Get-TSTMGMTProfile -ProfileId $_.ActiveProfileId).ProfileName}}, LastBootTime,LastHeardFrom,LastUsed
    $log | ft | Out-File $LogDest/Uninstalled_$(get-date -Format dd-MM-yyyy--hh-mm).txt


    ### move devices

    $DevicesUninstall | Move-TSTMGMTObject -DestinationFolderName $UninstalledF