每當我在下面運行時,它會給出輸出,但如果我沒有輸入進程,它也會給出錯誤。任何想法我怎麼能「隱藏」出現的錯誤信息?計數目錄錯誤消息
檢查目錄計數 讀取主機:發生類型爲「System.Management.Automation.Host.PromptingException」的錯誤。 在C:\用戶\拉拉\應用程序數據\本地的\ Temp \ 5820c1b7-ee6c-47f1-9a6c-06de6dc9e68f.ps1:2字符:20 + $源=讀主機< < < <「請輸入第一個目錄檢查「 + CategoryInfo:ResourceUnavailable:(:) [讀主持人],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語句的基本錯誤!
另外 - 是否有一種方法,而不是顯示=>和< =我顯示「不存在於$源」或「不存在於$目標」 - 我將發送此代碼給另一個團隊和不想混淆它們太多:
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"
您發佈的內容對我來說運行得非常好。那是你遇到問題的實際代碼? – EBGreen
嗨 - 謝謝你的回覆。它運行良好 - 問題是,當它要求一個directoy和一個用戶什麼都不輸入 - 只需按下取消 - 然後炸彈出來,錯誤上面說錯誤。我是否需要一些錯誤檢查來說明何時用戶沒有輸入任何內容,然後向用戶報告提示? – lara400
不,我要說的是,無論我什麼都沒輸入,或者進入了一條道路,我沒有遇到任何錯誤。 – EBGreen