[英]windows run script with local user account from administrator powershell
I have written a script which will do some stuffs on my application and that application running as local user app1user
. 我已经编写了一个脚本,该脚本将对我的应用程序以及以本地用户
app1user
运行的应用程序进行一些app1user
。 So I need to run that script from Administrator account and call that script with runas /user:app1user
, but it prompts for the user app1user
password. 因此,我需要从管理员帐户运行该脚本,并使用
runas /user:app1user
调用该脚本,但是它会提示输入用户app1user
密码。
Is there anyway that we can overcome this and run that script as app1user
without using password through powershell. 无论如何,我们可以克服这个问题并以
app1user
运行该脚本,而无需通过powershell使用密码。
Also am tried to calling that script from Evlevated powershell window only. 也试图仅从Evlevated powershell窗口调用该脚本。
In powershell you can use -Verb RunAs . 在powershell中,可以使用-Verb RunAs 。 Now you can use from cmd also and you can directly use it from Powershell also.
现在,您还可以从cmd中使用它,也可以直接从Powershell中使用它。
CMD: CMD:
powershell -Command "Start-Process powershell \"-ExecutionPolicy Bypass -NoExit -Command `\"cd \`\"C:\Temp\`\"; & \`\".\Script.ps1\`\"`\"\" -Verb RunAs"
Powershell with new session (you can pass NoNewWindow also): 具有新会话的Powershell(您也可以通过NoNewWindow ):
Start-Process powershell "-ExecutionPolicy Bypass -NoExit -Command `"cd \`"C:\Temp\`"; & \`".\Script.ps1\`"`"" -Verb RunAs
Next Alternative is using Dot Net : 下一个替代方法是使用Dot Net:
# Get the ID and security principal of the current user account
$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();
$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);
# Get the security principal for the administrator role
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;
# Check to see if we are currently running as an administrator
if ($myWindowsPrincipal.IsInRole($adminRole)){"Its in elevated mode"}
Hope this helps...!!! 希望这可以帮助...!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.