Skip to content

Instantly share code, notes, and snippets.

@Colby-PDQ
Last active November 22, 2017 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Colby-PDQ/f148a630293270149a7d4b2369b60682 to your computer and use it in GitHub Desktop.
Save Colby-PDQ/f148a630293270149a7d4b2369b60682 to your computer and use it in GitHub Desktop.
Aborts all Running, Connecting, and Queued deployments.
# Written by Colby Bouma
# Aborts all runnings deployments. Based off of this script: https://gist.github.com/Colby-PDQ/38d033889303e42faf0648aa7d2ec2fe
# https://gist.github.com/Colby-PDQ/f148a630293270149a7d4b2369b60682
#
# v 003
$Deploy_DB_File = "$env:SYSTEMDRIVE\ProgramData\Admin Arsenal\PDQ Deploy\Database.db"
$Error_Message = "<Error><Message>Aborted</Message><Type>AdminArsenal.Remote.Agent.AbortedException</Type><StackTrace></StackTrace></Error>"
# This is a stripped down version of a function from a blog post I am writing
function Submit-Query ( $Query ) {
$Query_Output = sqlite3.exe "$Deploy_DB_File" "$Query" 2>&1
# I borrowed the error handling from this, but kind of went my own way with it
# http://serverfault.com/questions/340711/redirect-stderr-to-variable-in-powershell
if ( $Query_Output.Exception -ne $null ) {
Write-Log "ERROR: Submit-Query encountered an error"
Write-Log $Query_Output.Exception
} else {
return $Query_Output
}
}
function Write-Log {
$Timestamp = Get-Date -Format "HH:mm:ss.fff"
$Log_Message = "$Timestamp --- $args"
Write-Output $Log_Message
}
$DeploymentComputerIds_To_Abort = Submit-Query "SELECT DeploymentComputerId FROM DeploymentComputers WHERE NOT Stage = 'Finished';"
ForEach ( $DeploymentComputerId_To_Abort in $DeploymentComputerIds_To_Abort ) {
$Stage = Submit-Query "SELECT Stage FROM DeploymentComputers WHERE DeploymentComputerId = $DeploymentComputerId_To_Abort;"
if ( $Stage -eq "Queued" ) {
# Unfortunately the DeploymentUser isn't populated for Queued deployments, so we have to retrieve it from the Credentials table.
$DeploymentId = Submit-Query "SELECT DeploymentId FROM DeploymentComputers WHERE DeploymentComputerId = '$DeploymentComputerId_To_Abort';"
$UserId = Submit-Query "SELECT UserId from Deployments WHERE DeploymentId = '$DeploymentId';"
$DeploymentUser = Submit-Query "SELECT UserName FROM Credentials WHERE CredentialsId = '$UserId';"
} elseif ( $Stage -eq "Running" -or $Stage -eq "Connecting") {
$DeploymentUser = Submit-Query "SELECT DeploymentUser FROM DeploymentComputers WHERE DeploymentComputerId = $DeploymentComputerId_To_Abort;"
} else {
Write-Log "ERROR: Colby didn't account for Stage: $Stage"
}
$Ended = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
if ( $DeploymentUser -ne $null ) {
Submit-Query "UPDATE DeploymentComputers SET
Stage = 'Finished', Error = '$Error_Message', DeploymentUser = '$DeploymentUser', Ended = '$Ended'
WHERE DeploymentComputerId = '$DeploymentComputerId_To_Abort';"
}
}
@Colby-PDQ
Copy link
Author

003 fixes an issue where the Run Time would increase forever because I didn't fill in the Ended field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment