Skip to content

Instantly share code, notes, and snippets.

@infamousjoeg
Last active January 27, 2022 16:48
Show Gist options
  • Save infamousjoeg/10309e37a90612cfbdfcf492b3b7d6fe to your computer and use it in GitHub Desktop.
Save infamousjoeg/10309e37a90612cfbdfcf492b3b7d6fe to your computer and use it in GitHub Desktop.
Safely Executes PACLI Commands from PowerShell using temporary bat files. Also throws on Error. Author: Ben Floyd
<#
Author: Ben Floyd
Date: 2017-05-08
Desc: Safely Executes PACLI Commands from PowerShell using temporary bat files. Also throws on Error.
TODO: Put into a module.
#>
param(
$baseDir = "C:\dev\somedirectory",
$pacliDir = "$baseDir\PACLI",
$vaultIni = "$baseDir\vault.ini",
$userIni = "$baseDir\user.ini",
[switch]$debug = $false
)
$cmdPacli = "$pacliDir\PACLI.exe"
$tmpPacliBat = "$baseDir\tmppacli.bat"
$stdErrorOut = "$baseDir\stderr.txt"
# Global functions
function Pacli {
Write-Debug "$cmdPacli $Args"
Set-Content $tmpPacliBat "@echo off`n""$cmdPacli"" $Args 2>""$stdErrorOut"""
$output = (& $tmpPacliBat)
$error = (Get-Content $stdErrorOut) -join " -- "
rm -Force $stdErrorOut
rm -Force $tmpPacliBat
if(![System.String]::IsNullOrWhiteSpace($error)) { throw $error }
return $output
}
# Example Usage:
try {
$safe = "Local Administrator Accounts"
Pacli INIT
Pacli DEFINEFROMFILE PARMFILE=$vaultIni VAULT=ReportingVault
Pacli DEFAULT VAULT=ReportingVault USER=$username FOLDER=Root
Pacli LOGON LOGONFILE=$userIni
$safeDesc = Pacli SAFEDETAILS SAFE=`"$safe`" OUTPUT`(DESCRIPTION`)
Pacli LOGOFF
} catch {
Write-Error $_.Exception.Message
} finally {
Pacli TERM
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment