?
Solved

Winrar from Powershell

Posted on 2012-09-11
6
Medium Priority
?
5,801 Views
Last Modified: 2012-09-14
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

0
Comment
Question by:reticentKoala
  • 3
  • 2
6 Comments
 
LVL 37

Accepted Solution

by:
Neil Russell earned 550 total points
ID: 38387077
FROM: http://powershellscripts.blogspot.co.uk/

Archive and pack IIS log files
Description

Archive and pack IIS log files.

Source Code

$WinRar = "C:\Program Files\WinRAR\rar"
$TodaysLogFile = get-date -Uformat %y%m%d
$TodaysLogFile = "ex$TodaysLogFile.log"
$LogFiles = ls *.log
if ($LogFiles) {
foreach ($File in $LogFiles) {
$FileName = $File.Name
if ($FileName -ne $TodaysLogFile) {
echo "Compressing $FileName"
&$WinRar m "$FileName.rar" $FileName
}
}
}
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 38387293
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.

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_24362244.html

HTH,

DH
0
 

Author Comment

by:reticentKoala
ID: 38387325
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.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 16

Assisted Solution

by:Dale Harris
Dale Harris earned 250 total points
ID: 38387412
Well the unexpected Token error that you are getting is because you didn't assign a value, you just put this:

$ParamPriority "-ri1"

It's probably something you overlooked in your haste to troubleshoot, but it should be:

$ParamPriority = "-ri1"
0
 

Author Comment

by:reticentKoala
ID: 38387526
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?
0
 
LVL 16

Assisted Solution

by:Dale Harris
Dale Harris earned 250 total points
ID: 38396657
So I found an easier way you might like:

Start-Process -FilePath notepad -ArgumentList "$env:windir\system32\drivers\etc\hosts"

This was from today's daily Powershell.com tip.

If your solution is solved, please assign points accordingly.

-DH
0

Featured Post

What Security Threats Are We Predicting for 2018?

Cryptocurrency, IoT botnets, MFA, and more! Hackers are already planning their next big attacks for 2018. Learn what you might face, and how to defend against it with our 2018 security predictions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month15 days, 5 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question