以批处理文件的管理员身份运行PowerShell功能
我有一个PowerShell脚本。我通常执行在PS主机此脚本的方法是使用:以批处理文件的管理员身份运行PowerShell功能
. .\psScript.ps1
functionName
functionName
是我的入口函数。我试图运行这个PowerShell脚本并从批处理文件中以管理员身份自动执行功能functionName
。我可以使用我的批处理文件执行此脚本和函数:
powershell -command "& { . .\psScript.ps1; functionName }"
但是,这不会以管理员身份执行脚本。所以我也试过以下内容:
powershell -NoProfile -ExecutionPolicy ByPass -command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy ByPass -File "". .\psScript.ps1; functionName""' -Verb RunAs}"
这仍然不起作用,并且控制台消失太快,无法读取错误消息。有谁可以帮忙吗?
这个工作,从非提升的命令提示符:
powershell -Command "Start-Process PowerShell –Verb RunAs -ArgumentList ""-File C:\Path\To\Script\psScript.ps1"""
感谢您的回复。这确实从非提升cmd提示符作为管理员运行脚本,但是,它不会自动执行我需要它的功能,即.e.e. functionName(如上所示) – potatopotarto
运行此函数的最简单方法是将'psScript.ps1'文件的dot-sourcing和函数调用包装到* second *脚本中。然后使用我上面的方法调用* second *脚本。 – TechSpud
@potatopotarto你可以在ps1文件的末尾添加对函数的调用,即在顶部有'function a {...}',然后在那之后输入'a'来调用它。 – ConnorLSW
我认为CMD没有推出,管理员? – 4c74356b41
Cmd不作为管理员启动,没有。 – potatopotarto