即使作为管理员,FileSystemObject.CopyFile上的权限也被拒绝

时间:2014-02-16 07:37:15

标签: vba vbscript

所以我是Windows部署的新手,所以我可能在这里做了一些基本的错误。我正在尝试使用MDT

在Windows部署期间将脚本复制到Windows目录中的文件夹

基本上我想要的是尝试将脚本复制到%windir%\ temp \ deploymentscripts文件夹,但即使是管理员,我也会被拒绝。我会完成我认为我正在做的事情

First, elevate to admin
Create %WinDir%\Temp\DeploymentScripts
Copy DefaultShell.vbs to that directory (this is where I get permission denied
Mount ntuser.dat to the registry
Set DefaultShell.vbs to the Run Once for default users
Unmount ntuser.dat

这是实际的代码

Option Explicit 

If WScript.Arguments.length = 0 Then
Dim wshShell : Set wshShell = CreateObject("Shell.Application")
wshShell.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" &_
 " RunAsAdministrator", , "runas", 1
Else

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell : Set objShell = WScript.CreateObject("WScript.Shell")
Dim TempDir
Dim ParentDir
Dim FullPath
TempDir = objShell.ExpandEnvironmentStrings("%WinDir%\Temp\DeploymentScripts")

If Not (objFSO.FolderExists(TempDir)) Then
objFSO.CreateFolder (TempDir)
End If

ParentDir = objFSO.GetParentFolderName(Wscript.ScriptFullName)
FullPath = ParentDir & "\DefaultShell.vbs"

objFSO.CopyFile FullPath, TempDir, True

objShell.run "reg load HKU\ZZZ C:\users\default\ntuser.dat"
objShell.RegWrite "HKU\ZZZ\Software\Microsoft\Windows\RunOnce\DefaultShell", _
"WScript.exe" & " " & FullPath, "REG_SZ"
objShell.run "reg unload HKU\ZZZ"

End If

1 个答案:

答案 0 :(得分:2)

一些事情:

  1. tempDir变量设置不正确。添加尾部斜杠,这应该有效。例如:

    TempDir = objShell.ExpandEnvironmentStrings("%WinDir%\Temp\DeploymentScripts\")

  2. 一个。您指向错误的注册表位置 - 您缺少CurrentVersion

    湾您引用HKU的方式在regwrite指令中可能会导致错误。如果它确实导致错误,仅对该行,将其更改为阅读HKEY_USERS的{​​{1}} insdead。

    例如:

    HKU

  3. 为了确保您的脚本按顺序运行,我添加了几条objShell.RegWrite "HKEY_USERS\ZZZ\Software\Microsoft\Windows\Currentversion\RunOnce\defaultshell", _ "WScript.exe" & " " & FullPath, "REG_SZ"指令。没有它,即使你还没有完成加载蜂巢,脚本也会继续处理。我想它只是告诉脚本等到StdOut.ReadAll()命令完成后再转到下一条指令。

  4. 以下是您的脚本的修订版本,其中包含了这些建议。

    reg