0

I've put code together to be able to open a saved MSC file but this needs to open as another user. This MSC file just opens Active Directory Users and Computers, DHCP, DNS, etc but it's only my admin account that will have permissions to this. I'll also want to do the same for Exchange Management Console which is also an MSC file.

The problem is I get the error "The requested operation requires elevation" I've tested this outside of Visual Studio by running the built exe using my normal account and an elevated admin account and it still gives the same error

Here's the code:

Dim passwordString As String
    passwordString = "blahblahblah"
    Dim password As SecureString = ConvertToSecureString(passwordString)


    Dim p As New ProcessStartInfo

    p.FileName = "mmc.exe"
    p.WorkingDirectory = "C:\Windows\System32"
    ' Use these arguments for the process
    p.Arguments = "mymmc.msc"
    p.Domain = "my.domain"
    p.UserName = "a_myadminaccount"
    p.Password = password
    p.UseShellExecute = False
    ' Start the process
    Process.Start(p)
  • Hello and Welcome to Stack Overflow! For security reasons, even though you're using an admin account, most processes still run with a lower set of privileges. Try right-clicking your executable and press `Run as administrator` to see if that fixes the problem. If it does, the following will help you force it to always run with administrative privileges: https://stackoverflow.com/a/2818776 – Visual Vincent Aug 12 '19 at 18:37
  • No I'm afraid it still displays the same message. If I change the executable to open to Notepad it works fine – Simon Foxwell Aug 13 '19 at 09:23
  • I'm afraid you've somewhat reached an impass, as to elevate another process `UseShellExecute = True` is required (which is not possible since you are also launching it as another user). Notepad works because it doesn't necessarily require elevated privileges, but mmc cannot run without it. In order to do this you will need a process in-between that does the actual elevation. – Visual Vincent Aug 14 '19 at 09:00
  • This article should shed some light on the situation: [Why Can’t I Elevate My Application to Run As Administrator While Using CreateProcessWithLogonW?](http://web.archive.org/web/20190110131943/https://blogs.msdn.microsoft.com/cjacks/2010/02/01/why-cant-i-elevate-my-application-to-run-as-administrator-while-using-createprocesswithlogonw/) – Visual Vincent Aug 14 '19 at 09:00
  • Thanks VV. I read that article and it made sense. The only way I have so far been able to get this to work for me is to run a batch file which elevates another batch file using the RunAs command to open the application. It's a bit messy but it works – Simon Foxwell Aug 15 '19 at 10:25
  • Glad I could help, though it should be enough to start `runas` directly from VB.NET. You shouldn't need any extra batch files. – Visual Vincent Aug 15 '19 at 12:43

0 Answers0