![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Re: How to run an executable with spaces in the command line Ah. Yes. Of course. I feel jolly stupid now. Thanks a lot for your help. On 21 Apr, 09:33, Lionel Fourquaux <use.reply...@xxxxxx-spam.invalid> wrote: Quote: > In your first example, %SystemRoot% is expanded by cmd.exe, which uses > this syntax for environment variables. On the other hand, PowerShell > uses another syntex (e.g. $env:SystemRoot), so it does not understand > your use of %ComSpec% in the second example. You could try using > $env:ComSpec instead to build $commandLine. > > On 2008-04-21, ssg31415926 <newsjunkm...@xxxxxx> wrote: > Quote: > > One further question: why does this work: Quote: > > $commandLine = "cmd /c %SystemRoot%\system32\defrag.exe" Quote: > > but this doesn't: Quote: > > $commandLine = "%ComSpec% /c %SystemRoot%\system32\defrag.exe" Quote: > > E.g. if I try this: > > $commandLine = "cmd /c %SystemRoot%\system32\defrag.exe" > > invoke-expression $commandLine > > $commandLine = "%ComSpec% /c %SystemRoot%\system32\defrag.exe" > > invoke-expression $commandLine Quote: > > I get this: Quote: > > ForEach-Object : Cannot bind parameter 'Process'. Cannot convert value > > "ComSpec%" to type "System.Management.Automation > > .ScriptBlock". Error: "Invalid cast from 'System.String' to > > 'System.Management.Automation.ScriptBlock'." > > At line:1 char:2 > > + %C <<<< omSpec% /c %SystemRoot%\system32\defrag.exe Quote: > > and I can't work out where the foreach-object is coming from. It > > looks like the % Quote: > > On 20 Apr, 20:40, ssg31415926 <newsjunkm...@xxxxxx> wrote: Quote: > >> That's solved it. Well, mostly that with a bit of realisation that my > >> Guid parameter was being treated as a script block! But it's now > >> working with invoke-expression. Quote: Quote: > >> Thanks very much. Quote: Quote: > >> On 20 Apr, 14:59, RichS [MVP] <RichS...@xxxxxx> > >> wrote: Quote: Quote: > >> > There is a good explanation of the differences in this thread Quote: Quote: Quote: Quote: > >> > -- > >> > Richard Siddaway > >> > All scripts are supplied "as is" and with no warranty > >> > PowerShell MVP > >> > Blog:http://richardsiddaway.spaces.live.com/ > >> > PowerShell User Group:http://www.get-psuguk.org.uk Quote: Quote: > >> > "RichS [MVP]" wrote: > >> > > Try Quote: Quote: > >> > > $com = "find.exe /?" > >> > > Invoke-Expression $com Quote: Quote: > >> > > -- > >> > > Richard Siddaway > >> > > All scripts are supplied "as is" and with no warranty > >> > > PowerShell MVP > >> > > Blog:http://richardsiddaway.spaces.live.com/ > >> > > PowerShell User Group:http://www.get-psuguk.org.uk Quote: Quote: > >> > > "ssg31415926" wrote: Quote: Quote: > >> > > > I'm trying to run an executable using a variable which holds the > >> > > > command line so that I can build up the options one-by-one. It won't > >> > > > run. I get the following error: > >> > > > The term 'etc' is not recognized as a cmdlet, function, operable > >> > > > program, or script file. Verify the term and try again. > >> > > > (I've removed the actual command line because it's several lines > >> > > > long.) > >> > > > I've tried a simple version to recreate the problem: Quote: Quote: > >> > > > find.exe > >> > > > write-host "----------------------------------------" > >> > > > find.exe /? > >> > > > write-host "----------------------------------------" > >> > > > $commandLine = "find.exe" > >> > > > &$commandLine > >> > > > write-host "----------------------------------------" > >> > > > $commandLine = "find.exe /?" > >> > > > &$commandLine Quote: Quote: > >> > > > which displays: Quote: Quote: > >> > > > PS C:\Scripts> .\RunningExternalCommand.ps1 > >> > > > FIND: Parameter format not correct > >> > > > ---------------------------------------- > >> > > > Searches for a text string in a file or files. Quote: Quote: > >> > > > FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:] > >> > > > [path]filename[ ...]] Quote: Quote: > >> > > > /V Displays all lines NOT containing the specified string. > >> > > > /C Displays only the count of lines containing the string. > >> > > > /N Displays line numbers with the displayed lines. > >> > > > /I Ignores the case of characters when searching for the > >> > > > string. > >> > > > /OFF[LINE] Do not skip files with offline attribute set. > >> > > > "string" Specifies the text string to find. > >> > > > [drive:][path]filename > >> > > > Specifies a file or files to search. Quote: Quote: > >> > > > If a path is not specified, FIND searches the text typed at the prompt > >> > > > or piped from another command. > >> > > > ---------------------------------------- > >> > > > FIND: Parameter format not correct > >> > > > ---------------------------------------- > >> > > > The term 'find.exe /?' is not recognized as a cmdlet, function, > >> > > > operable program, or script file. Verify the term and t > >> > > > ry again. > >> > > > At C:\Scripts\RunningExternalCommand.ps1:9 char:2 > >> > > > + &$ <<<< commandLine Quote: Quote: > >> > > > The first three work, so, I'm guessing it's something to do with there > >> > > > being a space in $commandLine. I've tried various combinations of > >> > > > quoting but can't make it work. Can anyone help? |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Running a command from inside a script, command line is corrupted | PowerShell | |||
| VBS script with spaces in executable path | VB Script | |||
| How to run an executable with spaces in the command line | PowerShell | |||
| How to handle spaces in command line paths? | PowerShell | |||
| RE: How to handle spaces in command line paths? | PowerShell | |||