运行 通过具有提升权限的批处理文件的 Powershell 脚本

Run Powershell script via batch file with elevated privileges

我需要 运行 一个 Powershell 脚本来通过批处理文件创建 AD 用户。问题是我需要 运行 这个 PS 具有提升权限的脚本(域管理员帐户)。我试图编写一个包含所有这些信息的“.bat”文件的脚本,但到目前为止我一直没有成功。这是脚本:

echo off
cls
echo Sign in with your ADM ID
set /p username=

powershell -noprofile -command "&{ start-process powershell -ArgumentList '-
noprofile -file C:\Users\...\Desktop\Powershell_scripts\New-ADuser\New-
Aduser_test.ps1' -verb RunAs}"

我已经尝试使用 /netonly /user:adm@domain 行,但它不起作用。

你们有什么想法吗?

提前致谢。

您可以使用其他凭据启动 powershell

@echo off
cls
echo Sign in with your ADM ID  
set/P user="*     user: "
rem set/P pass="* password: "
set "psCmd=powershell -Command "$pwd = read-host '* password' -AsSecureString; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd); [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /F "usebackq delims=" %%P in (`%psCmd%`) do set "pass=%%P"

powershell -executionpolicy bypass -Command "$p='%pass%'|convertto-securestring -asplaintext -force;$c=new-object -typename system.management.automation.pscredential('%user%',$p);start-process 'powershell' '-Command "C:\Users\...\Desktop\Powershell_scripts\New-ADuser\New-Aduser_test.ps1"' -credential $c -passthru -wait; read-host;"
exit/B

或者干脆

@echo off
cls

powershell -executionpolicy bypass -Command "start-process 'powershell' '-Command "C:\Users\...\Desktop\Powershell_scripts\New-ADuser\New-Aduser_test.ps1"' -credential $c -passthru -wait; read-host;"
exit/B

将提示输入凭据

我终于得到了这个:

runas.exe /netonly /noprofile /user:domainadm@domain "powershell.exe -
noprofile -File "C:\Users\...\Desktop\Powershell_scripts\New-
ADuser\.ps1" -verb RunAs"

它现在就像一个魅力!

希望对有需要的人有所帮助。 ;)