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

时间:2018-02-23 10:01:34

标签: powershell batch-file scripting sysadmin

我需要运行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}"

我尝试使用line / netonly / user:adm @ domain但它无法正常工作。

你们有什么想法吗?

提前致谢。

2 个答案:

答案 0 :(得分:2)

我终于得到了这个:

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

它现在就像一个魅力!

希望它能帮助任何有需要的人。 ;)

答案 1 :(得分:-1)

您可以使用其他凭据启动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

将提示输入凭据

相关问题