Run command line as administrator

YasserKhalil
PlatinumLounger
Posts: 4265
Joined: 31 Aug 2016, 09:02

Run command line as administrator

Post by YasserKhalil »

Hello everyone
I have found this code that runs command lines from VBA
http://www.vbaexpress.com/kb/getarticle.php?kb_id=971

Code: Select all

Public Sub TestCommandLine()
    Const lngCancelled_c As Long = 0
    Dim strCmd As String
    strCmd = VBA.InputBox("Enter DOS Command:", "Enter Command", "dir")
    If VBA.LenB(strCmd) = lngCancelled_c Then
        Exit Sub
    End If
    CommandLine strCmd, True
End Sub
Public Function CommandLine(command As String, Optional ByVal keepAlive As _
    Boolean = False, Optional windowState As VbAppWinStyle = VbAppWinStyle.vbHide) _
    As Boolean
     '--------------------------------------------------------------------------------
     ' Procedure : CommandLine
     ' Author    : Aaron Bush (Oorang)
     ' Date      : 10/02/2007
     ' Purpose   : Provides a simple interface to execute a command lines from VBA.
     ' Input(s)  :
     '               command     : The DOS command you wish to execute.
     '               keepAlive   : Keeps the DOS window open *after* command has been
     '                             executed. Default behavior is to auto-close. (See
     '                             remarks section for additional information.)
     '               windowState : Determines the window state of the DOS prompt
     '                             *during* command execution.
     ' Output    : True if completed with no errors, False if error encountered.
     ' Remarks   : If the windowState property is set to vbHide while the keepAlive
     '             parameter is set to True, then windowState will be changed to
     '             vbNormalFocus.
     '--------------------------------------------------------------------------------
    On Error GoTo Err_Hnd
    Const lngMatch_c As Long = 0
    Const strCMD_c As String = "cmd.exe"
    Const strComSpec_c As String = "COMSPEC"
    Const strTerminate_c As String = " /c "
    Const strKeepAlive_c As String = " /k "
    Dim strCmdPath As String
    Dim strCmdSwtch As String
    If keepAlive Then
        If windowState = vbHide Then
            windowState = vbNormalFocus
        End If
        strCmdSwtch = strKeepAlive_c
    Else
        strCmdSwtch = strTerminate_c
    End If
    strCmdPath = VBA.Environ$(strComSpec_c)
    If VBA.StrComp(VBA.Right$(strCmdPath, 7), strCMD_c, vbTextCompare) <> _
    lngMatch_c Then
        strCmdSwtch = vbNullString
    End If
    VBA.Shell strCmdPath & strCmdSwtch & command, windowState
    CommandLine = True
    Exit Function
Err_Hnd:
    CommandLine = False
End Function
What I am trying to do is how to give permissions to run the command line as admin?
Any ideas

User avatar
HansV
Administrator
Posts: 69376
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Run command line as administrator

Post by HansV »

See if Running a Command as Administrator in VBA helps. The code is slightly messed up (some line breaks are missing), but you should be able to correct that.
Regards,
Hans

YasserKhalil
PlatinumLounger
Posts: 4265
Joined: 31 Aug 2016, 09:02

Re: Run command line as administrator

Post by YasserKhalil »

I got an error at the hwnd part.

Code: Select all

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

  Sub RunAsAdmin()
    Const SW_NORMAL As Long = 1
    ShellExecute hWndAccessApp(), _
                 "runas", _
                 "c:\windows\system32\cmd.exe", _
                 "/k netsh -c dhcp", _
                 "c:\windows\system32", SW_NORMAL
End Sub

User avatar
HansV
Administrator
Posts: 69376
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Run command line as administrator

Post by HansV »

If you run this from Excel, replace hWndAccessApp() with Application.Hwnd

Also, replace "/k netsh -c dhcp" with the command that you want to run.
Regards,
Hans

YasserKhalil
PlatinumLounger
Posts: 4265
Joined: 31 Aug 2016, 09:02

Re: Run command line as administrator

Post by YasserKhalil »

Thanks a lot.
I am trying to change the system date but this doesn't change it .. No errors now but no expected

Code: Select all

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

  Sub RunAsAdmin()
    Const SW_NORMAL As Long = 1
    ShellExecute Application.hWnd, _
                 "runas", _
                 "c:\windows\system32\cmd.exe", _
                 "date 10/6/2020", _
                 "c:\windows\system32", SW_NORMAL
End Sub

User avatar
HansV
Administrator
Posts: 69376
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Run command line as administrator

Post by HansV »

I can't help you with that. I don't know anything about this stuff.
Regards,
Hans

YasserKhalil
PlatinumLounger
Posts: 4265
Joined: 31 Aug 2016, 09:02

Re: Run command line as administrator

Post by YasserKhalil »

Thanks a lot my tutor.
Waiting for others' ideas.

YasserKhalil
PlatinumLounger
Posts: 4265
Joined: 31 Aug 2016, 09:02

Re: Run command line as administrator

Post by YasserKhalil »

I could solve it out

Code: Select all

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Sub Run_As_Admin()
    Const SW_NORMAL As Long = 1
    ShellExecute Application.hWnd, _
                 "runas", _
                 "C:\Windows\System32\cmd.exe", _
                 "%comspec% /c" & "date 10/6/2020", _
                 "C:\Windows\System32", SW_NORMAL
End Sub