diff --git a/Get-DSAManifest.ps1 b/Get-DSAManifest.ps1 new file mode 100644 index 0000000..ce893a1 --- /dev/null +++ b/Get-DSAManifest.ps1 @@ -0,0 +1,59 @@ +#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-yyyyMMddHHmmss.csv, using the current local date and time. +Set the boolean $Zip to $true to include --zip; otherwise it is omitted. +#> + +If ( [string]::IsNullOrWhiteSpace([string]$DSAManifestSource) ) { + Throw 'Set $DSAManifestSource in the caller before running Get-DSAManifest.' +} + +$DSAManifestOutput = Join-Path -Path $Global:OutputDirectory -ChildPath ( + 'dsa-manifest-{0}.csv' -f (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 ( $Zip -eq $true ) { + $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' +$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)" +}