这段代码:
Get-ChildItem $targetConfig -Recurse | Set-ItemProperty -Name IsReadOnly -Value $false
Run Code Online (Sandbox Code Playgroud)
返回几个错误:
Set-ItemProperty:属性System.Boolean IsReadOnly = False不存在.在行: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
Run Code Online (Sandbox Code Playgroud)
返回DirectoryInfo和FileInfo.并且Set-ItemProperty在为DirectoryInfo设置"ReadOnly"时失败.
要处理这个用途:
Get-ChildItem $targetConfig -Recurse |
Where-Object {$_.GetType().ToString() -eq "System.IO.FileInfo"} |
Set-ItemProperty -Name IsReadOnly -Value $false
Run Code Online (Sandbox Code Playgroud)