Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Using @ as argument

Reply
 
Old 01-20-2008   #1 (permalink)
Anders


 
 

Using @ as argument

I'm writing a simple PS script to automate backup. The files are archived
using RAR (no wordwrap, all on the same line):

rar a -r -m1 -x@xxxxxx:\Tools\Scripts\backup_exclude.txt d:\Backup\epiajob
$dayofweek.rar @c:\Tools\Scripts\backup_include.txt

When running from cmd.exe, it works as intended. But running it inside the
PS script, it gives: "Unrecognized token in source text.". I guess the
parser don't like the @.

How can I fix this?

Thx in advance,
Anders.



My System SpecsSystem Spec
Old 01-20-2008   #2 (permalink)
Hal Rottenberg


 
 

Re: Using @ as argument

Anders wrote:
Quote:

> rar a -r -m1 -x@xxxxxx:\Tools\Scripts\backup_exclude.txt d:\Backup\epiajob
> $dayofweek.rar @c:\Tools\Scripts\backup_include.txt
>
> When running from cmd.exe, it works as intended. But running it inside the
> PS script, it gives: "Unrecognized token in source text.". I guess the
> parser don't like the @.
Can you paste the exact error? It will usually tell you the exact character
where the problem is.

--

Hal Rottenberg
Blog: http://halr9000.com
Webmaster, Psi (http://psi-im.org)
Co-host, PowerScripting Podcast (http://powerscripting.net)
My System SpecsSystem Spec
Old 01-20-2008   #3 (permalink)
Anders


 
 

Re: Using @ as argument

Hal Rottenberg <hal@xxxxxx> wrote in
news:Oe$$WH4WIHA.1132@xxxxxx:
Quote:

> Can you paste the exact error? It will usually tell you the exact
> character where the problem is.
Unrecognized token in source text.
At C:\Tools\epia_pscp_backup.ps1:18 char:85
+ rar a -r -m1 -x@xxxxxx:\Tools\Scripts\backup_exclude.txt d:\Backup\epiajob
$dayofweek.rar @ <<<< c:\Tools\Scripts\backup_include.txt



My System SpecsSystem Spec
Old 01-20-2008   #4 (permalink)
Shay Levi


 
 

Re: Using @ as argument

You can't use the "@" sign unless you intend to pass an array, hashtable
or a here-string.
It's a special PowerShell character with special meaning, much like the "$"
symbol.

To compress a file with RAR try this:

$winrar = "$env:ProgramFiles\WinRAR\WinRAR.exe"
& $winrar a -m1 "d:\Backup\epiajob\$dayofweek.rar" "C:\Tools\Scripts\backup_exclude.txt"



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> I'm writing a simple PS script to automate backup. The files are
> archived using RAR (no wordwrap, all on the same line):
>
> rar a -r -m1 -x@xxxxxx:\Tools\Scripts\backup_exclude.txt d:\Backup\epiajob\$dayofweek.rar
@c:\Tools\Scripts\backup_include.txt
Quote:

>
> When running from cmd.exe, it works as intended. But running it inside
> the PS script, it gives: "Unrecognized token in source text.". I guess
> the parser don't like the @.
>
> How can I fix this?
>
> Thx in advance,
> Anders.

My System SpecsSystem Spec
Old 01-20-2008   #5 (permalink)
Jeffrey Snover[MSFT]


 
 

Re: Using @ as argument

If you ever need to pass an argument that starts with an @, you'll need to
escape it using backtick. e.g.

PS>t @c:\Tools\Scripts\backup_include.t
Unrecognized token in source text.
At line:1 char:3
+ t <<<< @c:\Tools\Scripts\backup_include.t
PS>t `@c:\Tools\Scripts\backup_include.t
args = @c:\Tools\Scripts\backup_include.t

--
Jeffrey P. Snover[MSFT]
Partner Architect, Windows Server
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

My System SpecsSystem Spec
Old 01-20-2008   #6 (permalink)
Shay Levi


 
 

Re: Using @ as argument

Duh

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> If you ever need to pass an argument that starts with an @, you'll
> need to escape it using backtick. e.g.
>
PS>> t @c:\Tools\Scripts\backup_include.t
PS>>
Quote:

> Unrecognized token in source text.
> At line:1 char:3
> + t <<<< @c:\Tools\Scripts\backup_include.t
PS>> t `@c:\Tools\Scripts\backup_include.t
PS>>
Quote:

> args = @c:\Tools\Scripts\backup_include.t
>

My System SpecsSystem Spec
Old 01-20-2008   #7 (permalink)
Anders


 
 

Re: Using @ as argument

"Jeffrey Snover[MSFT]" <jsnover@xxxxxx> wrote in
news:F0569015-872B-4D85-BCEC-8B385494074E@xxxxxx:
Quote:

> If you ever need to pass an argument that starts with an @, you'll
> need to escape it using backtick. e.g.

Thx, that did the trick :-)

Regards,
Anders

My System SpecsSystem Spec
Old 01-20-2008   #8 (permalink)
Steven Hystad


 
 

Re: Using @ as argument

Keep in mind that arguments are passed to external commands as an array of
strings. If one of the arguments passed contains a character that has
special meaning to Powershell then you will have to quote the string.
The command works in CMD.EXE because CMD only recognizes the space character
as a separator between arguments. If you needed to pass an argument that
contained a space you would place double quotes around it.
However, in Powershell there are several more more characters that you must
be aware of, such as @, $, (, and ). There may be others. You may enclose
the string argument in single or double quotes, but remember that a double
quoted string will still need to use tic marks to escape the character.
(e.g. `@, `$, `n, etc...) A single quoted string will have its content
passes as literal.

"Anders" <no@xxxxxx> wrote in message
news:Xns9A2BB17CDA098nospamforme@xxxxxx
Quote:

> I'm writing a simple PS script to automate backup. The files are archived
> using RAR (no wordwrap, all on the same line):
>
> rar a -r -m1 -x@xxxxxx:\Tools\Scripts\backup_exclude.txt d:\Backup\epiajob
> $dayofweek.rar @c:\Tools\Scripts\backup_include.txt
>
> When running from cmd.exe, it works as intended. But running it inside the
> PS script, it gives: "Unrecognized token in source text.". I guess the
> parser don't like the @.
>
> How can I fix this?
>
> Thx in advance,
> Anders.
>
>
My System SpecsSystem Spec
Old 01-21-2008   #9 (permalink)
Keith Hill [MVP]


 
 

Re: Using @ as argument

"Jeffrey Snover[MSFT]" <jsnover@xxxxxx> wrote in message
news:F0569015-872B-4D85-BCEC-8B385494074E@xxxxxx
Quote:

> If you ever need to pass an argument that starts with an @, you'll need to
> escape it using backtick. e.g.
>
Hmm I wonder if PoSh could benefit from a utility method like
[regex]::escape() escape that it would escape arguments?

--
Keith

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
P4P invalid argument encountered Vista General
Problem with an IF argument .NET General
Internet argument! Vista General
Argument that starts with '-' and contains ':' gets separated. PowerShell
use a variable for -whatif argument PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46