I need to be able to launch command by shell console or powershell having the administrator window in powershell 7.
I have launched several commands with Start-Process but none of them returned the desired value. I was getting back that I needed an interactive screen or just an error.
Start-Process PowerShell -ArgumentList "-noexit","-File", "file url" -verb runas -Wait
Start-Process PowerShell -ArgumentList "-noexit","-File", "file url" -cred credentials -Wait
I also used a function I found, but it doesn't return anything either, it gives me an error that it can't add the line '$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)
}
all of this launched through the Invoke-VMScript
I have no excperience with VMScript but I recently had a similar effect.
My situation was:
(1) elevating a cmd file myCmdFile.bat from a powershell script with
(2) the content of myCmdFile.bat is
(Here is no need for -Verb RunAs because it inherits the admin rights from the cmd file.)
This worked for me, but when I used powershell.exe instead of pwsh.exe nothing happened.
So try to use pwsh.exe!
By the way also if the cmd file is on a network drive and you have not hacked the registry.