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
9,986 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

The problems with reply email signatures

Do you wish that you could place an email signature under a reply? Well, unfortunately, you can't. That great Exchange/Office 365 signature you've created will just appear at the bottom of an email chain. What a pain! Is there really no way to solve this? Well, there might be...

Join & Write a Comment

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now