[英]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:\Desktop\alllightsoff.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 Control\alllightsoff.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. 有问题与权限有关,脚本不需要管理员权限运行,当我从程序文件夹手动运行ps1脚本时,工作正常。
I even tried %AppData% folder and getting the same result, ps1 file doesnt run. 我甚至尝试%AppData%文件夹并获得相同的结果,ps1文件不运行。
PetSerAl has, as usual, provided the crucial pointer in a comment on the question: 像往常一样, PetSerAl在对问题的评论中提供了关键指针:
Use .AddCommand()
to add a command by name or executable / script-file path . 使用.AddCommand()
按名称或可执行文件/脚本文件路径添加命令 。
.AddParameters()
method - see example below. 在您需要将参数传递给该命令时,您必须使用.AddParameters()
方法 - 请参阅下面的示例。 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()
). 相比之下,使用(名称.AddScript()
) .AddScript()
方法将一个自包含的PowerShell 源代码作为字符串添加 ,将其解析为脚本块 (因此该方法的更好名称将是.AddScriptBlock()
)。
Therefore, simply using .AddCommand()
in lieu of .AddScript()
is the solution: 因此,简单地使用.AddCommand()
代替.AddScript()
就是解决方案:
ps.AddCommand(@"C:\Program Files (x86)\Home Control\scripts\alllightsoff.ps1");
If you wanted to pass an argument , say -Delay 60
, you'd then call: 如果你想传递一个参数 ,比如-Delay 60
,你就会调用:
ps.AddParameters(new Dictionary<string, int> { ["Delay"] = 60 });
.AddScript()
didn't work: 为什么.AddScript()
不起作用: Since you're passing a piece of PowerShell source code , the usual evaluation rules apply: 由于您传递了一段PowerShell 源代码 ,因此通常的评估规则适用:
A file path with embedded spaces such as 带有嵌入空格的文件路径,例如
C:\\Program Files (x86)\\Home Control\\alllightsoff.ps1
needs quoting for the PowerShell parser to recognize it as a single argument. C:\\Program Files (x86)\\Home Control\\alllightsoff.ps1
需要引用 PowerShell解析器才能将其识别为单个参数。
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 ). 但是,一旦引用,您必须使用&
,调用运算符,以便PowerShell知道引用的路径表示要调用的命令 (脚本文件)(否则,它会将其视为字符串 )。
Hence, if you want to use .AddScript()
in your case, you must use: 因此,如果要在大小写中使用.AddScript()
,则必须使用:
ps.AddScript(@"& 'C:\Program Files (x86)\Home Control\scripts\alllightsoff.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: 如果你想传递一个参数 ,比如-Delay 60
,你必须将它包含在字符串中:
ps.AddScript(
@"& 'C:\Program Files (x86)\Home Control\scripts\alllightsoff.ps1' -Delay 60"
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.