递归设置文件属性

Vit*_*liy 5 powershell

这段代码:

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

这个错误意味着什么?

Vit*_*liy 8

它发生是因为:

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)

  • schort条件可以是:`where {-not $ _.psiscontainer}`.在Powershell V3中,只需将`-File`开关添加到`getchilditem`即可. (6认同)