简体   繁体   English

网格外视图,即使找不到数据

[英]Out-Gridview even if no data found

I have an out-gridview implemented in my powershell script which shows me all files today created. 我在Powershell脚本中实现了外部网格视图,该视图向我显示了今天创建的所有文件。 If there are files in the specific path it works fine but if not nothing happens. 如果特定路径中有文件,则可以正常工作,但如果没有,则不会发生任何事情。

It would be great that the grid appears even if the directory doesn't contain any files. 即使目录中不包含任何文件,网格也会很好地显示。 Either the grid lists no items or just maybe a notification that no files have been found. 网格不列出任何项目,或者仅列出未找到文件的通知。

example: 例:

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

Everything would be nicer than nothing :-) 一切都会总比没有好:-)

Sure, I could use Test-Path to query and write anywhere (eg Write-Host) but it is more aesthetic to output the message in a grid. 当然,我可以使用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 thank you! @ TheIncorrigible1谢谢!

Looks like someone beat me to it, but I'll include my example as well. 看起来有人在击败我,但我也会列举我的榜样。 Mine uses services, however. 我的使用服务。 On my machine, if the Get-Service command uses -Name s*, it'll open Out-GridView with the services that begin with S. If the Get-Service command uses -Name x*, it'll run the Else portion and open Out-GridView with the PSCustomObject. 在我的机器上,如果Get-Service命令使用-Name s *,它将使用以S开头的服务打开Out-GridView。如果Get-Service命令使用-Name x *,它将运行Else部分并使用PSCustomObject打开Out-GridView。 What this version does give you is the ability to label the column. 此版本为您提供标记列的功能。 In Danijel de Vasco's example it uses a default of "string" as the column header vs. mine, which uses "Message." 在Danijel de Vasco的示例中,它使用默认值“ string”作为列标题,而我的则使用“ Message”。 More or less the same thing, however, with a minor customization. 但是,差不多是同一件事,只是进行了少量的自定义。

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

I am feeling generous today 我今天很慷慨

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