将 CurrentDirectory 从未提升的脚本转移到提升的脚本 [英] transfer CurrentDirectory from un-elevated script to elevated one

查看:19
本文介绍了将 CurrentDirectory 从未提升的脚本转移到提升的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将与脚本位于同一目录(在我的闪存驱动器中)的文件manufacturer.bmp"复制到 system32 目录.

I need to copy my file "manufacturer.bmp", wich is located in the same directory as the script (in my flash drive), to the system32 directory.

我成功地获取了变量 sourcefiledestinationdirectory,并提升了我的脚本,但是当我提升它时,我的 sourcefile 变量丢失,因为使用了 CurrentDirectory,在此模式下有所不同.

I succeed, in getting the variables sourcefile, destinationdirectory, and to elevate my script, but when I elevate it, my sourcefile variable is lost, because of the use of CurrentDirectory, which differs in this mode.

Set shell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

CurrentDirectory = fso.GetAbsolutePathName(".")
sourcefile = fso.buildpath(CurrentDirectory, "manufacturer.bmp")
MsgBox(sourcefile)

'Checks if the script is running elevated (UAC)
Function isElevated
  Set shell = CreateObject("WScript.Shell")
  Set whoami = shell.Exec("whoami /groups")
  Set whoamiOutput = whoami.StdOut
  strWhoamiOutput = whoamiOutput.ReadAll

  If InStr(1, strWhoamiOutput, "S-1-16-12288", vbTextCompare) Then 
    isElevated = True
  Else
    isElevated = False
  End If
End Function

'Re-runs the process prompting for priv elevation on re-run
Sub uacPrompt
  'Check if we need to run in C or W script
  interpreter = "wscript.exe"
  If InStr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
    interpreter = "wscript.exe"
  Else
    interpreter = "cscript.exe"
  End If

  'Start a new instance with an elevation prompt first
  Set shellApp = CreateObject("Shell.Application")
  shellApp.ShellExecute interpreter, Chr(34) & WScript.ScriptFullName & _
    Chr(34) & " uac", "", "runas", 1

  'End the non-elevated instance
  WScript.Quit
End Sub

'Make sure we are running elevated, prompt if not
If Not isElevated Then uacPrompt

destinationdir = fso.buildpath(shell.ExpandEnvironmentStrings("%windir%"), _
                 "system32")
MsgBox(destinationdir)

fso.CopyFile sourcefile, destinationdir

知道如何将我的源文件 var 推送到子提升脚本吗?

Any idea of how to push my sourcefile var to the child elevated script?

推荐答案

ShellExecute 方法允许您将工作目录指定为 3rd 参数,因此您可以传递当前目录到提升的脚本并在提升后构建 sourcefile 路径.此外,您的代码可以简化很多.

The ShellExecute 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 & "\"

编辑:或者至少如果您不提升流程,它会如何工作.根据 Raymond Chen 的这篇博文,提升进程时将忽略起始目录,因此当前目录中的恶意 DLL 不会错误地加载到提升的进程中.这意味着您必须手动"传递工作目录,如下所示:

or at least that's how it would work if you weren't elevating the process. According to this blog post from Raymond Chen the start directory is ignored when elevating processes, so that malicious DLLs from the current directory aren't loaded into elevated processes by mistake. Meaning that you must pass the working directory "manually", like this:

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 & "\"

请注意,由于您的目标路径是一个文件夹,所以它必须尾部有反斜杠(如 已记录).

Note that since your destination path is a folder, it must have a trailing backslash (as documented).

这篇关于将 CurrentDirectory 从未提升的脚本转移到提升的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆