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)
2)
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
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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\TestingWinrarCom mandLine.r ar"
$ParamPriority "-ri1"
$ParamMultiThread " -mt2"
$ParamDateName " -ag[yyyy-mm-dd]"
$ParamCompressionLevel " -m5"
$ParamFileToarchive " C:\test1\AdventureWorks201 2_2.bakAdv entureWork s2012_2.ba k"
OUt-host $winrar a $ArchiveLocation $ParamPriority $ParamMultiThread $ParamDateName $ParamCompressionLevel $ParamFileToarchive
Gives:
Unexpected token '-ri1' in expression or statement.
$winrar = "c:\program files\winrar\Rar.exe"
$ArchiveLocation = "c:\test1\TestingWinrarCom
$ParamPriority "-ri1"
$ParamMultiThread " -mt2"
$ParamDateName " -ag[yyyy-mm-dd]"
$ParamCompressionLevel " -m5"
$ParamFileToarchive " C:\test1\AdventureWorks201
OUt-host $winrar a $ArchiveLocation $ParamPriority $ParamMultiThread $ParamDateName $ParamCompressionLevel $ParamFileToarchive
Gives:
Unexpected token '-ri1' in expression or statement.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
https://www.experts-exchange.com/questions/24362244/Powershell-7-zip.html
HTH,
DH