Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute external EXE with Admin rights 1

Status
Not open for further replies.

ComputersUnlimited

IS-IT--Management
Dec 18, 2013
37
US
The program I am working on executes another program that requires admin rights. In attempts to get this working I have the following code

Code:
Dim MyProcess As System.Diagnostics.Process = Nothing
        Dim MyProcessStartInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo()

Try
                Dim UserAccount As String = "UserName"
                Dim Password As SecureString = New SecureString()
                Password.AppendChar("m")
                Password.AppendChar("y")
                Password.AppendChar("P")
                Password.AppendChar("a")
                Password.AppendChar("s")
                Password.AppendChar("s")
                Password.AppendChar("w")
                Password.AppendChar("o")
                Password.AppendChar("e")
                Password.AppendChar("g")
                Password.AppendChar("r")
                Password.AppendChar("d")
                Password.MakeReadOnly()

                With MyProcessStartInfo
                    .FileName = Path
                    .Arguments = Args
                    .Verb = "runas"
                    .UseShellExecute = True
                    .UserName = UserAccount
                    .Password = Password
                    .WindowStyle = ProcessWindowStyle.Normal

                End With

                MyProcess = Process.Start(MyProcessStartInfo)

                'Wait until the process passes back an exit code 
                MyProcess.WaitForExit()

                'Free resources associated with this process
                MyProcess.Close()

                'Password.
                Password.Dispose()
                Password.Clear()

            Catch ex As Exception
                MsgBox(ex.Message, "Error") 'for testing and trouble shooting purposes only

            End Try

When this line executes
Code:
MyProcess = Process.Start(MyProcessStartInfo)
an exception is thrown. (See Image)

If I comment out the UserName and Password lines the process starts but I am prompted to allow elevated rights, which is what I don't want as this program is intended to run unattended.

Am I going about this the proper way? Is there an easier or better way? This is the method almost every site I found says to use. I did see one site that said that secure password should not be use as it did not deliver what was promised, keeping the password or other sensitive data secure, but the article did not provide an alternative.

Any assistance is greatly appreciated!
 
 https://files.engineering.com/getfile.aspx?folder=1e273350-e8b2-43d4-985d-e288048e2cc9&file=VB_Errror.PNG
>I am prompted to allow elevated rights, which is what I don't want

No easy way of avoiding this, I'm afraid. The technique you are using above is NOT designed to bypass the UAC prompt

>I did see one site that said that secure password should not be use as it did not deliver what was promised

It simply isn't quite as secure as the name might suggest. it is, however, more secure than a regular string. But that is irrelevant - if you are using ProcessStartInfo there is no avoiding using one, even if you wanted to..

> Is there an easier or better way?

No, not really. Avoiding UAC is hard when attempting to elevate privileges. There's a reason for that ...

One technique is to run the launching application with elevated rights (e.g use a manifest) - but that just moves the point the UAC prompt appears ...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top