PowerShell 的運作方式與以管理者身分啟動 PowerShell 不同

PowerShell 的運作方式與以管理者身分啟動 PowerShell 不同

在提升的 PowerShell 控制台中執行以下命令可以運作並建立虛擬磁碟機。

New-VHD -Path c:\vm_vol\Base.vhdx -SizeBytes 10GB

但是,在非提升 shell 中執行以下命令:

runas /user:$env:userdomain\$env:username "New-VHD -Path c:\vm_vol\Base.vhdx -SizeBytes 10GB"

提示輸入密碼,並嘗試執行命令:

Enter the password for dom1\user1:  
Attempting to start New-VHD -Path c:\vm_vol\Base.vhdx -SizeBytes 10GB as user "dom1\user1" ...

但隨後給了錯誤:

RUNAS ERROR: Unable to run - New-VHD -Path c:\vm_vol\Base.vhdx -SizeBytes 10GB
2: The system cannot find the file specified.

如何讓此指令在非提升 shell 中運作?

答案1

作為寵物系列建議,使用runas /user:$env:userdomain\$env:username "powershell -NoExit", "Get-VM"會起作用。

然而,從長遠來看,我做了以下事情來使事情變得不那麼冗長。

在提升的 PowerShell 會話中執行命令的函數。

psudo.ps1:
function psudo ([string]$cmd = "echo Hello!") {
    Start-Process -FilePath "powershell" -Verb RunAs -ArgumentList "-NoExit", $cmd
}

if ($args[0] -eq $null) {
    $cmd = "echo 'please provide powershell command to execute in elevated shell as a quoted string'"
} else {
    $cmd = $args[0]    
}

psudo $cmd

psudo從非提升 shell運行:

psudo "echo 'This is a test using psudo'"

導致高架控制台保持開啟狀態:

This is a test using psudo

運行psudo以執行 PowerShell cmdlet Get-VM,這需要提升:

psudo Get-VM

導致高架控制台保持開啟狀態:

Name    State CPUUsage(%) MemoryAssigned(M) Uptime   Status             Version
----    ----- ----------- ----------------- ------   ------             -------
test_vm Off   0           0                 00:00:00 Operating normally 9.0

我真的希望我可以讓它與 an 一起使用,Invoke-Command這樣我就可以在同一個控制台中看到結果,而不必每次我想以管理員身份運行命令時打開一個單獨的視窗。

但是,我無法獲得類似於以下內容的任何版本來工作:

取得並儲存憑證

腳本檔案getcreds.ps1保存在我的使用者資料夾中的目錄中,該目錄也在我的$env:PATH變數中。這樣我只需輸入一次密碼,每次需要更改安全性原則時也只需輸入一次。

getcreds.ps1:
function getcurcreds {
    $pwd = Get-Content "$HOME\ExportedPassword.txt" | ConvertTo-SecureString
    $cred = New-Object System.Management.Automation.PSCredential -ArgumentList "$env:USERDOMAIN\$env:username", $pwd

    Add-Type -AssemblyName System.DirectoryServices.AccountManagement -ErrorAction Stop
    $Domain = $env:USERDOMAIN
    $ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
    $pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext $ct, $Domain
    $validated = $pc.ValidateCredentials($cred.UserName, $cred.GetNetworkCredential().password)

    if (-Not $validated) {
        $cred = Get-Credential -UserName "$env:USERDOMAIN\$env:username" -Message "WK NA password"
        Set-Content "$HOME\ExportedPassword.txt" ($cred.Password | ConvertFrom-SecureString)
    }
    $cred
}

getcurcreds

函數修改為在提升的 PowerShell 會話中執行命令並將結果傳回目前非提升的 PowerShell 工作階段。

psudo.ps1:
$cred = getcreds

function psudo ([string]$cmd = "echo Hello!") {
    $cmdBlk = [scriptblock]::Create(
        "Start-Process -FilePath 'powershell' -Verb RunAs -ArgumentList '-NoExit', $cmd"
    )
    Invoke-Command -ComputerName $env:computername $cmdBlk -RunAsAdministrator -Credential $cred
}

我不斷收到以下錯誤:

Invoke-Command : Parameter set cannot be resolved using the specified named parameters.

或者

cmdlet Invoke-Command at command pipeline position 1
Supply values for the following parameters:
ContainerId[0]:

相關內容