powershell - 递归设置文件属性

标签 powershell

这段代码:

Get-ChildItem $targetConfig -Recurse | Set-ItemProperty -Name IsReadOnly -Value $false

返回几个错误:

Set-ItemProperty : Property System.Boolean IsReadOnly=False does not exist. At line:1 char:56 + Get-ChildItem $targetConfig -Recurse | Set-ItemProperty <<<< -Name IsReadOnly -Value $false + CategoryInfo : ReadError: (System.Boolean IsReadOnly=False:PSNoteProperty) [Set-ItemProperty], IOException + FullyQualifiedErrorId : SetPropertyError,Microsoft.PowerShell.Commands.SetItemPropertyCommand



这个错误是什么意思?

最佳答案

它的发生是因为:

Get-ChildItem $targetConfig -Recurse

返回 DirectoryInfo 和 FileInfo。并且 Set-ItemProperty 无法为 DirectoryInfo 设置“只读”。

要处理此用途:
Get-ChildItem $targetConfig -Recurse |
    Where-Object {$_.GetType().ToString() -eq "System.IO.FileInfo"} |
    Set-ItemProperty -Name IsReadOnly -Value $false

关于powershell - 递归设置文件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14367989/

相关文章:

powershell - 在 PowerShell 中获取当前的 Windows session ID

PowerShell 6.2.4 : Get-Clipboard' is not recognized as the name of a cmdlet

powershell - 如何从字符串中删除结尾的逗号

powershell - 将 ConvertFrom-String 与所有数据集中不存在的值一起使用

excel - 循环遍历 Excel 中的每个工作表以在 PowerShell 中执行任务

shell - 在 Windows Powershell 和命令提示符中推送

windows - 如何使用 cacert 文件在 Powershell (Invoke-WebRequest) 中执行等效的 cUrl?

amazon-web-services - 使用 Elastic Beanstalk 在实例启动时运行 Windows Powershell 脚本

powershell - 使用Powershell脚本将新文件夹文件重命名为下一个增量编号

powershell - 如何在 Powershell 中提取 Command 中的值?