locked
Which high commas should I use to forward a variable to 7zip in PowerShell? RRS feed

  • Question

  • Hello PowerShell Community,

    currently I am writing a script that unzips all zipfiles in a folder. I am using 7zip because some of the zip-files are password protected.

    So anyway my problem is that some of the passwords contain a high comma(") oder a bang(!) and PowerShell tries to interpret them if I put the variable in like this:

    Set-Alias sz "$env:ProgramFiles\7-Zip\7z.exe"
    sz x "$directory\$name" -p"$pawd" -aoa

    But if I try to put the variable in like this:

    Set-Alias sz "$env:ProgramFiles\7-Zip\7z.exe"
    sz x "$directory\$name" -p'"$pawd"' -aoa

    7zip returns an error because the password is incorrect.

    Is there anyway to force PowerShell to deliver a password like: "!!!""!$ to 7zip in the correct form?

    I would appreciate any answer.

    Have a wonderfull day

    Jan


    Thursday, June 8, 2017 2:14 PM

Answers

  • You cannot have quotes in a CSV file in an unquoted string.

    Name ,Password
    file1,"!""!"
    file2,"$%("
    file3,"abc"

    The columns must be quoted and any quotes in the fields must be escaped.


    \_(ツ)_/

    • Proposed as answer by Hello_2018 Tuesday, June 13, 2017 4:43 AM
    • Marked as answer by JangoJR Tuesday, June 13, 2017 9:30 AM
    Friday, June 9, 2017 8:18 AM

All replies

  • Have you tried

    sz x "$directory\$name" -p $pawd -aoa

    There is no reason to encase $pawd in quotes.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Thursday, June 8, 2017 2:21 PM
  • First of all: Thank you for your fast answer.

    But sadly the removal of the quotes around $pawd will not help either.
    When I enter the version you suggested 7zip returns this:

    7-Zip [64] 16.04 : Copyright (c) 1999-2016 Igor Pavlov : 2016-10-04
    
    Scanning the drive for archives:
    1 file, 196 bytes (1 KiB)
    
    Extracting archive: C:\Users\***\Documents\***\test.zip
    --
    Path = C:\Users\***\Documents\***\test.zip
    Type = zip
    Physical Size = 196
    
    
    No files to process
    Everything is Ok
    
    Files: 0
    Size:       0
    Compressed: 196
    this might be caused by some wrong Parameters im my initial 7zip command but if I call 7zip like this:
    sz x "$directory\$name" -p"$pawd" -aoa

    It works fine until a password with a bang(!) or quotation mark(") apperars.

    Friday, June 9, 2017 5:39 AM
  • Hello,

    like clayman, I don't see any reason to put it in quotes. You could try to define quotes as [char]34 and the ! as [char]33.

    "abc$([char]34)"
    Otherwise, I guess, you have to look into the manual of 7zip. I don't think, that this is on the PowerShell side.
    Friday, June 9, 2017 5:53 AM
  • After a short web search, I found this discussion. I really seems to be a 7zip issue and not a PowerShell issue.

    https://sourceforge.net/p/sevenzip/discussion/45798/thread/e3ab198a/

    Friday, June 9, 2017 6:00 AM
  • To pass a string intact on a commandline as a variable you need to use single quotes to define the string.

    $pswd = 'abc"`!'

    The string will not be altered if defined with single quotes.


    \_(ツ)_/

    Friday, June 9, 2017 6:01 AM
  • Thanks for the many helpfull answers in such a short time.

    The decompress function is just one part of the Skript I have to write. So in general the function works like this:
    It selects every zip-file within a given folder and checks a file named "Passwords.txt" for the password for each zip-file. If there is no password for one or more of the zip-files it asks the user to enter the password(s) with the "read-host" command. If "Passwords.txt" contains every needed password the decompression with 7zip starts.

    So basicly I have all passwords in a file, which is in the correct format to be imported via "Import-CSV". Could that be of any use for my Porblem?

    If it could be helpfull I could post the whole function, but due to the fact that I am relativly new to PowerShell there are some very poor workarround within the function and it might be very poorly wirtten and no pleasure to read.

    Friday, June 9, 2017 7:14 AM
  • You cannot use quotes in a CSV file without escaping them.


    \_(ツ)_/

    Friday, June 9, 2017 7:40 AM
  • It's a txt file, which I transform to a .NET-Object through Import-Csv, rather than a CSV file.

    password.txt looks like this:

    Name,Password
    file1,!"!
    file2,$%(
    file3,abc

    If I import this file with

    Import-Csv password.txt

    I can use it like a .NET-Object.

    Name  Password
    ----  --------
    file1 !"!
    file2 $%(
    file3 abc


    • Edited by JangoJR Friday, June 9, 2017 8:02 AM spelling mistake
    Friday, June 9, 2017 8:00 AM
  • You cannot have quotes in a CSV file in an unquoted string.

    Name ,Password
    file1,"!""!"
    file2,"$%("
    file3,"abc"

    The columns must be quoted and any quotes in the fields must be escaped.


    \_(ツ)_/

    • Proposed as answer by Hello_2018 Tuesday, June 13, 2017 4:43 AM
    • Marked as answer by JangoJR Tuesday, June 13, 2017 9:30 AM
    Friday, June 9, 2017 8:18 AM