简体   繁体   English

以管理员身份运行脚本,从用户开始执行执行策略绕过

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

相关问题 从以管理员身份运行的进程中以用户身份启动进程 - Start a process as user from an process running as admin 从以SYSTEM身份运行的服务中启动可与用户交互的应用程序 - Starting application from service running as SYSTEM that can interact with the user 使用PowerShell脚本为数百个用户提供Set-ExecutionPolicy - Set-ExecutionPolicy for hundreds of users with PowerShell script 为特定脚本全局设置 ExecutionPolicy - Set-ExecutionPolicy gloablly for a specific script Jmeter 带有 Firefox 的脚本记录器不允许用户绕过身份验证 - Jmeter Script recorder with Firefox not letting user bypass Authentication 在以句点开头的 Windows 上运行 npm 脚本 - Running npm script on windows starting with a period Powershell无法执行竹任务,因为-ExecutionPolicy绕过-命令返回码是134而不是0 - Powershell is failing bamboo task because -ExecutionPolicy bypass -Command return code is 134 instead of 0 如何在用户登录时获得具有管理员权限的应用程序运行并防止用户杀死它? - How to get Application Running with admin permissions at user login and prevent user from killing it? QProcess 如何请求 windows UAC 绕过密码使用 runAs 来自管理员帐户的管理员级别命令 - QProcess how to request windows UAC to bypass password using runAs for admin level commands from an admin account 以管理员身份运行PowerShell脚本并隐藏PowerShell窗口 - Running PowerShell script as admin and with PowerShell window hidden
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM