使用 Invoke-VMScript Powershell 7 在虚拟机上以管理员身份运行 .exe

问题描述 投票:0回答:1

我需要能够通过 shell 控制台或 powershell 在 powershell 7 中具有管理员窗口来启动命令。

我已经使用 Start-Process 启动了多个命令,但没有一个返回所需的值。我回来后得知我需要一个交互式屏幕,或者只是一个错误。

Start-Process PowerShell -ArgumentList "-noexit","-File", "file url" -verb runas -Wait
Start-Process PowerShell -ArgumentList "-noexit","-File", "file url" -cred credentials -Wait

我还使用了我找到的一个函数,但它也没有返回任何内容,它给了我一个错误,它无法添加行 '$startinfo.verb = "RunAs"'。

Function Elevate-Process  {

    param ([string]$arguments)

    $startinfo = New-Object System.Diagnostics.ProcessStartInfo

    $startinfo.FileName = "powershell.exe"

    $startinfo.Arguments = $arguments

    $startinfo.verb = "RunAs"

    $startinfo.UseShellExecute = $false

    $startinfo.CreateNoWindow = $true

    $process = [System.Diagnostics.Process]::Start($startinfo)

}

所有这一切都是通过 Invoke-VMScript 启动的

powershell virtual-machine invoke-command powershell-7
1个回答
0
投票

我没有使用 VMScript 的经验,但我最近也有类似的效果。
我的情况是:
(1) 使用 从 powershell 脚本中提升 cmd 文件 myCmdFile.bat

Start-Process aCmdFile.bat -Verb RunAs

(2) myCmdFile.bat的内容是

pwsh.exe -File "C:\Mark\Temp\myScript.ps1" -ExecutionPolicy Bypass -wait:$true .... (and all other parameters)
pause

(这里不需要-Verb RunAs,因为它继承了cmd文件的管理员权限。)

这对我有用,但是当我使用 powershell.exe 而不是 pwsh.exe 时,什么也没发生。顺便说一句,如果 cmd 文件位于网络驱动器上并且您没有破解注册表。
所以尝试使用pwsh.exe

© www.soinside.com 2019 - 2024. All rights reserved.