[英]Permission Denied on FileSystemObject.CopyFile even as Administrator
So I'm new to Windows Deployments so I may be doing something basic wrong here. 所以我是Windows部署的新手,所以我可能在这里做一些基本的错误。 I'm trying to copy a script to a folder in the windows directory during a windows deployment using MDT
我正在尝试使用MDT进行Windows部署期间将脚本复制到Windows目录中的文件夹中
Basically what I'm going for is trying to copy a script to the %windir%\\temp\\deploymentscripts folder but I get permission denied even as admin. 基本上,我想要的是尝试将脚本复制到%windir%\\ temp \\ deploymentscripts文件夹,但是即使以管理员身份我也被拒绝权限。 I'll run through what I think I'm doing
我会经历我认为正在做的事情
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
Here's the actual code 这是实际的代码
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
A few things: 一些东西:
The tempDir
variable was not set right. tempDir
变量设置不正确。 Add a trailing slash, and this should work. 添加一个斜杠,这应该工作。 eg:
例如:
TempDir = objShell.ExpandEnvironmentStrings("%WinDir%\\Temp\\DeploymentScripts\\")
a. 一种。 You were pointing to the wrong registry location - you were missing
CurrentVersion
. 您指向错误的注册表位置-您缺少
CurrentVersion
。
b. 湾 The way you are referencing
HKU
In the regwrite
instruction is likely to cause errors. 您在
regwrite
指令中引用HKU
的方式可能会导致错误。 If it does cause an error, for that line only, change it to read HKEY_USERS
insdead of HKU
. 如果确实导致错误,则仅将该行更改为读取
HKU
HKEY_USERS
insdead。
eg: 例如:
objShell.RegWrite "HKEY_USERS\\ZZZ\\Software\\Microsoft\\Windows\\Currentversion\\RunOnce\\defaultshell", _ "WScript.exe" & " " & FullPath, "REG_SZ"
To be sure that your script runs in sequence, I've added a couple of StdOut.ReadAll()
instructions. 为确保您的脚本按顺序运行,我添加了一些
StdOut.ReadAll()
指令。 Without that, the script will continue processing even if you haven't finished loading the hive yet. 否则,即使您尚未完成配置单元的加载,脚本也将继续处理。 I guess it just simply tells the script to wait until the
reg
command finishes before moving on to the next instruction. 我想这只是告诉脚本要等到
reg
命令完成之后再继续执行下一条指令。
Here's a revised version of your script, with these suggestions incorporated. 这是脚本的修订版,其中包含了这些建议。
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, ParentDir, FullPath, Regload, Regresp
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
Set regload = objShell.exec ("reg load HKU\ZZZ C:\users\default\ntuser.dat" )
regresp = regload.StdOut.ReadAll()
objShell.RegWrite "HKEY_USERS\ZZZ\Software\Microsoft\Windows\Currentversion\RunOnce\DefaultShell", _
"WScript.exe" & " " & FullPath, "REG_SZ"
Set regload = objShell.exec ("reg unload HKU\ZZZ")
regresp = regload.StdOut.ReadAll()
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.