First of all, the commands that i want to execute are get-vm
and start-vm
, which are need admin permission.
In my app , i will check the hyper-v vm state to see it Running or Off. thus , i need to invoke get-vm {vmName}
the get the state. after got the vm state , if the state is Off, then i will invoke start-vm {vmName}
to start the vm.
secondly, i use Process to start PowerShell with UserName and Password. the code is below:
var tmp = CShortPath.GetShortPath(System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName()));
var processInfo = new ProcessStartInfo
{
Verb = "runas",
LoadUserProfile = true,
CreateNoWindow = true,
FileName = "powershell.exe",
//Arguments = "Start-VM -name 'win11-Lite'",
Arguments = $"get-VM 'win11-Lite' >{tmp}",
RedirectStandardOutput = false,
UseShellExecute = false,
UserName = "{AdminUserName}",
Password = MakePwd("AdminPwd")
};
Process.Start(processInfo);
as you see, when executed , the output file {tmp} is empty, and there will not display UAC Permission dialog. what should do ?