Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Ive been working on this issue for 2 days now, and Im stumped. So I humbly come here hoping someone can help me out.

I am trying to write a powershell script that will setup Dell hardware to add an asset tag and property owner tag using cctk. Here is what I have written so far.

$prox86 = ${env:ProgramFiles(x86)}
$cctkpath = "$prox86\Dell\CCTK\X86_64\cctk.exe"
$assettag = "123456"
$proptag = "Property of My Company"
& cmd.exe /c $cctkpath "--asset=$assettag"
& cmd.exe /c $cctkpath "--propowntag=$proptag"

When I run the PS script, the asset tag portion works perfectly. The propowntag will not work when i include spaces. It comes back with an error that says..

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

For whatever reason those additional spaces in my $proptag variable seem to kill that line of code. if I change the property tag to soemthing like "test123", or anything without spaces, it will work correctly. I tried using the suggestions in the link below, but I couldnt get it to work. Any help would be greatly apprecaited.

How to Call CMD.EXE from PowerShell with a Space in the Specified Command's Directory Name

share|improve this question
    
You can just remove cmd.exe /c and execute it "natively" using the call operator & like you have. That way you don't have to pass a string with quotes around it to cmd.exe. –  Andy Arismendi Jun 19 '13 at 2:37

1 Answer 1

up vote 0 down vote accepted

If you don't need cmd.exe to run it, I came up with this:

$prox86 = ${env:ProgramFiles(x86)}
$cctkpath = "$prox86\Dell\CCTK\X86_64\cctk.exe"
$assettag = "123456"
$proptag = "Property of My Company"

$cmd = "& `'$cctkpath`' --asset=`'$assettag`'"
Invoke-Expression $cmd
$cmd = "& `'$cctkpath`' --propowntag=`'$proptag`'"
Invoke-Expression $cmd

The reason why this works is that `' will escape the spaces and store it in $cmd then when invoke expression is run it will evaluate $cmd correctly.

Hope this helps.

share|improve this answer
1  
`' doesn't escape spaces. They're escaped single quotes in the command string, and they don't require escaping in the first place. Plain single quotes will work just fine. –  Ansgar Wiechers Jun 19 '13 at 8:42
    
Andy and Kyle, thanks for your input. I tried both methods and they both worked. Both of your help is greatly appreciaed! –  Adam Jun 19 '13 at 15:12
    
Thanks @Ansgar, I wasn't aware. and you're welcome Adam. –  Kyle Muir Jun 19 '13 at 19:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.