繁体   English   中英

甚至以管理员身份在FileSystemObject.CopyFile上被拒绝的权限

[英]Permission Denied on FileSystemObject.CopyFile even as Administrator

所以我是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. tempDir变量设置不正确。 添加一个斜杠,这应该工作。 例如:

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

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

    您在regwrite指令中引用HKU的方式可能会导致错误。 如果确实导致错误,则仅将该行更改为读取HKU HKEY_USERS insdead。

    例如:

    objShell.RegWrite "HKEY_USERS\\ZZZ\\Software\\Microsoft\\Windows\\Currentversion\\RunOnce\\defaultshell", _ "WScript.exe" & " " & FullPath, "REG_SZ"

  3. 为确保您的脚本按顺序运行,我添加了一些StdOut.ReadAll()指令。 否则,即使您尚未完成配置单元的加载,脚本也将继续处理。 我想这只是告诉脚本要等到reg命令完成之后再继续执行下一条指令。

这是脚本的修订版,其中包含了这些建议。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM