Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vbscript 将CurrentDirectory从未提升的脚本传输到提升的脚本_Vbscript_Uac - Fatal编程技术网

Vbscript 将CurrentDirectory从未提升的脚本传输到提升的脚本

Vbscript 将CurrentDirectory从未提升的脚本传输到提升的脚本,vbscript,uac,Vbscript,Uac,我需要将我的文件“manufacturer.bmp”复制到system32目录,该文件与脚本位于同一目录中(在我的闪存驱动器中) 我成功地获取了变量sourcefile,destinationdirectory,并提升了我的脚本,但当我提升脚本时,我的sourcefile变量丢失,因为使用了与此模式不同的CurrentDirectory Set shell = WScript.CreateObject("WScript.Shell") Set fso = CreateObject("Script

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

我成功地获取了变量
sourcefile
destinationdirectory
,并提升了我的脚本,但当我提升脚本时,我的
sourcefile
变量丢失,因为使用了与此模式不同的
CurrentDirectory

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
您知道如何将my sourcefile var推送到提升的子脚本吗?

该方法允许您将工作目录指定为第三个参数,这样您就可以将当前目录传递给提升的脚本,并在提升后构建
源文件
路径。此外,您的代码可以简化很多

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不会错误地加载到提升进程中。这意味着您必须“手动”传递工作目录,如下所示:

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 & "\"
Const HKLM=&H8000002
常量删除=&h10000
Set sh=CreateObject(“WScript.Shell”)
Set reg=GetObject(“winmgmts://./root/default:StdRegProv")
reg.CheckAccess HKLM,“系统\CurrentControlSet”,删除,isAdmin
如果不是我的话
如果WScript.Arguments.Count=0,则
CreateObject(“Shell.Application”).ShellExecute WScript.FullName_
Chr(34)&WScript.ScriptFullName&Chr(34)&_
Chr(34)&sh.CurrentDirectory&Chr(34),“runas”,1
WScript.0退出
其他的
WScript.Echo“权限提升失败!”
WScript.Quit 1
如果结束
如果结束
sh.CurrentDirectory=WScript.Arguments(0)
设置fso=CreateObject(“Scripting.FileSystemObject”)
src=fso.BuildPath(sh.CurrentDirectory,“manufacturer.bmp”)
dst=fso.buildpath(sh.ExpandEnvironmentStrings(“%windir%”),“system32”)
fso.CopyFile src、dst和“\”

请注意,由于您的目标路径是一个文件夹,因此它必须有一个尾随的反斜杠(as)。

非常感谢您的回答和时间,我尝试了您的解决方案,但它似乎不起作用。为了验证,我添加了消息框来输出变量。当提升的脚本运行时,workind目录仍然是C:\windows\system32,我无法使用它的相对路径访问给定的bmp文件。我的想法是暂时将bmp文件复制到一个固定的临时文件夹中,然后在提升时将其复制到System32文件夹中。它与临时目录一起工作:如果不是isAdmin,则sourcefile=fso.buildpath(CurrentDirectory,“manufacturer.bmp”)fso.CopyFile sourcefile,temploc&“\”如果WScript.Arguments.Count=0,则[…]Else temploc=sh.ExpandEnvironmentStrings(“%temp%”)sourcefile=fso.buildpath(temploc,“manufacturer.bmp”)dst=fso.buildpath(sh.ExpandEnvironmentStrings(“%windir%”,“system32”)fso.CopyFile源文件,dst&“\”EndIf@Letouane很抱歉反应太晚。有关解释和解决方法,请参阅答案。尝试了您的答案,效果良好。非常感谢Ansgar(与“延迟响应”无关,您为我提供了帮助)!