Run shell as Administrator

I’m trying to create like a MDM program to update machines and commands on my work. The remote connection works perfectly. I’m using Chocolatey package manager (like apt-get for windows) to install software in all the machines, but when I send the update command (ej: cmd.exe chocolatey update all) I need to do it in Administrator Console. How can I get run a shell in administrator mode?? It’s possible? Are there any solution or I can’t finish my project?
Thanks

You will need your app to run as administrator, just the same as the command prompt.

See
http://answers.microsoft.com/en-us/windows/forum/windows_vista-security/always-run-cmdexe-as-administrator-the-journey/eff735b4-9c65-444f-a943-e1707f74cd14?auth=1
http://stackoverflow.com/questions/1726048/runasadmin-in-registry-doesnt-seem-to-work-in-windows-7

Have a look at http://developer.xojo.com/elevating-user-access-control

@Wayne Golding This ^ is great stuff. :slight_smile:

@Wayne Golding

How do I modify the ExecuteWithUAC method to just execute some shell commands with elevated privileges? I’m not really sure how to use this. Am I supposed to put the path to the exe of the program that is running the method to get this working?

You should be able to pass “CMD.EXE” as the program & the commands as the args. You shouldn’t need to have the full path to cmd.exe, however if you do use “%windir%\system32\cmd.exe”.

@Wayne Golding

I actually tried that and I couldn’t get it working. I’ll try again, I’ve almost completed a console application I was going to let it do the shell work and try to call it via the method and see if that worked for me better. Thank you.

Maybe "CMD.EXE/C " followed by command - don’t as args? Don’t have time to try it myself just now.

So I’m getting closer I think. I only made one change to the ExecuteWithUAC method.

I have a global shell property called mShell I want to use so I can see what the shell returns in a textarea.

So in my window open I have this:

mShell = New Shell mShell.Mode = 1 //Asynchronous

And I altered the ExecuteWithUAC method like this:

[code] Dim f As FolderItem
Dim t As TextOutputStream
Dim script As String = “Set objShell = CreateObject(”“Shell.Application”")" + EndOfLine _

  • “objShell.ShellExecute “””", “”"", “”"", ““runas””, 1" + EndOfLine

Dim s As String
f = GetTemporaryFolderItem
f = GetFolderItem(f.NativePath + “.vbs”)
t = TextOutputStream.Create(f)
s = ReplaceAll(script, “”, program)
s = ReplaceAll(s, “”, ReplaceAll(args, chr(34), chr(34) + chr(34)))
t.WriteLine(s)
t.Close

mShell.Execute("Wscript.exe " + f.NativePath)[/code]

I’m trying to execute the method like this:

[code] Dim strName As String
Dim strPath As String

strName = “My Service Name”

strPath = chr(34) + strName + chr(34)

ExecuteWithUAC(“CMD.exe”, "SC Start " + strPath)[/code]

I get the UAC prompt but then it’s just a command prompt at C:\ and no code executes?

No problem thanks I’ll keep trying. :slight_smile:

Have you tried using sc as the program?

ExecuteWithUAC("SC", "Start " + strPath)

DUH. :confused:

That got it! :slight_smile:

THANKS!!!

@Wayne Golding

This is a really nifty piece of code! Thanks again for sharing it!

Glad you got it working :slight_smile:

@Wayne Golding

I actually ended up writing a console app to do the service stop/start work for me because there was a UAC prompt for both service stop and start. This way I use the ExecuteWithUAC method to start the console app and there is only one UAC prompt and the service app can then execute multiple commands.

I wrote a method that checks to see if the service is running too. Somebody might find it useful.

  ' Check to see if My Service is running and continue...
  
  dim sh as new shell
  sh.mode = 0
  
  dim strName As String
  dim strArgs As String
  dim strCommand as String
  
  dim s as string
  dim i as integer
  
  isStart = false
  isStop = false
  
  dim intCheck as integer = 0
  
  strName = chr(34) + "My Service" + chr(34)
  strArgs = chr(34) + "STATE" + chr(34)
  
  strCommand = "sc query " + strName + " | findstr /i " + strArgs
  
  sh.Execute(strCommand)
  
  s = sh.Result
  
  i = InStr(s, "RUNNING")
  
  if i > 0 then
    intCheck = 1
  else
    intCheck = 2
  end
  
  ' Perform service action based on result
  Select Case intCheck
    
  Case 1
    ' RUNNING
    isStart = true
    isStop = false
    
    strResult = "My Service is STARTED."
  Case 2
    ' STOPPED
    isStop = true
    isStart = false
    
    strResult = "My Service is STOPPED."
  End Select
  
  return strResult

:slight_smile: