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,046 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

690 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