admin管理员组

文章数量:1410724

My line of code in the R console is like this:

system2("powershell", args=c("-File", "C:\\Path To File\\PowerShellScript.ps1"))

And the PowerShell script itself is very simple. It just should start PowerPoint (for demonstration purpose).

Start-Process "powerpnt.exe"

But the execution fails with the following error output (I translated it to English so hopefully the content is given correctly...)

Error processing file “C:\Path” because the file does not have the extension “.ps1”.

Some annotations:

  • As you can see my file has the extension .ps1
  • My PowerShell version is 5.1.22621.4391
  • I tried also to set a perhaps better fitting ExecutionPolicy, but in the PowerShell the other ExecutionPolicy Unrestricted is maintained

How can I execute my PowerShell script?

My line of code in the R console is like this:

system2("powershell", args=c("-File", "C:\\Path To File\\PowerShellScript.ps1"))

And the PowerShell script itself is very simple. It just should start PowerPoint (for demonstration purpose).

Start-Process "powerpnt.exe"

But the execution fails with the following error output (I translated it to English so hopefully the content is given correctly...)

Error processing file “C:\Path” because the file does not have the extension “.ps1”.

Some annotations:

  • As you can see my file has the extension .ps1
  • My PowerShell version is 5.1.22621.4391
  • I tried also to set a perhaps better fitting ExecutionPolicy, but in the PowerShell the other ExecutionPolicy Unrestricted is maintained

How can I execute my PowerShell script?

Share Improve this question edited Mar 31 at 22:35 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Mar 10 at 13:14 tueftlatueftla 4991 gold badge4 silver badges20 bronze badges 1
  • 4 In real life, does the PathToFile folder have spaces in its name? Try passing path with literal quotation marks: args=c("-File", "\"C:\\PathToFile\\PowerShellScript.ps1\"") – Mathias R. Jessen Commented Mar 10 at 13:23
Add a comment  | 

2 Answers 2

Reset to default 4

The behavior described is consistent with what you'd see if you passed an unqualified file path containing whitespace, ie:

powershell -File C:\Path with spaces\to\file.ps1

The way to solve this is to qualify the full extent of the path with literal quotation marks:

powershell -File "C:\Path with spaces\to\file.ps1"

Which in an R string literal you should be able to express with the simple escape sequence \":

system2("powershell", args=c("-File", "\"C:\\PathToFile\\PowerShellScript.ps1\""))

Quoting from the system2 function documentation, regarding the args parameter (emphasis added):

The arguments have to be quoted e.g. by shQuote in case they contain space(s) or other special characters (a double quote or backslash on Windows, shell-specific special characters on Unix).

Since your argument contains spaces, it must be enclosed in "..." on the process command line on Windows, in order to be recognized as a single argument - this is what shQuote() can do for you:[1]

system2(
 'powershell',
 args=c(
   '-File', 
   shQuote('C:\\Path To File\\PowerShellScript.ps1')
 )
)

The above satisfies the syntax requirements of powershell.exe, the Windows PowerShell CLI, with respect to its -File parameter, i.e. it is equivalent to the following command line:[2]

powershell -File "C:\Path To File\PowerShellScript.ps1"

Taking a step back:

With literal arguments, you can perform the quoting (escaping) yourself (note the embedded "..." quoting):

system2(
 'powershell',
 args=c(
   '-File', 
   '"C:\\Path To File\\PowerShellScript.ps1"')
 )
)

In fact, if you know how to escape all individual arguments, you can encode them in a single argument passed to system2():

system2(
 'powershell',
 '-File "C:\\Path To File\\PowerShellScript.ps1"'
)

Finally, while system2 is recommended over the older system function function for new code, notably if you need system2's enhanced flexibility and portability, in simple cases system is still sufficient, which allows you to pass an entire command line, with appropriate embedded quoting/escaping:

system(
 'powershell -File "C:\\Path To File\\PowerShellScript.ps1"'
)

[1] Note that the escaping of \ as \\ in your literal argument is solely necessary to satisfy R's syntax requirements and that \ ends up on the process command line that gets invoked. On Windows, \ is not special at the system or shell level, though many executables themselves recognize the escape sequence \" as an escaped ".
Note that shQuote() applies embedded quoting unconditionally and therefore also to arguments that don't strictly need it; e.g., verbatim a is placed as verbatim "a" on the resulting process command line on Windows, and as verbatim 'a' on the resulting shell command line on Unix-like platforms (executed via sh -c).

[2] This command line not only works in both cmd.exe and PowerShell itself, but also in no-shell invocations that are possible on Windows, such as from a scheduled task or the Windows Run dialog (WinKey+R) or - as is the case here - via an API such as system2() that launches an executable directly.
By contrast, on Unix-like platforms system2() uses the system default shell, sh to execute the command line.

本文标签: Start a PowerShell script in R via system2()Stack Overflow