计算目录错误消息

时间:2011-08-09 14:03:25

标签: powershell

无论何时我在下面运行它都会给出输出,但如果我没有输入一个指令,它也会出错。知道如何“隐藏”来自的错误信息吗?

检查目录计数 Read-Host:发生了“System.Management.Automation.Host.PromptingException”类型的错误。 在C:\ Users \ lara \ AppData \ Local \ Temp \ 5820c1b7-ee6c-47f1-9a6c-06de6dc9e68f.ps1:2 char:20 + $ Source = Read-Host<<<< “请输入第一个目录进行检查”     + CategoryInfo:ResourceUnavailable:(:) [Read-Host],PromptingException     + FullyQualifiedErrorId:System.Management.Automation.Host.PromptingException,Microsoft.PowerShell.Commands.ReadHostCommand

Write-Host "Checking Directory Count" -ErrorAction SilentlyContinue
$Source = Read-Host "Please enter first directory to check" 

If($Source)
{
    Write-host "There are " (Get-ChildItem $Source).Count "items in the ""$Source"" directory"  
}
Else
{
    Write-Host "Please enter a directory"
}

更新

感谢所有帮助。我基本上想将下面的内容合并到另一个进行文件夹比较的脚本中。我需要知道我要检查的目录中有多少文件以及不存在的文件。我有以下代码 - 我知道它写的很糟糕!我只是不太确定如何将$ source和$ target放到相同的IF语句中,所以最终犯了两个IF语句的基本错误!

另外 - 是否有一种方法而不是显示=>和< =我显示“$ source中不存在”或“$ target中不存在” - 我将把这段代码发送给另一个团队,并且不想太混淆他们:

Write-Host "Checking Directory Count and Folder comparision" -ErrorAction SilentlyContinue

$Source = Read-Host "Please enter Source directory to check" 
$Target = Read-Host "Please enter Target directory to check"

$child1 = Get-ChildItem -Path $Source
$child2 = Get-ChildItem -Path $Target

Compare-Object $child1 -DifferenceObject $child2 

Write-Host ""
If($source -and (Test-Path -Path $source -PathType Container))
{
    "There are $(@(Get-ChildItem $Source).Count) items in the '$Source' directory"  
}
Else
{
    Write-Host "Please enter a directory"
}
If($source -and (Test-Path -Path $Target -PathType Container))
{
    "There are $(@(Get-ChildItem $Target).Count) items in the '$Target' directory"  
}
Else
{
    Write-Host "Please enter a directory"
}


Write-Host ""
Write-Host "Any symbols with '<=' mean that the file Does NOT exist in TARGET"
Write-Host "Any symbols with '=>' mean that the file Does NOT exist in SOURCE"

1 个答案:

答案 0 :(得分:1)

我建议您首先测试输入的路径是否存在,然后再尝试列出其内容。我也会将结果转换为数组,如果使用Get-ChildItem的结果是标量(仅一个对象),它将没有count属性:

$Source = Read-Host "Please enter first directory to check" 

If($source -and (Test-Path -Path $source -PathType Container))
{
    "There are $(@(Get-ChildItem $Source).Count) items in the '$Source' directory"  
}
Else
{
    Write-Host "Please enter a directory"
}

UPADTE:

我没有测试代码,但您需要重新排序命令。另外,在Name属性上进行比较:

$Source = Read-Host "Please enter Source directory to check" 
$Target = Read-Host "Please enter Target directory to check"

If($source -and (Test-Path -Path $source -PathType Container))
{
    "There are $(@(Get-ChildItem $Source).Count) items in the '$Source' directory"  
}
Else
{
    Write-Host "Please enter a source directory"
    return
}

If($source -and (Test-Path -Path $Target -PathType Container))
{
    "There are $(@(Get-ChildItem $Target).Count) items in the '$Target' directory"  
}
Else
{
    Write-Host "Please enter a Target directory"
    return
}


$child1 = Get-ChildItem -Path $Source
$child2 = Get-ChildItem -Path $Target

Compare-Object $child1 -DifferenceObject $child2 -Property Name

Write-Host ""
Write-Host "Any symbols with '<=' mean that the file Does NOT exist in TARGET"
Write-Host "Any symbols with '=>' mean that the file Does NOT exist in SOURCE"