The ShellExecute https://msdn.microsoft.com/en-us/library/windows/desktop/gg537745%28v=vs.85%29.aspx method allows you to specify the working directory as the 3rd argument, so you can pass the current directory to the elevated script and build the sourcefile
path after elevation. Also, your code could be streamlined quite a bit.
Const HKLM = &h80000002
Const DELETE = &h10000
Set sh = CreateObject("WScript.Shell")
Set reg = GetObject("winmgmts://./root/default:StdRegProv")
reg.CheckAccess HKLM, "SYSTEM\CurrentControlSet", DELETE, isAdmin
If Not isAdmin Then
If WScript.Arguments.Count = 0 Then
CreateObject("Shell.Application").ShellExecute WScript.FullName, _
Chr(34) & WScript.ScriptFullName & Chr(34) & " uac", _
sh.CurrentDirectory, "runas", 1
WScript.Quit 0
Else
WScript.Echo "Privilege elevation failed!"
WScript.Quit 1
End If
End If
Set fso = CreateObject("Scripting.FileSystemObject")
src = fso.BuildPath(sh.CurrentDirectory, "manufacturer.bmp")
dst = fso.buildpath(sh.ExpandEnvironmentStrings("%windir%"), "system32")
fso.CopyFile src, dst & "\"
Edit:或者至少如果你不提升流程的话它会是这样的。根据这篇博文 http://blogs.msdn.com/b/oldnewthing/archive/2007/12/11/6648397.aspxRaymond Chen 表示,提升进程时会忽略起始目录,因此当前目录中的恶意 DLL 不会被错误地加载到提升的进程中。这意味着您必须“手动”传递工作目录,如下所示:
Const HKLM = &h80000002;
Const DELETE = &h10000;
Set sh = CreateObject("WScript.Shell")
Set reg = GetObject("winmgmts://./root/default:StdRegProv")
reg.CheckAccess HKLM, "SYSTEM\CurrentControlSet", DELETE, isAdmin
If Not isAdmin Then
If WScript.Arguments.Count = 0 Then
CreateObject("Shell.Application").ShellExecute WScript.FullName, _
Chr(34) & WScript.ScriptFullName & Chr(34) & " " & _
Chr(34) & sh.CurrentDirectory & Chr(34), , "runas", 1
WScript.Quit 0
Else
WScript.Echo "Privilege elevation failed!"
WScript.Quit 1
End If
End If
sh.CurrentDirectory = WScript.Arguments(0)
Set fso = CreateObject("Scripting.FileSystemObject")
src = fso.BuildPath(sh.CurrentDirectory, "manufacturer.bmp")
dst = fso.buildpath(sh.ExpandEnvironmentStrings("%windir%"), "system32")
fso.CopyFile src, dst & "\"
请注意,由于您的目标路径是一个文件夹,因此must有一个尾部反斜杠(如有记录的 https://msdn.microsoft.com/en-us/library/e1wf9e7w%28v=vs.84%29.aspx).