0

I have an elevated C# exe file that can run elevated commands using System.Diagnostics.Process just fine, however I need to run a specifically unelevated command - that being subst. I am not sure whether I should (or respectively could) make a secondary .exe file or .bat or something? There is an alternative, as there is only one command that requires admin privileges to run - that being a WSL mount command. Can I make the C# script unelevated and just elevate that one specific command, or vice versa?

I've tried using runas /trustlevel:0x20000, but that throws an access denied (even though the directory being accessed by the SUBST command is \\WSL$\

As for the alternative solution I've tried the following code:

startInfo startInfo = new ProcessStartInfo(); 
startInfo.FileName = "cmd.exe"; 
startInfo.Arguments = $"/c powershell -command wsl --mount \\.\PHYSICALDRIVE1 --partition 2;pause" 
startInfo.Verb = "runas";   
Process proc = Process.Start(startInfo)
proc.WaitForExit(); 

That opens up an UAC prompt, opens up an elevated powershell Window, but for some reason doesn't recognize "WSL" as a command, even when using it's absolute path.

Fixing either method would fix the issue.

  • 4
    Your idea of running unelevated, and only running one specific command elevated is a much better idea. – Neil Jan 22 '23 at 13:17
  • 1
    Also why start cmd to start powershell, why not start powershell directly? Or for that matter run wsl.exe directly? – Charlieface Jan 22 '23 at 14:40
  • I actually just found out, that you could run wsl commands from CMD directly, I used powershell as a guaranteed way for it to work. Using CMD returns the same 'WSL Not found' as well, though I just found out that for some reason thecode starts a shell from SYSWOW64 instead of SYSTEM32 – Cpt Dingus Jan 22 '23 at 14:51
  • The following may be helpful: https://stackoverflow.com/a/74005878/10024425, https://stackoverflow.com/questions/72678147/an-attempt-was-made-to-reference-a-token-that-does-not-exist-process-start-adm/72678720#72678720, and https://stackoverflow.com/a/51146532/10024425 – Tu deschizi eu inchid Jan 22 '23 at 16:37
  • On a Windows 64 machine the dlls in the System32 folder are really 64 bit. I do not think the issue is with WSL not being found. The real error is "access denied". PS does not start as Admin. So I think solution is to start PS by right click shortcut and select Run As Admin. Then you should not get "access denied". – jdweng Jan 22 '23 at 19:10

0 Answers0