我正在尝试在Firefox配置文件中替换prefs.js文件,以更改firefox在大约5000台计算机上的行为方式。
我杀死了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
当我使用自己的(域管理员)帐户手动运行脚本时,我收到了所有ARE N'T不属于的用户配置文件的访问被拒绝错误,但文件已成功复制到我的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吗?
答案 0 :(得分: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}