简体   繁体   English

批处理脚本未调用Powershell

[英]Batch script not calling Powershell

I have a batch script that calls a Powershell file in administration mode. 我有一个批处理脚本,该脚本在管理模式下调用Powershell文件。 I found this code a while ago, and it's worked great ever since: 我在不久前找到了这段代码,从那时起它一直很有效:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command 
"& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File %PSFile%' -Verb RunAs}";

This time though, I called the batch script from another program. 但是这次,我从另一个程序调用了批处理脚本。 This program says the process worked, but it didn't actually do anything. 该程序表示该过程有效,但实际上并未执行任何操作。 Examining the logs from echo, I can see the batch script is being called, but it's not calling Powershell. 从echo看日志,我可以看到批处理脚本被调用了,但是没有调用Powershell。 I tried running the batch script manually, and it calls PS fine, so something with how the batch script is being called by the other program is messing with how it calls PS. 我尝试手动运行批处理脚本,它很好地调用了PS,因此其他程序如何调用批处理脚本的某些事情弄乱了它如何调用PS。

This in mind, I tried changing the batch script to directly run my .ps1 file, instead of starting a new admin instance of powershell to start it. 考虑到这一点,我尝试更改批处理脚本以直接运行.ps1文件,而不是启动新的powershell管理实例来启动它。 My new .bat file looked like this: 我的新.bat文件如下所示:

Powershell -File %PSFILE% -Verb RunAs

Calling this from the other program sucessfully calls my Powershell script, but I get a bunch of errors from the PS script, since it's not an admin PS session like it needs to be. 从另一个程序调用此程序成功调用了我的Powershell脚本,但是由于PS脚本不是必需的admin PS会话,因此我从PS脚本中收到了很多错误。

How can I change my batch script to call Powershell as an admin, without using Powershell to call itself (which doesn't seem to work with the program that needs to run it)? 如何更改批处理脚本以以管理员身份调用Powershell,而又不使用Powershell调用自身(似乎与需要运行它的程序不兼容)?

EDIT: After trying a bunch of tweaks, I've found I don't even need to be in admin mode to do what this script does. 编辑:尝试了一堆调整后,我发现我什至不需要进入管理员模式即可执行此脚本。 However, I still get access denied errors when running it through the program (admin or not). 但是,通过程序(无论是否通过管理员)运行它时,仍然出现拒绝访问错误。 So something about running it from the program is making it need more permissions than when I run the batch script manually. 因此,与从程序中运行它相比,与手动运行批处理脚本时相比,它需要更多权限。

This is what I do (inside the .bat file): 这是我所做的(在.bat文件中):

If the .bat is NOT running as admin 如果.bat 不是以管理员身份运行

powershell.exe -Command "powershell.exe 'C:\path\to\script.ps1' -Verb runAs"

If the .bat is running as admin 如果.bat 以管理员身份运行

powershell.exe -ExecutionPolicy Bypass -Command "C:\path\to\script.ps1"

You could use a small utility I wrote called elevate32.exe / elevate64.exe . 您可以使用一个我写的名为elevate32.exe / elevate64.exe的小实用程序。

elevate64 -- C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy Bypass -File "d:\path to script\scriptfile.ps1"

elevate32.exe (32-bit version) and elevate64.exe (64-bit version) basically elevate whatever command line you pass to them. elevate32.exe (32位版本)和elevate64.exe (64位版本)基本上可以提升传递给它们的命令行。

You can get it here (ElevationToolkit1.zip): 您可以在此处获取它(ElevationToolkit1.zip):

http://www.westmesatech.com/misctools.html http://www.westmesatech.com/misctools.html

An alternative is to use a short WSH script that, when called, provokes elevation. 一种替代方法是使用简短的WSH脚本,该脚本在被调用时会引起海拔升高。 An example is on Aaron Margosis' blog here: 一个例子在Aaron Margosis的博客上:

https://blogs.msdn.microsoft.com/aaron_margosis/2007/07/01/scripting-elevation-on-vista/ https://blogs.msdn.microsoft.com/aaron_margosis/2007/07/01/scripting-elevation-on-vista/

Script: 脚本:

// elevate.js -- runs target command line elevated
if (WScript.Arguments.Length >= 1) {
  Application = WScript.Arguments(0);
  Arguments = "";
  for (Index = 1; Index < WScript.Arguments.Length; Index += 1) {
    if (Index > 1) {
      Arguments += " ";
    }
    Arguments += WScript.Arguments(Index);
  }
  new ActiveXObject("Shell.Application").ShellExecute(Application, Arguments, "", "runas");
}
else {
  WScript.Echo("Usage:");
  WScript.Echo("elevate Application Arguments");
}

The limitations of this approach is that it relies on the WSH command-line parser and can't wait for the program to terminate. 这种方法的局限性在于它依赖于WSH命令行解析器,而不能等待程序终止。 These limits may not be a problem in your scenario. 在您的方案中,这些限制可能不是问题。

Looks like I was totally off as to the problem source. 看来我对问题根源完全不满意。 This was a permissions error on some folders I was editing. 这是我正在编辑的某些文件夹上的权限错误。 The program I was running the scripts through acts as a separate service. 我通过运行脚本的程序充当单独的服务。 I had to add that with modify permissions to the security groups of all the folders I was editing. 我必须添加它,并对我正在编辑的所有文件夹的安全组具有修改权限。 No elevation required in the scripts, just modifying permissions. 脚本中不需要提升权限,只需修改权限即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM