0

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

  • 1
    Looks like OK.txt is whats in the zip file but it cant write to C:\ Are you running powershell as a administrator? Also it looks like you got your code from here [https://stackoverflow.com/questions/32189955/unzip-password-protected-files](https://stackoverflow.com/questions/32189955/unzip-password-protected-files) – ArcSet Nov 04 '20 at 14:36

2 Answers2

2

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.

ArcSet
  • 6,518
  • 1
  • 20
  • 34
2

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.)

Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62