?
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
Medium Priority
?
10,056 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 1800 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 200 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
Ransomware Attacks Keeping You Up at Night?

Will your organization be ransomware's next victim?  The good news is that these attacks are predicable and therefore preventable. Learn more about how you can  stop a ransomware attacks before encryption takes place with our Ransomware Prevention Kit!

 

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

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.

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…
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Suggested Courses

762 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