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:
> 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:
>
> > One further question: why does this work: >
> > $commandLine = "cmd /c %SystemRoot%\system32\defrag.exe" >>
> > $commandLine = "%ComSpec% /c %SystemRoot%\system32\defrag.exe" >
> > 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 >>
> > 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 >
> > and I can't work out where the foreach-object is coming from. It
> > looks like the % >
> > On 20 Apr, 20:40, ssg31415926 <newsjunkm...@xxxxxx> wrote:
> >> 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. >>
> >> On 20 Apr, 14:59, RichS [MVP] <RichS...@xxxxxx>
> >> wrote: >
> >> > There is a good explanation of the differences in this thread >>>
> >> > "RichS [MVP]" wrote:
> >> > > Try >
> >> > > $com = "find.exe /?"
> >> > > Invoke-Expression $com >>
> >> > > "ssg31415926" wrote: >
> >> > > > 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: >
> >> > > > find.exe
> >> > > > write-host "----------------------------------------"
> >> > > > find.exe /?
> >> > > > write-host "----------------------------------------"
> >> > > > $commandLine = "find.exe"
> >> > > > &$commandLine
> >> > > > write-host "----------------------------------------"
> >> > > > $commandLine = "find.exe /?"
> >> > > > &$commandLine >
> >> > > > which displays: >
> >> > > > PS C:\Scripts> .\RunningExternalCommand.ps1
> >> > > > FIND: Parameter format not correct
> >> > > > ----------------------------------------
> >> > > > Searches for a text string in a file or files. >
> >> > > > FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:]
> >> > > > [path]filename[ ...]] >
> >> > > > /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. >
> >> > > > 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 >
> >> > > > 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?