ファイラーから呼び出すFireFileCopyの昇格をコントロールする
前回の続き。私の環境では、ファイル操作にFireFileCopyを使っている(あふから呼び出す)。
こいつも状況によって一般権限と昇格した権限を使い分ける必要がある。フォルダリダイレクションの対象になっているフォルダ(system32とかProgram Filesとか)の下にファイルをコピーする場合には、昇格した権限を使う必要があるからである。
そこで私が用意したのが、下記2つのスクリプト。
ffc.vbsは、一般権限でFireFileCopyを起動するためのスクリプト。
ファイルのコピー先(Toオプション)がリダイレクション対象であった場合は警告ダイアログを出すようになっている。
ffcElevate.vbsは、昇格した権限でFireFileCopyを起動するためのスクリプト。FireFileCopyは32bitアプリケーションなので、64bit OSでのSystem32のリダイレクトに対する考慮を入れてある。
(「毎回ffcElevate.vbsを使えばいいじゃん!」と思うかもしれないが、ファイルコピーのたびにUACのダイアログが出たらたまらん。)
' Launch FireFileCopy from Afxw
Dim args, arg, options
Dim ws
Dim system32, syswow64, programFiles, programFilesX86
Dim i
Dim confirm
Dim isWow64
options = Split("to,copy,move,sync,a,bg,cb,ed,-ed,ft,jb,kd,lg,md,min,ng,nk,nq,ov,s,sr,st,t,tXY,ts,ts,tp,mid,low,idle,wait1,wait2,wait3,vf,vfl,-vf,vn,wd,ys,?,go!,sel", ",")
Set args = WScript.Arguments
Set ws = CreateObject("WScript.Shell")
isWow64 = False
' If FFC.exe will run on wow64 and
' the destination("/to:" switch) is a VirtualStore
' (%windir%\system32 or
' %windir%\SysWOW64 or
' "Program Files"or
' "Program Files (x86)),
' show confirmation dialog.
If ws.ExpandEnvironmentStrings("%PROCESSOR_ARCHITEW6432%") _
= "AMD64" or _
ws.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") _
= "AMD64" Then
isWow64 = True
End If
If isWow64 Then
system32 = ws.ExpandEnvironmentStrings("%windir%") & _
"\System32"
syswow64 = ws.ExpandEnvironmentStrings("%windir%") & _
"\SysWOW64"
programFiles = ws.ExpandEnvironmentStrings( _
"%ProgramFiles%")
programFilesX86 = ws.ExpandEnvironmentStrings( _
"%ProgramFiles(x86)%")
toFolder = args.Named.Item("to")
If left(toFolder, len(system32)) = system32 Or _
left(toFolder, len(syswow64)) = soswow64 Or _
left(toFolder, len(programFiles)) = programFiles Or _
left(toFolder, len(programFilesX86)) = programFilesX86 Then
confirm = MsgBox( _
"コピー先はVirtualStoreにリダイレクトされます。よろしいですか?", _
49, "FireFileCopy 事前確認")
If confirm = 2 Then
WScript.Quit
End If
End If
End If
' unnamed arguments
For i = 0 To args.unnamed.length - 1
arg1 = args.Unnamed.Item(i)
If InStr(arg1, " ") > 0 Then
arg1 = """" & arg1 & """"
End If
' If FFC.exe will run on wow64,
' replace "system32" to "sysnative" in 'From' path.
If isWow64 Then
arg1 = Replace(arg1, "C:\Windows\System32", _
"C:\Windows\sysnative")
End If
arg = arg & " " & arg1
Next
' named arguments
For i = 0 To UBound(options)
If args.named.exists(options(i)) Then
arg1 = args.Named.Item(options(i))
If Len(arg1) > 0 Then
If InStr(arg1, " ") > 0 Then
arg1 = """" & arg1 & """"
End If
arg1 = ":" & arg1
End If
arg = arg & " /" & options(i) & arg1
End If
Next
ws.Run "%APP%\FireFileCopy\FFC.exe" & arg, vbHide
' Launch FireFileCopy from Afxw with an Elevated user Token.
Dim args, arg, arg1, options
Dim ws
Dim cmdpath
cmdpath = "C:\Windows\System32"
options = Split("to,copy,move,sync,a,bg,cb,ed,-ed,ft,jb,kd,lg,md,min,ng,nk,nq,ov,s,sr,st,t,tXY,ts,ts,tp,mid,low,idle,wait1,wait2,wait3,vf,vfl,-vf,vn,wd,ys,?,go!,sel", ",")
Set args = WScript.Arguments
' unnamed arguments
For i = 0 To args.Unnamed.Length - 1
arg1 = args.Unnamed.Item(i)
If InStr(arg1, " ") > 0 Then
arg1 = """" & arg1 & """"
End If
arg = arg & " " & arg1
Next
' named arguments
For i = 0 To UBound(options)
If args.Named.Exists(options(i)) Then
arg1 = args.Named.Item(options(i))
If len(arg1) > 0 Then
If InStr(arg1, " ") > 0 Then
arg1 = """" & arg1 & """"
End If
arg1 = ":" & arg1
End If
arg = arg & " /" & options(i) & arg1
End If
Next
Set ws = CreateObject("WScript.Shell")
' if this script is running on wow64,
' replace "system32" to "sysnative"
' because ffc.exe is a 32bit application.
If ws.ExpandEnvironmentStrings("%PROCESSOR_ARCHITEW6432%") _
= "AMD64" Then
cmdpath = "C:\Windows\sysnative"
arg = Replace(arg, "C:\Windows\System32", _
"C:\Windows\sysnative")
End If
If ws.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") _
= "AMD64" Then
arg = Replace(arg, "C:\Windows\System32", _
"C:\Windows\sysnative")
End If
ws.Run cmdpath & _
"\cmd.exe /c elevate.cmd %APP%\FireFileCopy\FFC.exe" & _
arg, vbhide
0 コメント:
コメントを投稿