这段代码:
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/