PS Script Not Accepting Password
Hi all -
I have a powershell script that is supposed to password protect a file, then, compress it. The purpose of it is I run this on suspicious files via live response on Defender, then, I can safely collect the file without worry of accidental detonation.
However, I'm having an issue with it. It is not accepting the password (Test). Would anyone be able to assist with troubleshooting the issue?
Issues:
- It is not accepting the password
- It prompts for the password, but says it's wrong
- It seems to not accept all file types. Sometimes it does, sometimes it doesnt.
- It doesnt always prompt for a password when extracting to a location.
Any assistance would be greatly appreciated. Script below.
param (
[string]$filePath
)
# Path to 7-Zip executable
$sevenZipPath = "C:\Program Files\7-Zip\7z.exe"
# Password to protect the compressed file
$password = "Test"
# Ensure 7-Zip is installed
if (-Not (Test-Path $sevenZipPath)) {
Write-Error "7-Zip is not installed or the path to 7z.exe is incorrect."
exit
}
# Output the provided file path for debugging
Write-Output "Provided file path: $filePath"
# Verify the file exists
if (-Not (Test-Path $filePath)) {
Write-Error "The specified file does not exist: $filePath"
exit
}
# Get the directory and filename from the provided file path
$fileDirectory = Split-Path -Parent $filePath
$fileName = Split-Path -Leaf $filePath
# Output the parsed directory and filename for debugging
Write-Output "File directory: $fileDirectory"
Write-Output "File name: $fileName"
# Define the output zip file path
$zipFilePath = Join-Path -Path $fileDirectory -ChildPath "$($fileName).zip"
# Output the zip file path for debugging
Write-Output "ZIP file path: $zipFilePath"
# Compress and password protect the file
& $sevenZipPath a $zipFilePath $filePath -p$password
if ($LASTEXITCODE -eq 0) {
Write-Output "File '$fileName' has been successfully compressed and password protected as '$zipFilePath'."
} else {
Write-Error "An error occurred while compressing and password protecting the file."
}
Thanks!