Product: PowerShell Universal
Version: 5.5.1
Let me start by apologizing for the long post. I’m a Microsoft production DBA and a self professed PowerShell hack. I can do what I’ve needed in PowerShell to this point, but it’s normally very ugly (so please, no jabs at the code). I’ve created an app that works as expected, but I need something to show that it’s progressing. The script queries a set of SQL servers for a login. Depending on the time of day it can take from 3 secs up to 15 secs to run, but during that time there is no indication that it’s doing anything. I think that I need to run the main portion of the script in the background or a separate job maybe. Below is my “hack” code. Any help would be appreciated.
<#
Date: 07-08-24
Written By: Alonzo Hess
Purpose: Check all known SQL servers for terminated logins
#>
Import-Module -name DbaTools
Set-DbatoolsInsecureConnection -SessionOnly
New-UDApp -Title 'Term Check' -Content {
New-UDHtml -Markup '<h1>SQL Term Check Check</h1>'
New-UDForm -Content {
New-UDTextbox -Label "Enter PID" -Id 'PID'
} -onSubmit {
$UPID = $EventData.PID
New-UDTypography -Text "Checking on $UPID"
# Clearing file contents from previous run
clear-content -Path 'C:\esarftemp\All_SQL_Servers.txt'
clear-content -Path 'C:\esarftemp\All_SQL_Servers_1.txt'
clear-content -Path 'C:\esarftemp\All_SQL_Servers_Final.txt'
clear-content -Path 'C:\esarftemp\found_user.txt'
# Variables
$ScriptStart = (Get-Date) # used to determine the time it takes to complete the script
$Server_File = 'C:\esarftemp\All_SQL_Servers.txt'
# Get a list of all SQL servers from the central management instance and save to txt file.
Get-DbaRegServer -SQLInstance SQLCMS | Select-Object -Unique Servername | Out-File $Server_File
# The next 3 commands can be combined into one
# Stripping out the first 3 lines of the server list.
Get-Content $Server_File | Select-Object -Skip 3 | Set-Content 'c:\esarftemp\All_SQL_Servers_1.txt'
Get-Content -Path C:\esarftemp\All_SQL_Servers_1.txt | ForEach-Object { $_.TrimEnd() } | Set-Content C:\esarftemp\All_SQL_Servers_Final.txt
#Get the final "clean" list of servers (have to strip out the blank lines at the end of the file)
$SQLservers = Get-Content C:\esarftemp\All_SQL_Servers_Final.txt | Where-Object { $_.trim() -ne "" }
$SQLServerCount = $SQLservers.Count
#Loop through the server list, connect to each server and query for the PID
ForEach ($server in $SQLservers) {
#Query that runs on each server
$PIDSearch = "
Use MASTER
Select name from sys.server_principals where name LIKE '`%" + $UPID + "'"
$QueryOutput = Invoke-Sqlcmd2 -Query $PIDSearch -ServerInstance $server
if ($NULL -ne $QueryOutput) {
Write-Output "$UPID found on server $Server;" | Out-File -FilePath "C:\esarftemp\found_user.txt" -Append
}
}
#checking if there are any results in the found_user file
$Results = (Get-Content C:\esarftemp\found_user.txt).Length
if ($Results -ne 0) {
Write-Output "$UPID found on $Results server(s)"
New-UDHtml -Markup '<br></br>'
foreach ($line in Get-Content "C:\esarftemp\found_user.txt") {
write-Output "$line`n"
}
}
ELSE {
New-UDHtml -Markup "**********************************************************"
Write-Output "* Good News *"
Write-Output "* The PID $UPID was not found on any known SQL instance *"
Write-Output "* No action needed *"
Write-Output "**********************************************************"
New-UDHtml -Markup '<br></br>'
}
#not technically needed
$ScriptEnd = (Get-Date)
$TotalScriptTime = ($ScriptEnd - $ScriptStart)
New-UDHtml -Markup "Total time to check <b>$SQLServerCount</b> SQL servers $TotalScriptTime seconds"
}}
