Link to home
Start Free TrialLog in
Avatar of reticentKoala
reticentKoala

asked on

Winrar from Powershell

I'm trying to move to using PowerShell for CLI tasks, so I'm trying to run the CLI version of WinRAR there.

I've tried a few options, and I'm getting a few failures! How can I run my Command Line command (which works fine from the command line) using PowerShell?



1)
PS C:\program files\winrar> & "c:\program files\winrar\Rar.exe a c:\test1\TestingWinrarCommandLine.rar -ri1 -mt2 -ag[yyy
y-mm-dd]   -m5 C:\test1\AdventureWorks2012_2.bakAdventureWorks2012_2.bak"
The term 'c:\program files\winrar\Rar.exe a c:\test1\TestingWinrarCommandLine.rar -ri1 -mt2 -ag[yyyy-mm-dd]   -m5 C:\te
st1\AdventureWorks2012_2.bakAdventureWorks2012_2.bak' is not recognized as the name of a cmdlet, function, script file,
 or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and tr
y again.
At line:1 char:2
+ & <<<<  "c:\program files\winrar\Rar.exe a c:\test1\TestingWinrarCommandLine.rar -ri1 -mt2 -ag[yyyy-mm-dd]   -m5 C:\t
est1\AdventureWorks2012_2.bakAdventureWorks2012_2.bak"
    + CategoryInfo          : ObjectNotFound: (c:\program file...Works2012_2.bak:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Open in new window


2)
$winrar = "c:\program files\winrar\Rar.exe" 
$args = " a c:\test1\TestingWinrarCommandLine.rar -ri1 -mt2 -ag[yyyy-mm-dd]   -m5 C:\test1\AdventureWorks2012_2.bakAdventureWorks2012_2.bak"

Invoke-command $winrar + $args

Invoke-Command : A positional parameter cannot be found that accepts argument ' a c:\test1\TestingWinrarCommandLine.rar -ri1 -mt2 -ag[yyyy-mm-dd]   -m5 C:\test1\AdventureWorks2012_2.bakAdve
ntureWorks2012_2.bak'.
At C:\Users\ben.hill\AppData\Local\Temp\e967fe55-481f-4885-934a-fa518808a2e8.ps1:4 char:15
+ Invoke-command <<<<  $winrar + $args
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

Open in new window

c:\program files\winrar\Rar.exe a c:\test1\TestingWinrarCommandLine.rar -ri1 -mt2 -ag[yyyy-mm-dd]   -m5 C:\test1\AdventureWorks2012_2.bakAdventureWorks2012_2.bak
Invoke-Command : A positional parameter cannot be found that accepts argument ' a c:\test1\TestingWinrarCommandLine.rar -ri1 -mt2 -ag[yyyy-mm-dd]   -m5 C:\test1\AdventureWorks2012_2.bakAdve
ntureWorks2012_2.bak'.
At C:\Users\ben.hill\AppData\Local\Temp\e967fe55-481f-4885-934a-fa518808a2e8.ps1:4 char:15
+ Invoke-command <<<<  $winrar + $args
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Neil Russell
Neil Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also explore 7-Zip to do your bidding as well... this might prove to be an easier if not just an alternative method to WinRAR.

https://www.experts-exchange.com/questions/24362244/Powershell-7-zip.html

HTH,

DH
Avatar of reticentKoala
reticentKoala

ASKER

The problem seems to be linked to the hyphen used in the Winrar parameters. Powershell thinks it's a token.

$winrar = "c:\program files\winrar\Rar.exe"
$ArchiveLocation = "c:\test1\TestingWinrarCommandLine.rar"
$ParamPriority "-ri1"
$ParamMultiThread  " -mt2"
$ParamDateName " -ag[yyyy-mm-dd]"  
$ParamCompressionLevel " -m5"
$ParamFileToarchive " C:\test1\AdventureWorks2012_2.bakAdventureWorks2012_2.bak"

OUt-host $winrar a $ArchiveLocation $ParamPriority $ParamMultiThread $ParamDateName $ParamCompressionLevel $ParamFileToarchive

Gives:
Unexpected token '-ri1' in expression or statement.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome. Working.

Can anyone explain why I have to put everything into parameters for this to work? Why can't I just give the whole string to Invoke-Command?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial