【发布时间】: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