【问题标题】:Copying file into folder via system profile通过系统配置文件将文件复制到文件夹中
【发布时间】:2018-12-04 09:52:18
【问题描述】:

我正在尝试替换 Firefox 配置文件中的 prefs.js 文件,以更改 Firefox 在大约 5k 台机器上的行为方式。

我杀了firefox,用脚本删除文件

Remove-Item -Path "C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\prefs.js" -Force -Recurse

没有问题。然后我使用 copy-item 将 prefs.js 的所需副本拉入机器上的所有 firefox 配置文件。这是我要提问的脚本。

$Target = Get-ChildItem -path "C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*"
Copy-Item "C:\DTS\BMC\ReplacePrefsjs\prefs.js" -Destination $Target -Force -Recurse

当我在我自己的(域管理员)帐户上手动运行脚本时,我收到所有不是我的用户配置文件的访问被拒绝错误,但文件已成功复制到我的 firefox 配置文件。这似乎很正常,因为它仅以正常权限运行。以下是该执行的日志摘录:

Get-ChildItem : Access is denied
At C:\DTS\BMC\ReplacePrefsjs\PrefsJSReplace.ps1:2 char:11
+ $Target = Get-ChildItem -path "C:\Users\*\AppData\Roaming\Mozilla\Fir ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : PermissionDenied: (C:\Users\<removed>\AppData:String) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
Get-ChildItem : Access is denied
At C:\DTS\BMC\ReplacePrefsjs\PrefsJSReplace.ps1:2 char:11
+ $Target = Get-ChildItem -path "C:\Users\*\AppData\Roaming\Mozilla\Fir ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : PermissionDenied: (C:\Users\<removed>\AppData:String) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

但是,当我使用我们的部署解决方案(如 SCCM 等)发送脚本时,我收到以下错误:

PS>TerminatingError(Copy-Item): "Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Destination'. Specified method is not supported."
Copy-Item : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Destination'. Specified 
method is not supported.
At C:\DTS\BMC\ReplacePrefsjs\PrefsJSReplace.ps1:3 char:61
+ ... tem "C:\DTS\BMC\ReplacePrefsjs\prefs.js" -Destination $Target -Force  ...
+                                                           ~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.CopyItemCommand
 Copy-Item : Cannot convert 'System.Object[]' to the type 'System.String'
required by parameter 'Destination'. Specified method is not supported.
At C:\DTS\BMC\ReplacePrefsjs\PrefsJSReplace.ps1:3 char:61
+ ... tem "C:\DTS\BMC\ReplacePrefsjs\prefs.js" -Destination $Target -Force  ...
+                                                           ~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.CopyItemCommand

当我第一次注意到这一点时,我认为脚本需要类似“foreach”的东西来处理 $Target 将提取的每个条目(为所有 firefox 配置文件运行脚本),而不是 -recurse。

如果是这种情况,那么当我手动运行它时它也不应该工作,但是当我这样做时,它会遍历每个配置文件,让我拒绝访问我无法在没有提升权限的情况下编辑的位置,适用于我的个人资料,并且在没有转换错误的情况下完成。

谁能帮我理解为什么我的 Copy-Item 在系统配置文件运行时无法解析目标 $Target?

【问题讨论】:

  • $Target 是目录和文件对象的集合copy-item 想要一个特定的目的地。你需要一个循环。使用适当的帐户运行提升应该解决另一个问题。 -Recurse 有源上下文...不是目标
  • 好吧,“另一个问题”是它在本地运行时不会遇到该问题。我可以在日志中发布,但它只是运行机器上的每个用户目录而没有问题。
  • 本地运行时是否找到多个$target
  • 确实如此。这就是奇怪的事情。我在原始帖子中添加了一些日志,这是那里的第三个代码块。我没有发布整个内容,但它只是针对机器上存在的每个配置文件运行相同的错误。我已经用下面答案中的脚本替换了我的脚本,这似乎是有效的。仍然很好奇为什么当我在本地运行它时它会起作用。
  • 想通了。 $Target 正在查找多个位置,但由于它无法访问除我之外的任何位置,$Target 实际上等于

标签: powershell firefox windows-7 copy-item


【解决方案1】:

您正在使用 $Targets 创建一个对象数组

$Target = Get-ChildItem -path "C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*"

然后,您不会通过管道输出输出,而是将数组放入新命令中。 试试这个

Get-ChildItem "C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*" -Directory | foreach-object{Copy-Item "C:\DTS\BMC\ReplacePrefsjs\prefs.js" -Destination $_ -Force -Recurse}

【讨论】:

  • 这在我的测试盒上工作。仍然不确定为什么我的错误脚本在本地运行时会遍历每个配置文件,但是当公司没有互联网中断时,我可以弄清楚这一点。谢谢。
  • 可能是因为在您的计算机上您只得到 1 个对象,这不会使其成为对象数组,而只是一个单一的对象。
  • 我可以看到,但是当我在本地运行时,每个用户配置文件都有错误,说我无权将文件放入其中。因此,在这种情况下,它似乎在每个配置文件上查看并运行脚本。我已经在我的原始帖子中添加了一些日志,这是那里的第三个代码块。这不是全部,但它只是针对机器上存在的每个配置文件运行相同的错误。
  • 好吧,你是对的。刚刚又看了一眼。由于“Get-ChildItem”是我得到错误的地方,它看起来像是在尝试制作数组,但只有查看我的个人资料的权限,所以这就是它所获得的全部。然后,将 1 配置文件通过管道传输到 Copy-Item 命令没有问题。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-11
  • 1970-01-01
  • 2015-03-28
  • 2014-10-26
  • 1970-01-01
  • 2018-01-27
  • 1970-01-01
相关资源
最近更新 更多