[英]Running script as admin with executionpolicy bypass starting from user
My powershell code is first run as user then when user wants to do something else I want to launch another script but that script required admin privileges so I have this command in my first powershell to run the required script as admin 我的powershell代码首先以用户身份运行,然后当用户想要执行其他操作时,我想启动另一个脚本,但是该脚本需要管理员权限,因此我在第一个powershell中具有此命令,以便以admin身份运行所需的脚本。
Start-Process -WindowStyle Hidden -FilePath PowerShell.exe -Verb Runas -ArgumentList "-executionpolicy bypass -File $path"
But this just does nothing it doesn't even run the file 但这什么都不做,甚至不运行文件
I wrote a function for about what you are trying to do 我写了一个关于您要做什么的函数
<#
.SYNOPSIS
Creates new powershell consoles
.DESCRIPTION
Used to create new powershell consoles running as same rights unless Elevated is selected in which case it runs as administrator
.EXAMPLE
New-PowershellConsole -Count 2 -Elevated -Exit
.PARAMETER Count
Starts up this many consoles
.PARAMETER Elevated
Starts Consoles as Administrator
.PARAMETER Exit
Closes the current powershell console
#>
function New-PowershellConsole {
param(
[int]$Count = 1,
[switch]$Elevated,
[switch]$Exit
)
if ($(new-object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) -or $($Elevated -eq $True)) {
while ($Count -gt 0){
Start-Process powershell -Verb runAs -ArgumentList "-NoExit -Command `"Set-Location $((Get-Location).path)`"";
$Count--
}
} else {
while ($Count -gt 0){
Start-Process powershell -ArgumentList "-NoExit -Command `"Set-Location $((Get-Location).path)`"";
$Count--
}
}
If($Exit){
exit
}
}
Based off your message it looks like you want to run a new powershell console as admin 根据您的消息,您似乎想以管理员身份运行新的Powershell控制台
Start-Process powershell -Verb runAs -ArgumentList "-NoExit";
I suggest to avoid errors is to check if the user is a administrator 我建议避免错误是检查用户是否是管理员
if ($(new-object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) -or $($Elevated -eq $True)) {
Start-Process powershell -Verb runAs -ArgumentList "-NoExit";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.