PowerShell - Logfile with devices LastUsed less than X

Logfile with devices LastUsed less than X

Written by Ines

Last published at: January 27th, 2023

Script Events:

  1. Discover devices LastUsed before X days
  2. Create log directory
  3. Export details into text file to log directory


Log example:

Script:

#parameters


$LogDest = "C:\Temp\ThinScale"    #Path to where the log files are saved with list of disabled and deleted machines

$date = (get-date).AddDays(-30)       #specify date limit for removing machines (number in brackets counts back the days from today)


### connect to Mgmt Server

$creds = Get-Credential

Connect-TSTMGMTServer -Uri "http://SERV2019.thinscale.local/TSTMgmt" -Credentials $creds


    ### discover devices


$Devices = Get-TSTMGMTDevices | where {($_.LastUsed -lt $date)}

    ### create logs in C:\Temp\ThinScale

    New-Item -Path $LogDest -ItemType Directory -Force -Confirm:$false

    $log = $Devices| 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/LastUsed_$(get-date -Format dd-MM-yyyy--hh-mm).txt