2013-01-16 65 views
3

此代码:递归设置文件属性

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

这是什么错误意味着什么?

回答

6

这是因为:

Get-ChildItem $targetConfig -Recurse 

同时返回的DirectoryInfo和FileInfo的。 Set-ItemProperty无法为DirectoryInfo设置“ReadOnly”。

为了处理这一点:

Get-ChildItem $targetConfig -Recurse | 
    Where-Object {$_.GetType().ToString() -eq "System.IO.FileInfo"} | 
    Set-ItemProperty -Name IsReadOnly -Value $false 
+6

一个环线条件可以是:'其中{ - 不是$ _ psiscontainer}'。在Powershell V3中只需将'-File'开关添加到'getchilditem'。 –