【问题标题】:Copy file from script directory to All Users' desktop将文件从脚本目录复制到所有用户的桌面
【发布时间】:2015-12-18 06:02:37
【问题描述】:

我已经编写了下面的脚本,但是当我运行它(使用 PrimalScript 进行故障排除)时,我收到错误“权限被拒绝”。 我是这台设备的管理员,运行提升的脚本时遇到同样的错误。

这是脚本:

Dim WshShell, strCurDir, File, strDesktop

Set WshShell = CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("AllUsersDesktop")
Set ofso = CreateObject("Scripting.FileSystemObject")
strPath = ofso.GetParentFolderName(WScript.ScriptFullName)
File = "pwsafe.psafe3"
strCurDir = ofso.BuildPath(strPath, File)

ofso.CopyFile strCurDir , strDesktop , OverwriteExisting

我做错了什么?

【问题讨论】:

    标签: vbscript allusersprofile


    【解决方案1】:

    你必须设置一个进程:

    Dim ofso, oshell, oproc, strDesktop, overwrite
    Set ofso = CreateObject("Scripting.FileSystemObject")
    Set oshell = CreateObject("WScript.Shell")
    Set oproc  = oshell.Environment("Process")
    strDesktop = oproc.Item("UserProfile") & "\Desktop\"
    overwrite = True
    ofso.CopyFile "pwsafe.psafe3" , strDesktop , overwrite
    

    【讨论】:

    • 如您所见,我只是一个脚本小子,但没有太多时间学习。但是,您的解决方案中的 Dim 缺少逗号。一旦我把它安装到位,它就像一个魅力。谢谢。
    • 这是所有用户配置文件吗?不应该是strDesktop = oproc.Item("ALLUSERSPROFILE") & "\Desktop\"吗?
    • 你不需要对 OPs 代码做太多改动,他们只需要Set oproc = oshell.Environment("Process") 他们就可以使用strDesktop = oproc.Item("AllUsersDesktop") 没问题。
    • @totalyscrewedup 您确实意识到这不是您所要求的所有用户桌面?
    • 虽然 Sergio 的脚本在本地运行良好,但在通过 primalScript 进行测试期间,它在通过平台运行时因权限问题而失败。由于平台部署通常通过 vbs 创建快捷方式就好了,我很困惑为什么我会收到拒绝访问错误。因此,它不会写入“allusersdesktop”的声明是正确的。我尝试了 Lankymart 的建议,并利用建议对我的原始脚本进行更改,使其看起来像这样,但它因“无效的过程调用或参数”而失败:
    【解决方案2】:

    虽然我上一条评论显示的脚本不起作用,但下面的脚本是 Sergio 和 Lankymart 工作的组合。想提他们两个,因为他们值得称赞。这是工作脚本,我希望其他人可以使用它。

    Dim ofso, oshell, oproc, strDesktop, overwrite
    Set ofso = CreateObject("Scripting.FileSystemObject")
    Set oshell = CreateObject("WScript.Shell")
    Set oproc  = oshell.Environment("Process")
    strDesktop = oproc.Item("ALLUSERSPROFILE") & "\Desktop\"
    overwrite = True
    ofso.CopyFile "pwsafe.psafe3" , strDesktop , overwrite
    

    谢谢你们。

    【讨论】:

    • 不要将解决方案重新发布为您自己的答案,您应该accept 为您提供解决方案的答案。
    • 由于两者都有贡献,我无法选择其中一个。如果像我这样的另一个菜鸟通过它,他们需要知道正确的代码,而不必猜测什么代码在工作。
    猜你喜欢
    • 2021-01-02
    • 1970-01-01
    • 2013-12-02
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    相关资源
    最近更新 更多