Windows 7 从VBScript提升权限
我们经营动力大奖赛。由于它存储表单/报表的方式,我需要一些安装脚本将.SET文件复制到程序目录中。这可以手动完成,但是让用户运行安装程序脚本为他们安装正确的文件要快得多 我一直在构建一个VBScript安装程序,它可以复制周围的必要文件。棘手的是,有些客户端运行的是Windows XP,有些客户端运行的是Windows 7(甚至是8)。UAC已启用,因此权限开始发挥作用 我尝试这样做的方式是盲目尝试复制文件,如果检测到权限错误,它将使用管理员权限重新启动脚本。我们遇到的问题是某些(所有?)Windows 7计算机启用了虚拟化文件/注册表写入,因此当脚本尝试将文件复制到C:\Program files\Microsoft Dynamics\GP2010时,它会自动失败并将其复制到用户的AppData\Local\VirtualStore目录。这不适用于GP 因此,我需要做的是让脚本将文件复制到C:\Program文件(而不是VirtualStore目录),并仅在必要时提升权限。如果我让它全面提升,这会导致WindowsXP机器在启动脚本时只弹出一个神秘的“运行方式”对话框 以下是我目前掌握的情况:Windows 7 从VBScript提升权限,windows-7,vbscript,Windows 7,Vbscript,我们经营动力大奖赛。由于它存储表单/报表的方式,我需要一些安装脚本将.SET文件复制到程序目录中。这可以手动完成,但是让用户运行安装程序脚本为他们安装正确的文件要快得多 我一直在构建一个VBScript安装程序,它可以复制周围的必要文件。棘手的是,有些客户端运行的是Windows XP,有些客户端运行的是Windows 7(甚至是8)。UAC已启用,因此权限开始发挥作用 我尝试这样做的方式是盲目尝试复制文件,如果检测到权限错误,它将使用管理员权限重新启动脚本。我们遇到的问题是某些(所有?)Win
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
Dim WSHShell、FSO、桌面、桌面路径
设置FSO=CreateObject(“Scripting.FileSystemObject”)
设置WSHShell=CreateObject(“WScript.Shell”)
Desktop=WSHShell.SpecialFolders(“桌面”)
DesktopPath=FSO.GetAbsolutePathName(桌面)
'将工作目录设置为脚本所在的目录。
'如果脚本是
'从ShellExecute或电子邮件中的链接开始,因此中断
'相对路径。
WSHShell.CurrentDirectory=FSO.GetFile(WScript.ScriptFullName).ParentFolder
出错时继续下一步
如果存在FSO.folder(“C:\ProgramFiles(x86)”,则
Echo“安装64位。”
FSO.CopyFile“64位\*.set”,“C:\Program Files(x86)\Microsoft Dynamics\GP2010\”,True
FSO.CopyFile“64位\*.lnk”,DesktopPath,True
ElseIf FSO.FolderExists(“C:\Program Files\Microsoft Dynamics\GP2010\Mekorma MICR”)然后
Echo“安装32位(使用MICR)。”
FSO.CopyFile“32位MICR\*.set”,“C:\Program Files\Microsoft Dynamics\GP2010\”,True
FSO.CopyFile“32位MICR\*.lnk”,DesktopPath,真
其他的
Echo“安装32位。”
FSO.CopyFile“32位\*.SET”,“C:\Program Files\Microsoft Dynamics\GP2010\”,True
FSO.CopyFile“32位\*.lnk”,DesktopPath,真
如果结束
如果错误编号=70,则
CreateObject(“Shell.Application”).Shell执行“wscript.exe”、“wscript.ScriptFullName&”、“runas”、“1”
WScript.Quit
否则错误号为0
MsgBox“错误”和错误编号、vbCrLf和错误源、vbCrLf和错误说明
其他的
MsgBox“已成功安装。”
如果结束
总而言之:如何让VBScript提升权限而不会导致XP在“运行方式”对话框中暂停,也不会导致Windows 7将文件复制到AppData\Local\VirtualStore?似乎这是最简单的方法
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
改进了@db2-answer:
- 真正的提升测试,不依赖于传递的参数
- 将所有原始参数传递给提升的脚本
- 使用与初始脚本相同的主机:
,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
看起来你自己回答了,但是我会测试当前登录的用户是否有管理员权限,而不是Win版本,如果需要,会提升。