83 lines
3.7 KiB
PowerShell
83 lines
3.7 KiB
PowerShell
#Requires -Version 5.0
|
|
|
|
<#
|
|
.SYNOPSIS
|
|
Downloads and runs the DSA manifest collector.
|
|
.DESCRIPTION
|
|
Tools.ps1 must be dot-sourced by the caller first. Set $DSAManifestSource
|
|
in the caller's scope before using Run-Script -LivePSScript Get-DSAManifest.
|
|
The output path is generated in $Global:OutputDirectory with the filename
|
|
dsa-manifest-HOSTNAME-yyyyMMddHHmmss.csv, using the computer name and current local date and time.
|
|
Set $Zip to 'true', '$true', 'yes', 'y', or '1' to include --zip.
|
|
Use 'false', '$false', 'no', 'n', or '0' to disable ZIP; an empty value also disables it.
|
|
Values are case-insensitive and surrounding whitespace is ignored.
|
|
#>
|
|
|
|
If ( [string]::IsNullOrWhiteSpace([string]$DSAManifestSource) ) {
|
|
Throw 'Set $DSAManifestSource in the caller before running Get-DSAManifest.'
|
|
}
|
|
|
|
# Normalize RMM text values without treating every nonempty string as true.
|
|
$DsaZipValue = ([string]$Zip).Trim().ToLowerInvariant()
|
|
If ( $DsaZipValue -in @('true', '$true', 'yes', 'y', '1') ) {
|
|
$DsaZipEnabled = $true
|
|
}
|
|
ElseIf ( $DsaZipValue -in @('', 'false', '$false', 'no', 'n', '0') ) {
|
|
$DsaZipEnabled = $false
|
|
}
|
|
Else {
|
|
Throw 'Invalid $Zip value. Use true, $true, yes, y, 1, false, $false, no, n, or 0.'
|
|
}
|
|
|
|
$DSAManifestOutput = Join-Path -Path $Global:OutputDirectory -ChildPath (
|
|
'dsa-manifest-{0}-{1}.csv' -f $Env:COMPUTERNAME, (Get-Date -Format 'yyyyMMddHHmmss')
|
|
)
|
|
New-Item -Path $Global:OutputDirectory -ItemType Directory -Force -ErrorAction Stop | Out-Null
|
|
|
|
# Select the executable for the OS, even when the RMM uses 32-bit PowerShell.
|
|
If ( [Environment]::Is64BitOperatingSystem ) {
|
|
$DsaDownloadUrl = 'https://xfer.emberkom.com/shares/tools/folders/9b9988fb-c53a-4833-a4ad-68d01a3021bb/files/dsa-collect-windows-amd64-exe/download/dsa-collect-windows-amd64.exe'
|
|
}
|
|
Else {
|
|
$DsaDownloadUrl = 'https://xfer.emberkom.com/shares/tools/folders/9b9988fb-c53a-4833-a4ad-68d01a3021bb/files/dsa-collect-windows-386-exe/download/dsa-collect-windows-386.exe'
|
|
}
|
|
|
|
$DsaExecutablePath = Join-Path -Path $Global:ToolsDirectory -ChildPath 'dsa-collect.exe'
|
|
New-Item -Path $Global:ToolsDirectory -ItemType Directory -Force -ErrorAction Stop | Out-Null
|
|
|
|
Write-Output "Downloading dsa-collect.exe to ${DsaExecutablePath}"
|
|
$DsaDownloadedFile = Download-File -URL $DsaDownloadUrl -File $DsaExecutablePath -ErrorAction Stop
|
|
|
|
# Download-File can return nothing on failure. Do not run a stale executable.
|
|
If ( [string]::IsNullOrWhiteSpace([string]$DsaDownloadedFile) -or
|
|
!(Test-Path -LiteralPath $DsaExecutablePath -PathType Leaf) ) {
|
|
Throw "Failed to download dsa-collect.exe to ${DsaExecutablePath}"
|
|
}
|
|
|
|
$DsaArguments = @('--source', [string]$DSAManifestSource, '--output', [string]$DSAManifestOutput)
|
|
If ( $DsaZipEnabled ) {
|
|
$DsaArguments += '--zip'
|
|
}
|
|
|
|
# Quote native arguments explicitly, including paths ending in a backslash.
|
|
# Start-Process joins its argument list without adding the required quoting.
|
|
$DsaCommandLine = ($DsaArguments | ForEach-Object {
|
|
'"{0}"' -f ($_ -replace '(\\*)"', '$1$1\"' -replace '(\\+)$', '$1$1')
|
|
}) -join ' '
|
|
|
|
Write-Output "Running dsa-collect.exe (ZIP compression: ${DsaZipEnabled})"
|
|
$DsaProcess = Start-Process -FilePath $DsaExecutablePath -ArgumentList $DsaCommandLine `
|
|
-Wait -PassThru -WindowStyle Hidden -ErrorAction Stop
|
|
If ( $DsaProcess.ExitCode -ne 0 ) {
|
|
Throw "dsa-collect.exe failed with exit code $($DsaProcess.ExitCode)"
|
|
}
|
|
|
|
$DsaResultPath = $DSAManifestOutput
|
|
If ( $DsaZipEnabled ) {
|
|
$DsaResultPath = [System.IO.Path]::ChangeExtension($DSAManifestOutput, '.zip')
|
|
}
|
|
If ( !(Test-Path -LiteralPath $DsaResultPath -PathType Leaf) ) {
|
|
Throw "dsa-collect.exe did not create the expected output: ${DsaResultPath}"
|
|
}
|
|
Write-Output "DSA manifest created: ${DsaResultPath}"
|