【问题标题】:Recursively set file attributes递归设置文件属性
【发布时间】:2012-12-31 08:05:52
【问题描述】:

这段代码:

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

返回几个错误:

Set-ItemProperty : 属性 System.Boolean IsReadOnly=False 不 存在。在行:1 字符:56 + 获取子项 $targetConfig -Recurse | Set-ItemProperty

这个错误意味着什么?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    这是因为:

    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
    

    【讨论】:

    • 一个 schort 条件可以是:where { -not $_.psiscontainer }。在 Powershell V3 中,只需将 -File 开关添加到 getchilditem
    猜你喜欢
    • 2013-06-20
    • 1970-01-01
    • 2021-05-01
    • 2015-09-30
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多