Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

c# - Cant run ps1 script from out program files folder

I can run this script perfectly from my desktop:

    private void Sleep_Click(object sender, EventArgs e)
    {
         PowerShell ps = PowerShell.Create();
         ps.AddScript(@"D:Desktopalllightsoff.ps1");
         ps.Invoke();
    }

But when i change the paths to program files it does nothing.. any ideas?

    private void Sleep_Click(object sender, EventArgs e)
    {
        PowerShell ps = PowerShell.Create();
        ps.AddScript(@"C:Program Files (x86)Home Controlalllightsoff.ps1");
        ps.Invoke();
    }

Has proberly something to do with permissions, the script doesnt needs admin rights to run, and when i manually run the ps1 script from the program folder is just works ok.

I even tried %AppData% folder and getting the same result, ps1 file doesnt run.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

PetSerAl has, as usual, provided the crucial pointer in a comment on the question:

  • Use .AddCommand() to add a command by name or executable / script-file path.

    • In you needed to pass arguments to that command, you would then have to use the .AddParameters() method - see example below.
  • By contrast, use the (poorly named) .AddScript() method to add a self-contained piece of PowerShell source code as a string, which is parsed into a script block (a better name for the method would therefore have been .AddScriptBlock()).

    • In you needed to pass arguments, you would have to embed them directly in the source-code string - see example at the bottom.

Therefore, simply using .AddCommand() in lieu of .AddScript() is the solution:

ps.AddCommand(@"C:Program Files (x86)Home Controlscriptsalllightsoff.ps1");

If you wanted to pass an argument, say -Delay 60, you'd then call:

ps.AddParameters(new Dictionary<string, int> { ["Delay"] = 60 });

Why .AddScript() didn't work:

Since you're passing a piece of PowerShell source code, the usual evaluation rules apply:

A file path with embedded spaces such as
C:Program Files (x86)Home Controlalllightsoff.ps1 needs quoting for the PowerShell parser to recognize it as a single argument.

However, once quoted, you must use &, the call operator, for PowerShell to know that the quoted path represents a command (script file) to invoke (otherwise, it would treat it as a string).

Hence, if you want to use .AddScript() in your case, you must use:

ps.AddScript(@"& 'C:Program Files (x86)Home Controlscriptsalllightsoff.ps1'");

Note the leading &? and the '...' around the file path.

If you wanted to pass an argument, say -Delay 60, you'd have to include it in the string:

ps.AddScript(
  @"& 'C:Program Files (x86)Home Controlscriptsalllightsoff.ps1' -Delay 60"
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...