简体   繁体   English

以管理员身份从批处理文件运行PowerShell FUNCTION

[英]Run PowerShell FUNCTION as administrator from Batch file

I have a PowerShell script. 我有一个PowerShell脚本。 The way I usually execute this script in the PS console is by using: 我通常在PS控制台中执行此脚本的方式是使用:

. .\psScript.ps1

functionName

functionName is my entry function. functionName是我的输入函数。 I am attempting to run this PowerShell script and auto execute the function functionName as Administrator from a batch file. 我正在尝试运行此PowerShell脚本,并以管理员身份从批处理文件中自动执行函数functionName I can execute this script and function from my batch file using: 我可以使用以下命令从批处理文件中执行此脚本和功能:

powershell -command "& { . .\psScript.ps1; functionName }"

However, this does not execute the script as administrator. 但是,这不会以管理员身份执行脚本。 So I have also tried the following: 所以我也尝试了以下方法:

powershell -NoProfile -ExecutionPolicy ByPass -command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy ByPass -File "". .\psScript.ps1; functionName""' -Verb RunAs}"

This still does not work, and the console disappears too quickly for me to read the error message. 这仍然不起作用,并且控制台消失得太快,以至于我无法读取错误消息。 Could anybody please help? 有人可以帮忙吗?

这可以从非提升的命令提示符下进行:

powershell -Command "Start-Process PowerShell –Verb RunAs -ArgumentList ""-File C:\Path\To\Script\psScript.ps1"""

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

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