使用PowerShell Out-GridView作为GUI替代

Out-GridView是内置的powershell cmdlet,可将给定命令的输出发送到交互式窗口。这篇文章将讨论几种不同的用例,以及如何将它们合并到代码中。

为什么要使用Out-GridView?

  • 简单
  • 内置于PowerShell
  • 可订制
  • 强大

参量

首先,让我们开始讨论本文中将介绍的参数和可以使用的值。

  • 输出方式
    • 单一:这会将您的选择限制为一个对象;如果下一条命令只能接受一个值作为输入,请使用此选项。
    • 多个:这允许您一次选择多个对象。如果下一条命令可以处理多个值作为输入,请使用此选项。
    • 无:将无法选择任何内容,如果只需要报告,请使用此选项。这是默认值。
  • 标题
    • 这是您在Out-GridView窗口中放置描述性名称的地方。任何其他说明也应放在此处。

程式码范例

的GitHub

有几种将数据导入Out-GridView的方法,以及多种从Out-GridView输出数据的方法。我们将介绍我最常使用的方法。

方法1:直接从命令输入,仅显示结果

Out-GridView的最基本功能是仅向用户显示数据。在下面的示例中,我们执行Get-Service命令以获取给定系统上的所有服务,然后将这些结果通过管道传输到Out-GridView进行查看。我们已经为Out-GridView窗口设置了一个标题,并将outputmode设置为None,这意味着没有“确定”或“取消”按钮,并且没有数据将传递给下一个命令。

Get-Service | Out-GridView -Title "List of all the services on this system" -OutputMode None
 

方法2:来自变量的输入,单个输出

稍微高级一点的选项将使用一个变量来保存Get-Service命令的所有输出,然后我们将该变量通过管道传递给Out-GridView。这为我们提供了更大的灵活性,并在需要时可以在脚本的其他部分重用该变量。我们再次为Out-GridView窗口设置标题,但是这次我们将outputmode设置为Single,这意味着将有一个“确定”和“取消”按钮,而只允许用户选择一个值。所选的那个值将存储在指定的变量($ single_output)中。在此示例中,我们使用Stop-Service命令停止选定的服务。

 
$services = Get-Service ### Getting all services on the system and sending them to the services variable
$single_output = $services | Out-GridView -Title "Select the service that you want to stop" -OutputMode Single ### Sending the services variable to Out-GridView, the single service selected will be sent to single_output variable
 
Write-Host "Stopping service" $single_output.Name -BackgroundColor Cyan -ForegroundColor Black
Stop-Service $single_output.Name ### Finally the service selected will be stopped

选择单个服务,然后单击“确定”按钮将导致所选服务被停止。

方法3:来自自定义数组的输入,多个输出

稍微高级一点的选项将使用自定义数组来保存来自services变量的所有数据。服务变量将从Get-Service命令填充,然后通过管道传递到Where-Object,该对象仅包含状态为“正在运行”的服务。该阵列将使用Foreach循环中的所有Running服务填充。然后将自定义数组通过管道传递到Out-GridView。对于停止服务的这个简单示例,自定义数组并不是完全必要的,但是对于更复杂的脚本(您需要将多个来源的数据组合在一起),它可能很有用。我们再次为Out-GridView窗口设置标题,但是这次我们将outputmode设置为Multiple这意味着在允许用户选择多个值的同时会有一个“确定”和“取消”按钮。单击所需的值时,按住CTRL可以选择多个值。通过按CTRL + A可以选择所有值。所选的值将存储在指定的变量($ multiple_output)中。在此示例中,我们在Foreach循环中利用Stop-Service命令来停止多个选定的服务。

$custom_array = @() ### Creating an empty array to populate data in
$services = Get-Service | Select Name, Status, DisplayName, StartType | Where-Object {$_.Status -eq "Running"} ### Getting Running services on the system and sending them to the services variable
 
### Looping through all of the running services and adding them to the custom array
Foreach ($service in $services){ 
 
    ### Setting up custom array utilizing a PSObject
    $custom_array += New-Object PSObject -Property @{
    Service_Name = $service.Name
    Service_Status = $service.Status
    Service_DisplayName = $service.DisplayName
    Service_StartType = $service.StartType
    }
}
 
$multiple_output = $custom_array | Out-GridView -Title "This is using a custom array with multiple output values" -OutputMode Multiple
 
### Looping through all of the selected services and stopping the service
Foreach ($output in $multiple_output){
 
    Write-Host "Stopping service" $output.Service_Name -BackgroundColor Cyan -ForegroundColor Black
    Stop-Service $output.Service_Name ### Finally the service selected will be stopped
 
}

选择多个服务(单击所需服务时按住CTRL),然后单击“确定”按钮将导致所选服务停止。(CTRL + A全选)

就是这样,感谢您的阅读!

猜你喜欢

转载自blog.csdn.net/allway2/article/details/109556096
今日推荐