簡體   English   中英

網格外視圖,即使找不到數據

[英]Out-Gridview even if no data found

我在Powershell腳本中實現了外部網格視圖,該視圖向我顯示了今天創建的所有文件。 如果特定路徑中有文件,則可以正常工作,但如果沒有,則不會發生任何事情。

即使目錄中不包含任何文件,網格也會很好地顯示。 網格不列出任何項目,或者僅列出未找到文件的通知。

例:

gci C:\User\Executions\2018-01-25 | Out-GridView

一切都會總比沒有好:-)

當然,我可以使用Test-Path在任何地方查詢和編寫消息(例如Write-Host),但是在網格中輸出消息更為美觀。

$list = Get-ChildItem "C:\User\Executions\2018-01-25"
if(($list).count -gt 0){
  Get-ChildItem $list | Out-GridView
}else{
  'No Data found' | Out-GridView
}

@ TheIncorrigible1謝謝!

看起來有人在擊敗我,但我也會列舉我的榜樣。 我的使用服務。 在我的機器上,如果Get-Service命令使用-Name s *,它將使用以S開頭的服務打開Out-GridView。如果Get-Service命令使用-Name x *,它將運行Else部分並使用PSCustomObject打開Out-GridView。 此版本為您提供標記列的功能。 在Danijel de Vasco的示例中,它使用默認值“ string”作為列標題,而我的則使用“ Message”。 但是,差不多是同一件事,只是進行了少量的自定義。

If (Get-Service -Name s* -OutVariable Services) {
    $Services | Out-GridView
} Else {
    $Message = [PSCustomObject]@{
        Message = 'No Files Found'
    }
    $Message | Out-GridView
}

我今天很慷慨

try
{
    $out = gci C:\User\Executions\2018-01-25
    if ($out)
    {
        $out | Out-GridView
    }
    else
    {
        $null = [System.Windows.Forms.MessageBox]::Show("Directory is empty", "Notification", "OK", "Information")
    }
}
catch
{
    $ErrMsg = $_.Exception.Message
    $null = [System.Windows.Forms.MessageBox]::Show("Error Occurred: $ErrMsg", "Error", "OK", "Error")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM