windows-7 - 从 VBScript 提升权限

标签 windows-7 vbscript

我们运行 Dynamics GP。由于它存储表单/报告的方式,我需要一些安装脚本将 .SET 文件复制到程序目录中。这可以手动完成,但让用户运行安装程序脚本来为他们​​安装适当的文件会快得多。

我一直在构建一个 VBScript 安装程序来复制必要的文件。棘手的部分是一些客户端运行的是 Windows XP,而一些客户端运行的是 Windows 7(甚至 8)。 UAC 已启用,因此权限发挥作用。

我尝试这样做的方法是盲目尝试复制文件,如果检测到权限错误,它会以管理员权限重新启动脚本。我们遇到问题的地方是一些(全部?)Windows 7 机器启用了虚拟化文件/注册表写入,因此当脚本尝试将文件复制到 C:\Program Files\Microsoft Dynamics\GP2010 时,它会默默地失败并复制它们到用户的 AppData\Local\VirtualStore 目录。这在 GP 上不能正常工作。

所以我需要做的是让脚本将文件复制到 C:\Program Files(不是 VirtualStore 目录),并仅在必要时提升权限。如果我让它全面提升,这会导致 Windows XP 机器在启动脚本时简单地弹出一个神秘的“运行方式”对话框。

这是我到目前为止所拥有的:

Dim WSHShell, FSO, Desktop, DesktopPath
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSHShell = CreateObject("WScript.Shell")
Desktop = WSHShell.SpecialFolders("Desktop")
DesktopPath = FSO.GetAbsolutePathName(Desktop)

'Set working directory to directory the script is in.
'This ends up being C:\Windows\System32 if the script is
'started from ShellExecute, or a link in an email, thus breaking
'relative paths.
WSHShell.CurrentDirectory = FSO.GetFile(WScript.ScriptFullName).ParentFolder

On Error Resume Next

If FSO.FolderExists("C:\Program Files (x86)") Then
    WScript.Echo "Installing 64-bit."
    FSO.CopyFile "64-bit\*.set", "C:\Program Files (x86)\Microsoft Dynamics\GP2010\", True
    FSO.CopyFile "64-bit\*.lnk", DesktopPath, True
ElseIf FSO.FolderExists("C:\Program Files\Microsoft Dynamics\GP2010\Mekorma MICR") Then
    WScript.Echo "Installing 32-bit (with MICR)."
    FSO.CopyFile "32-bit MICR\*.set", "C:\Program Files\Microsoft Dynamics\GP2010\", True
    FSO.CopyFile "32-bit MICR\*.lnk", DesktopPath, True 
Else
    WScript.Echo "Installing 32-bit."
    FSO.CopyFile "32-bit\*.SET", "C:\Program Files\Microsoft Dynamics\GP2010\", True
    FSO.CopyFile "32-bit\*.lnk", DesktopPath, True
End If

If Err.Number = 70 Then
    CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """" , "", "runas", 1
    WScript.Quit
ElseIf Err.Number <> 0 Then
    MsgBox "Error " & Err.Number & vbCrLf & Err.Source & vbCrLf & Err.Description
Else
    MsgBox "Installed successfully."
End If

总结:如何让 VBScript 提升权限,而不会导致 XP 在“运行方式”对话框中停滞,并且不会导致 Windows 7 将文件复制到 AppData\Local\VirtualStore?

最佳答案

改进了@db2 答案:

  • 真正的高程测试,不依赖于传递的参数
  • 将所有原始参数传递给提升的脚本
  • 使用与初始脚本相同的主机:wscript.exe , cscript.exe , 随便

  • 代码:
    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
    

    关于windows-7 - 从 VBScript 提升权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13296281/

    相关文章:

    windows - Windows 管理员和 Windows 系统用户有什么区别

    css - 使用 VBSCRIPT,经典的 ASP 动态设置表单样式

    http - 检查http连接vbscript

    regex - 如何使用 VB 从字符串中删除表情符号

    windows-7 - Windbg 故障转储分析

    security - 从 Windows-7 登录屏幕创建事件

    android - SDK 管理器不会启动 Windows 7 64 位?

    visual-studio-2010 - Windows 7 下的 ClickOnce/Excel-VSTO

    arrays - 大型 VBScript 数组产生 'Out of Memory' 错误

    vbscript - 刷新桌面和任务栏中的所有图标