2

我在我的 powershell 脚本中实现了一个 out-gridview,它显示了今天创建的所有文件。如果特定路径中有文件,它可以正常工作,但如果没有任何反应。

即使目录不包含任何文件,网格也会出现。网格要么没有列出任何项目,要么只是一个没有找到文件的通知。

例子:

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

一切都比没有好:-)

当然,我可以使用 Test-Path 在任何地方查询和写入(例如 Write-Host),但在网格中输出消息更美观。

4

3 回答 3

1
$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 谢谢!

于 2018-01-25T15:50:07.450 回答
0

看起来有人打败了我,但我也会包括我的例子。但是,我的使用服务。在我的机器上,如果 Get-Service 命令使用 -Name s*,它将使用以 S 开头的服务打开 Out-GridView。如果 Get-Service 命令使用 -Name x*,它将运行 Else 部分并使用 PSCustomObject 打开 Out-GridView。这个版本给你的是标记列的能力。在 Danijel de Vasco 的示例中,它使用默认的“字符串”作为列标题,而我的使用“消息”。但是,或多或少是相同的,只是进行了少量定制。

If (Get-Service -Name s* -OutVariable Services) {
    $Services | Out-GridView
} Else {
    $Message = [PSCustomObject]@{
        Message = 'No Files Found'
    }
    $Message | Out-GridView
}
于 2018-01-25T16:06:33.850 回答
-1

我今天感觉很慷慨

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")
}
于 2018-01-25T15:46:57.000 回答