简体   繁体   English

Windows使用管理员Powershell的本地用户帐户运行脚本

[英]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.

相关问题 如何使用远程PowerShell创建本地Windows用户帐户? - How to create a local windows user account using remote powershell? 如何从 python 运行 Windows powershell 脚本? - How to run Windows powershell script from python? 在 Windows 中,NT 权限/系统能否以另一个用户身份运行 powershell 脚本 - In Windows, can NT Authority/System run a powershell script as another user 作为 Windows 10 管理员,如何从 Python 以管理员身份运行命令? - As a Windows 10 administrator, how to run a command as administrator from Python? VBScript 不会从从 Windows 任务计划程序运行的 Powershell 脚本中运行 - VBScript will not run from Powershell script that runs from Windows Task Scheduler 从网络驱动器使用管理员帐户运行批处理文件 - Run a batch file with administrator account from a network drive 从C#中的标准帐户以管理员身份运行批处理文件 - Run a batch file as administrator from an standard account in C# 如何运行 powershell 脚本或更改 session 并使用不同的用户帐户执行代码? - How to run a powershell script or change the session and execute code with different user account? 使用powershell创建本地Windows用户,以用作服务用户来运行服务 - Create a local windows user using powershell to be used just as a service user to run services 如何在Windows中使用批处理脚本获取管理员的用户名 - How to get just the user name of administrator using batch script in Windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM