Vistaにおいて、VBScriptスクリプト中で管理者権限に移行する方法
2008年4月12日
SFC mini用の新しいinstall.vbsが完成。
Vistaかどうかのチェック方法として、『WSHのバージョンが5.7以上』かつ『Windowsのバージョンが6.0以上』で調べている。これに該当する場合、Shell.ApplicationオブジェクトのShellExecuteメソッドを利用して管理者権限でwscript.exeを起動しなおす(このとき、引数に『vista』を指定)。最後に、regsvr32.exeを実行して終了。
option explicit
Dim wmi,os,value,wsh,sha,sfo
Set sfo=CreateObject("Scripting.FileSystemObject")
do while WScript.Arguments.Count=0 and WScript.Version>=5.7
'Check if Vista
Set wmi = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set os = wmi.ExecQuery ("SELECT * FROM Win32_OperatingSystem")
For Each value in os
if left(value.Version,3)<6.0 then exit do 'Exit if not Vista
Next
'Run this script as admin.
Set sha=CreateObject("Shell.Application")
sha.ShellExecute "wscript.exe",""""+WScript.ScriptFullName+""" vista","","runas"
WScript.Quit
loop
'Current directory may be moved to system32, maybe not.
Set wsh=CreateObject("WScript.Shell")
wsh.run "regsvr32.exe """+sfo.GetAbsolutePathName(WScript.ScriptFullName & "\..\" & "sfcmini.dll")+""""Vistaかどうかのチェック方法として、『WSHのバージョンが5.7以上』かつ『Windowsのバージョンが6.0以上』で調べている。これに該当する場合、Shell.ApplicationオブジェクトのShellExecuteメソッドを利用して管理者権限でwscript.exeを起動しなおす(このとき、引数に『vista』を指定)。最後に、regsvr32.exeを実行して終了。