Get-ChildItem :找不到与参数名称匹配的参数'Directory'



我正在获得Get-ChildItem : *A parameter cannot be found that matches parameter name 'Directory'*错误消息,下面是使用的脚本,请帮助我找到问题?

脚本:

Function Clear-ReadOnlyFiles([string]$path)
{
  "Clearing readonly attribute from files in $path"
 New-Variable -Name read_only -Value 1 -Option readonly
 Get-ChildItem -Path $path |
 Where-Object { $_.attributes -match 'readonly' } |
 ForEach-Object {
   $_.attributes = $_.attributes -Bxor $read_only }
}#end Clear-ReadonlyFiles

$ZipCmd     = "{0}7za.exe" -f $BackupDir
$ZipCmdArgs     = "-sdel"
# Grab Directories in Location
$Directories = Get-ChildItem -Path $BackupDir -Directory
# Cycle Through and Launch 7za to Compress
# Check if Archive Exists - If yes, delete source folder
ForEach ($Directory in $Directories)
{
    $Source  = "{0}{1}" -f $BackupDir,$Directory.Name
    $Archive = "{0}.7z" -f $Source
    # Clear Read-Only Attribute on Folder
    $SubFiles = Get-ChildItem -Path $Directory -Recurse -ReadOnly
    ForEach ($SubFile in $SubFiles) {
        $SubFile.IsReadOnly = $False
    }
    #If ($Directory.IsReadOnly -eq $True) { $Directory.set_IsReadOnly($False) }
    $AllArgs = @('a', $ZipCmdArgs, $Archive, $Source);
    & $ZipCmd $AllArgs
}

错误信息:

Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.
At C:PlayBackupCompress.ps1:35 char:57
+ $Directories = Get-ChildItem -Path $BackupDir -Directory <<<< 
    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Get-ChildItem : A parameter cannot be found that matches parameter name 'ReadOnly'.
At C:PlayBackupCompress.ps1:45 char:66
+     $SubFiles = Get-ChildItem -Path $Directory -Recurse -ReadOnly <<<< 
    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Property 'IsReadOnly' cannot be found on this object; make sure it exists and is settable.
At C:PlayBackupCompress.ps1:47 char:18
+         $SubFile. <<<< IsReadOnly = $False
    + CategoryInfo          : InvalidOperation: (IsReadOnly:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

我相信目录开关是在PowerShell 3.0中添加的。在旧版本中,你需要检查每个子项的PSIsContainer属性,如果子项是一个目录,该属性将为true:

$Directories = Get-ChildItem -Path $BackupDir | Where-Object { $_.PSIsContainer }

-Directory是一个条件参数,只适用于提供程序为"FileSystem"的路径。它出现在这一页:Get-ChildItem for FileSystem,但不在通用的那一页。

请确保在键入以下内容时显示正确的提供程序:

Get-Item $BackupDir | Select-Object -Property PSProvider

相关内容

最新更新