• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 505
  • Last Modified:

Powershell - item in string being interpreted as a command.

I'm trying to use powershell to run a command against another API.  When running the script, powershell see's the symdg command (which is intended for the API) and tries to do something with it, which it cannot.

#####
$command = "for /f "tokens=3" %a in ('symdg list -v ^|find /I "Group Name:"') do symdg delete %a -force"

Invoke-expression $command
#####

The following shows Powershell trying to interpret the command.

Unexpected token 'tokens=3" %a in (`'symdg list -v ^|find /I "Group' in expression or statement.
At C:\test1.ps1:3 char:68
+ $cmd03 =  "for /f "tokens=3" %a in ('symdg list -v ^|find /I "Group <<<<  Name:"') do symdg delete %a -force"
    + CategoryInfo          : ParserError: (tokens=3" %a in...|find /I "Group:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

Is there a way to put the command into a string that powershell will not try to interpret it? I've tried to put the command into "<command>", ""<command>"", and other combinations of single, double quotes, brackets, parenthasies.
0
LN41
Asked:
LN41
1 Solution
 
footechCommented:
"for" is a command that's built in to cmd.exe, so you would either need to start that to run it, or change your approach to not use it.  Maybe the below would work for you, but I'm really not sure about the quotes.
& cmd /c "for /f ""tokens=3"" %a in ('symdg list -v ^|find /I ""Group Name:""') do symdg delete %a -force"

Open in new window

0
 
Vaseem MohammedCommented:
See if this works, we need to include in single quote for all double quotes to be inside it and act as a part of string.

$command = 'for /f "tokens=3" %a in ("symdg list -v ^|find /I "Group Name:"") do symdg delete %a -force'
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now