Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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,071 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

609 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