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

时间:2016-12-22 17:01:46

标签: windows powershell

我编写了一个脚本,它将在我的应用程序上执行一些操作,并且该应用程序作为本地用户app1user运行。因此,我需要从管理员帐户运行该脚本,并使用runas /user:app1user调用该脚本,但它会提示输入用户app1user

无论如何,我们可以克服这个问题并将该脚本作为app1user运行而不使用PowerShell密码。

我也尝试仅从Evlevated powershell窗口调用该脚本。

1 个答案:

答案 0 :(得分:2)

在powershell中,您可以使用 -Verb RunAs 。现在您也可以使用cmd,也可以直接从Powershell使用它。

<强> CMD:

powershell -Command "Start-Process powershell \"-ExecutionPolicy Bypass -NoExit -Command `\"cd \`\"C:\Temp\`\"; & \`\".\Script.ps1\`\"`\"\" -Verb RunAs"

新会话的Powershell(您也可以传递 NoNewWindow ):

Start-Process powershell "-ExecutionPolicy Bypass -NoExit -Command `"cd \`"C:\Temp\`"; & \`".\Script.ps1\`"`"" -Verb RunAs

Next Alternative正在使用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"}

希望这有助于...... !!!

相关问题