Permission elevation from VBScript

前端 未结 2 961
时光说笑
时光说笑 2020-12-17 02:00

We run Dynamics GP. Because of the way it stores forms/reports, I need to have some install scripts that copy a .SET file into the program directory. This can be done manual

相关标签:
2条回答
  • 2020-12-17 02:10

    Improved on @db2 answer:

    • real elevation testing, without depending on passed arguments
    • passes all original arguments to the elevated script
    • uses the same host of the initial script: wscript.exe, cscript.exe, whatever

    Code:

    Set OSList = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
    For Each OS In OSList
        If InStr(1, OS.Caption, "XP") = 0 And InStr(1, OS.Caption, "Server 2003") = 0 Then
            With CreateObject("WScript.Shell")
                IsElevated = .Run("cmd.exe /c ""whoami /groups|findstr S-1-16-12288""", 0, true) = 0
                If Not IsElevated Then
                    Dim AllArgs
                    For Each Arg In WScript.Arguments
                        If InStr( Arg, " " ) Then Arg = """" & Arg & """"
                        AllArgs = AllArgs & " " & Arg
                    Next
                    Command = """" & WScript.ScriptFullName & """" & AllArgs
                    With CreateObject("Shell.Application")
                        .ShellExecute WScript.FullName, " //nologo " & Command, "", "runas", 1
                        WScript.Echo WScript.FullName & " //nologo " & Command
                    End With
                    WScript.Quit
                End If
            End With
        End If
    Next
    
    ' Place code to run elevated here
    
    0 讨论(0)
  • 2020-12-17 02:34

    Seems like this is the simplest way to do it.

    1. Check OS version.
    2. If it's not XP or 2003 (I don't anticipate this running on anything older), re-execute with elevation.

    Here's the code block I added to the beginning of the script:

    Dim OSList, OS, UAC
    UAC = False
    If WScript.Arguments.Count >= 1 Then
        If WScript.Arguments.Item(0) = "elevated" Then UAC = True
    End If
    
    If Not(UAC) Then
        Set OSList = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
        For Each OS In OSList
            If InStr(1, OS.Caption, "XP") = 0 And InStr(1, OS.Caption, "Server 2003") = 0 Then
                CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ elevated" , "", "runas", 1
                WScript.Quit
            End If
        Next
    End If
    
    0 讨论(0)
提交回复
热议问题