2014-02-16 128 views
1

所以我新到Windows部署,所以我在这里可以做一些基本的错误。我试图将脚本复制到Windows目录中的文件夹使用MDT拒绝了对FileSystemObject.CopyFile即使管理员

基本上我想要的是试图将脚本复制到%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 

回答

2

有几件事情:

  1. tempDir变量未设置正确。添加一个尾部斜线,这应该工作。例如:

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

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

    b。您参考的方式HKUregwrite指令中可能会导致错误。如果它确实会导致错误,则仅针对该行,将其更改为HKEY_USERS insdead HKU

    如:

    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 
+0

非常感谢您的帮助。你太棒了!我的东西不过,在测试我的部署后,注意到的是,该行 “objShell.RegWrite “HKEY_USERS \ ZZZ \软件\微软\的Windows \ CurrentVersion \的RunOnce \ DefaultShell”,_ “WScript.exe的” & “” &FULLPATH,“REG_SZ “”在FULLPATH应TEMPDIR中的RegWrite命令 - 否则它看起来在第一日志中部署共享 现在脚本提供了一个错误。“Windows脚本宿主的执行失败(没有足够的 的存储可用于完成这个操作。) “ –

+0

有趣的是,我跑的脚本没有问题。你可以在你的问题中添加一个部分并重新发布修改后的脚本,并显示哪一行产生了该错误? – Damien

+0

让我试着澄清一下。你最初的帮助代码完美无缺。设置为在新用户登录后运行的defaultshell.vbs脚本就是引发该错误的脚本。这个脚本是 Dim objShell:Set objSell = CreateObject(“Wscript.Shell”) objShell.RegWrite“HKEY_CURRENT_USER \ ControlPanel \ Desktop \ Wallpaper”,“”,“REG_SZ” –

相关问题