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: October 6th, 2021

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:

import-module ThinScaleManagement


Get-TSTMGMTRefreshData


# provide server details and credentials

$creds = Get-Credential

$serverURI = 'SERVER'

Connect-TSTMGMTServer -Uri $serverURI -Credentials $creds


#parameters

$Folder = "\Devices\Client 1 Landing Folder"  #set default folder name

$file = "C:\Temp\VTresults_$NOW.txt" #APresults file output




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


$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




}


$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