簡體   English   中英

Powershell 為 RunAs 提供憑據

[英]Powershell provide credentials for RunAs

我正在嘗試編寫 PowerShell 腳本以使用用戶/密碼進行自我提升。 問題是, runas 和 -Credential 不能在 Start-Process 中一起使用。 我嘗試在 powershell 中運行 powershell,但它不起作用。 這個想法是用用戶/通行證繞過UAC。 有人有建議嗎?

編輯:這是不可能的!

$username = "test"
$password = "123456"
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))


     # Self-elevate the script if required
    if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
     if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
     
        Write-Host -ForegroundColor 'Red' "Bypassing UAC..."
        Start-Sleep -s 2
        
     
      $CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
      $CommandLine = "Start-Process -FilePath PowerShell.exe -verb runas -ArgumentList " + $CommandLine
      Start-Process powershell.exe -Credential $credentials -ArgumentList $CommandLine
      
        Write-Host -ForegroundColor 'Red' "Bypassing UAC..." $CommandLine
        Start-Sleep -s 30
      
      Exit
     }
    }


Write-Host -ForegroundColor 'Green' "Admin mode..."
Write-Host -ForegroundColor 'Gray' "Shell will exit in 15 seconds..."
Start-Sleep -s 15

UAC 旨在在嘗試提升到管理權限時顯示確認提示。 (當進程已經提升運行時不會出現提示。)如果啟用了 UAC(強烈建議您將其保持啟用狀態),提升提示將始終出現。 您不能繞過(或者,如果您更喜歡其他術語,“以編程方式接受”)UAC 提示。 這是設計使然。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM