Reputation: 1
Powershell password protected file Hi, I'm new to powershell and try to learn some trick with it. I created a simple code that's supposed to unzip a file using 7zip and a known password.
Here's the code:
$7ZipPath = '"C:\Program Files\7-Zip\7z.exe"'
$zipFile = '"C:\Users\touff\OneDrive\Bureau\0\Encrypted Zip\test.zip"'
$path = 'C:\Users\touff\OneDrive\Bureau\0\Encrypted Zip\foldertest'
New-Item -ItemType directory -Path $path
Read-Host -Prompt 'step1'
$password = Read-Host -Prompt 'Input the password'
Write-Host $password
$command = "& $7ZipPath e -oC:\ -y -tzip -p$password $zipFile"
Invoke-Expression $command
I keep getting these errors :
7-Zip [64] 16.04 : Copyright (c) 1999-2016 Igor Pavlov : 2016-10-04
Scanning the drive for archives:
1 file, 310 bytes (1 KiB)
Extracting archive: C:\Users\touff\OneDrive\Bureau\0\Encrypted Zip\test.zip
Path = C:\Users\touff\OneDrive\Bureau\0\Encrypted Zip\test.zip
Type = zip
Physical Size = 310
ERROR: Can not open output file : Accès refusé. : C:\ok.txt
Sub items Errors: 1
Archives with Errors: 1
Sub items Errors: 1
Upvotes: 0
Views: 7719
Reputation: 24525
You don't need Invoke-Expression
; just run the command with the parameters you need. Here's a short sample script you can use (modify to suit your needs):
param(
[Parameter(Mandatory = $true)]
[String]
$ArchiveFilename,
[String]
$DestinationPath,
[Switch]
$HasPassword
)
$ARCHIVE_TOOL = "C:\Program Files\7-Zip\7z.exe"
function ConvertTo-String {
param(
[Security.SecureString] $secureString
)
try {
$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureString)
[Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
}
finally {
if ( $bstr -ne [IntPtr]::Zero ) {
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
}
}
}
if ( $HasPassword ) {
$securePwd = Read-Host -AsSecureString -Prompt "Enter archive password"
if ( $securePwd ) {
$password = ConvertTo-String $securePwd
}
}
if ( -not $DestinationPath ) {
$DestinationPath = (Get-Location).Path
}
& $ARCHIVE_TOOL e "-o$DestinationPath" "-p$password" $ArchiveFilename
If the script is named Expand-ArchiveFile.ps1
, run it like this:
Expand-ArchiveFile.ps1 "C:\Users\touff\OneDrive\Bureau\0\Encrypted Zip\test.zip" -HasPassword
Note that when specifying filenames, you do not need the embedded quotes. (The quotes are not part of the file's name.)
Upvotes: 2
Reputation: 6860
So here is what you are doing in function form to clean it up a bit
Function Open-7ZipFile{
Param(
[Parameter(Mandatory=$true)]
[string]$Source,
[Parameter(Mandatory=$true)]
[string]$Destination,
[string]$Password,
[Parameter(Mandatory=$true)]
[string]$ExePath7Zip,
[switch]$Silent
)
$Command = "& `"$ExePath7Zip`" e -o`"$Destination`" -y" + $(if($Password.Length -gt 0){" -p`"$Password`""}) + " `"$Source`""
If($Silent){
Invoke-Expression $Command | out-null
}else{
"$Command"
Invoke-Expression $Command
}
}
And here is how to run it
Open-7ZipFile -ExePath7Zip "C:\Program Files\7-Zip\7z.exe" -Source "C:\Users\touff\OneDrive\Bureau\0\Encrypted Zip\test.zip" -Destination "C:\Users\touff\OneDrive\Bureau\0\Encrypted Zip\foldertest" -Password "Password"
Make sure you have access to the folder you are trying to unzip to
If you do not have rights you will end up with the error you are getting now
ERROR: Can not open output file : Accès refusé. : C:\ok.txt
Edited the function to allow spaces and run silently.
Upvotes: 2