PowerShell - Collect VT Device list, APResults info of a folder and email them

Collect VT Device list, APResults info of a folder and email them

Written by Ines

Last published at: January 19th, 2024

Script events:

  1. discovers all devices created by the Validation tool within a target (landing) folder
  2. checks the ApResults of each device and saves it as a txt file within a target folder
  3. Sends email with the body containing the date and time of running the script, and basic Devices information, with the attachment of the APresults.


Device Info in the body:



APResults txt.file:

Script:


Delete

Warning!

The Send-MailMessage cmdlet is obsolete. This cmdlet doesn't guarantee secure connections to SMTP servers. While there is no immediate replacement available in PowerShell, we recommend you do not use Send-MailMessage. For more information, see This Microsoft Article for further details.


import-module ThinScaleManagement

# provide server details and credentials

$creds = Get-Credential

$serverURI = 'SERVER'

Connect-TSTMGMTServer -Uri $serverURI -Credentials $creds


#parameters

$Folder = "\Devices\Folder_of_interest"  #set default folder name

$NOW = Get-Date -Format dd-MM-yyyy--hh-mm  #current time for the email details

$dir = "C:\Temp"

$file = "$dir\VTresults_$NOW.txt" #APresults file output

$FolderId = (Get-TSTMGMTFolder -FolderName $Folder).ObjectId  #target folder

$DeviceID = Get-TSTMGMTDevices -FolderId $FolderId | where {($_.ProductType -eq 'ValidationTool')}  #discovering all VT devices in the specified folder 

#finding details on the devices


$DeviceInfo = $DeviceID | Foreach {

 $dID = $_.ObjectId

 $fID = $_.FolderId

 $prID = $_.ActiveProfileId

$fName = Get-TSTMGMTFolder -FolderId $fID

$pName = Get-TSTMGMTProfile -ProfileId $prID

Get-TSTMGMTDevice -DeviceId $dID | Select DeviceName, ObjectId, ProductType, ProductVersion, @{n='Profile';e={$pName.ProfileName}}, ActiveProfileRevision, @{n='Folder';e={$fName.FolderName}}, LastUser, LastBootTime,LastHeardFrom,LastUsed  | Sort ResultTime -Descending

}

#AP Results info

$APR = $DeviceID | Foreach {

 $dID = $_.ObjectId

Get-TSTMGMTAPResults -DeviceId $dID

}


if ((Get-ChildItem $dir -ErrorAction SilentlyContinue)){

$APR | Out-File $file #export the results

}

else{

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

    $APR | Out-File $file #export the results

}



#email format

$css = @"

<style>

h1, h5, th { text-align: left; font-family: Segoe UI; }

table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }

th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }

td { font-size: 11px; padding: 5px 20px; color: #000; }

tr { background: #b8d1f3; }

tr:nth-child(even) { background: #dae5f4; }

tr:nth-child(odd) { background: #b8d1f3; }

</style>

"@

# Send Email Report

$SMTPServer = "smtp.domain.com"

$body = "<h2>APResults</h2> <p>$NOW</p>" +($DeviceInfo | ConvertTo-Html -Head $css | Out-String)

# Mail variables

$toAddress = "receiver@domain.com"

Send-MailMessage -From "APResults@domain.com" -To $toAddress -Subject "APResults - $Now" -BodyasHtml -Body $body -Attachments $file -SmtpServer $SMTPServer