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

时间:2016-07-05 12:10:04

标签: powershell

我收到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:\Play\BackupCompress.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:\Play\BackupCompress.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:\Play\BackupCompress.ps1:47 char:18
+         $SubFile. <<<< IsReadOnly = $False
    + CategoryInfo          : InvalidOperation: (IsReadOnly:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

-Directory 是条件参数,仅适用于提供程序为“ FileSystem ”的路径。它显示在此页面上:Get-ChildItem for FileSystem,但不在the generic one上。

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

public interface ParameterService { ... }

@Stateless
@Remote(ParameterService .class)
public class ParameterServiceImpl implements ParameterService { }