Solved

How do I run a windows command in powershell with parameters that include hyphens - having problems, specifically with WBADMIN

Posted on 2008-10-14
6
10,008 Views
Last Modified: 2012-05-05
I'm trying to run WBADMIN via a PS script. I'm generating the commandline with variables. The script is designed to perform a backup (obviously as a scheduled task every night) to a different location which is created depending on the date and time that the script is called. Code below.

When I run this as a script "powershell -command .\backup.ps1" I get the following error from WBADMIN:

"ERROR - The value for option: backuptarget is missing. See usage below."

The problem I'm having is that the parameters for WBADMIN require "-" which it seems to me is throwing the call to WBADMIN from powershell.

I've tried a lot of things to ascerrtain what exactly is causing the problem. I'm certain it's the hyphens (obviously because they form a vital part of PS syntax). Is there a built in way around this or can anybody recommeny anything or is it something really stupid I'm doing wrong.

Thanks.
Tom.



$date=$(Get-Date f o)
$date=$date.SubString(0,$date.IndexOf("."))
$date=$date -replace "/", "-"
$date=$date -replace "T", " "
$date=$date -replace ":", "."
$destpath = "Full Backup On $date"
$storagepath=new-item -path \\nas\backups -name $destpath -type directory
$commandline="wbadmin.exe"
$params="start backup -backuptarget:""$storagepath"" -include:C:,D: -VSSFull"
$exec = "$commandline $params"
invoke-expression $exec

Open in new window

0
Comment
Question by:tomfogarty
  • 4
6 Comments
 
LVL 6

Accepted Solution

by:
xfreddie earned 450 total points
ID: 22709722
I think you need to use an escape character see eg. http://www.leedesmond.com/weblog/?p=35

Also a simmular question has been answered here : http://www.eggheadcafe.com/software/aspnet/30491887/escape-character-question.aspx

hope it helps.
Gr,
Xfreddie
0
 

Author Comment

by:tomfogarty
ID: 22710076
Hi xfreddie,

I'm beginning to get places based on the egghead link. The solution lies in a mixture of backticks and curly braces.

This is the line of code that seems to get over the problem with the hyphens:
$params={start backup `-backuptarget:"$storagepath" `-include:C:,D: `-VSSFull}

When I run the script now I get the following error from WBADMIN:
ERROR - Command syntax incorrect. Error: D:. See usage below.

At least it seems to have moved past the hyphen problem. For some reason it doesn't seem to like the C:,D: piece - if I take out the ,D: it works in the script. If I pass the following command in a DOS prompt it works WBADMIN is happy with it and offers with a prompt to proceed with the backup:
wbadmin start backup -backuptarget:"\\nas\backup On 2008-10-13 20.18.39" -include:C:,D: -VSSFULL

Could there be some problem with the "," after C: - unlikely?

Tom.
0
 
LVL 18

Assisted Solution

by:BSonPosh
BSonPosh earned 50 total points
ID: 22710653
Change This
$params="start backup -backuptarget:""$storagepath"" -include:C:,D: -VSSFull"

To This
$params="start backup -backuptarget:`"$storagepath`" -include:C:,D: -VSSFull"
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:tomfogarty
ID: 22714425
Sorry BsonPosh - this doesn't work. Gives me the same as the previous error.

"ERROR - The value for option: backuptarget is missing. See usage below."

The curly braces seem to be a requirement.
$params={start backup `-backuptarget:"$storagepath" `-include:C:,D: `-VSSFull}

Just need to find out what's going on with the "C:,D:" piece.

Tom.
0
 

Author Closing Comment

by:tomfogarty
ID: 31505806
Thanks guys - giving most of the points to xfreddie since his solution pointed me in the right direction. The other solution made me think a bit further!
0
 

Author Comment

by:tomfogarty
ID: 22714680
Final solution was to add quotes on -include parameter. Full code snippet attached (less debug code). Points assigned.

Thanks for your help!

Tom.

$date=$(Get-Date f o)
$date=$date.SubString(0,$date.IndexOf("."))
$date=$date -replace "/", "-"
$date=$date -replace "T", " "
$date=$date -replace ":", "."
$destpath = "Full Backup On $date"
$storagepath=new-item -path \\techpartnas\backups -name $destpath -type directory
$commandline="wbadmin.exe"
$params={start backup `-backuptarget:"$storagepath" `-include:"C:,D:" `-VSSFull}
$exec = "$commandline $params"
Invoke-Expression $exec

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

786 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