我在powershell脚本中实现了一个out-gridview,它显示了今天创建的所有文件。如果在特定路径中有文件,它可以正常工作,但如果没有,则什么也不会发生。
即使目录中不包含任何文件,网格也会出现,那就太好了。网格可能没有列出任何项目,也可能只是通知没有找到任何文件。
示例:
gci C:\User\Executions\2018-01-25 | Out-GridView一切都比没有好:-)
当然,我可以使用Test-Path在任何地方进行查询和写入(例如Write-Host),但在网格中输出消息更美观。
发布于 2018-01-25 23:50:07
$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-26 00:06:34
看起来有人比我抢先一步,但我也会包括我的例子。然而,我使用的是服务。在我的机器上,如果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
}发布于 2018-01-25 23:46:57
我今天感觉很大方
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")
}https://stackoverflow.com/questions/48446567
复制相似问题