首页 文章

管理员脚本不会在重启时以管理员身份重新启动

提问于
浏览
0

我正在尝试运行一个脚本,它将在重新启动时运行另一个Powershell脚本 . 我正在使用HKLM \ Software \ Microsoft \ Windows \ CurrentVersion \ Run键 .

我的问题是脚本在重启时启动,但是从非管理员PS窗口运行 . 这让我“拒绝访问” . 我在系统上禁用了UAC,但得到了同样的错误 . 如何在重新启动时从Admin Powershell窗口启动它?

目前有:

REG添加HKLM \ Software \ Microsoft \ Windows \ CurrentVersion \ Run / v RunThis / t REG_SZ / f / d“C:\ windows \ system32 \ WindowsPowerShell.exe -ExecutionPolicy Unrestricted -File C:\ script.ps1 -Verb Runas

2 回答

  • 0

    假设您在手动运行时没有拒绝访问,这应该可以解决问题 . 打开Powershell管理员提示 . 关键在于 -

    set-executionpolicy remotesigned
    

    现在将其添加到Run键 -

    "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file C:\script.ps1
    
  • 0

    这实际上取决于几个因素:UAC设置;是机器域加入;等等...

    此脚本适用于大多数情况 . 注意:在脚本完成执行后,admin powershell主机将关闭 .

    $currentUserID = [System.Security.Principal.WindowsIdentity]::GetCurrent()
    $currentUserPrincipal = New-Object System.Security.Principal.WindowsPrincipal($currentUserID)
    
    $adminPrincipal = [System.Security.Principal.WindowsBuiltInRole]::Administrator
    
    # If not already admin start a new process and pass the current session definition to the admin powershell instance.
    if(-not ($currentUserPrincipal.IsInRole($adminPrincipal))) {
        $adminProc = New-Object System.Diagnostics.ProcessStartInfo "powershell"
        $adminProc.Arguments = $MyInvocation.MyCommand.Definition
        $adminProc.Verb = "runas"
    
        [System.Diagnostics.Process]::Start($adminProc)
    
        #If you don't need to keep the current session open uncomment this line.
        #exit
    }
    
    # Any code executed at this point will execute in an admin PS session.
    ping google.com
    

相关问题