0

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

Community
  • 1
  • 1
Adam
  • 3
  • 1
  • 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 02:37

1 Answers1

0

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.

Kyle Muir
  • 3,875
  • 2
  • 22
  • 27
  • 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 08: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