当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 Out-GridView 在 PowerShell 中查看和选择表数据

作者:精品下载站 日期:2024-12-14 23:11:29 浏览:13 分类:玩电脑

使用 Out-GridView 在 PowerShell 中查看和选择表数据


Out-GridView cmdlet 允许将数据显示为交互式图形表格,可以根据不同的条件对数据进行过滤或排序。您可以在脚本中使用 Out-Gridview cmdlet,为用户提供最简单的 GUI 来选择对象。

事实上,Out-GridView 是运行 .NET DataGridView 的包装器,它是来自 Windows 窗体控件的标准图形表单。

使用 Out-GridView 表

让我们看一下使用 Out-GridView cmdlet 显示 Windows 服务列表及其一些属性的最简单示例:

Get-Service | Select DisplayName,Status,ServiceName,Can* | Out-GridView

[玩转系统] 使用 Out-GridView 在 PowerShell 中查看和选择表数据

正如您所看到的,出现了一个包含 Windows 服务属性列表的图形表格。该 cmdlet 根据对象属性或数据类型自动设置列名称,并在无法定义数据格式时扩展 PSObject 属性。

您可以使用“过滤器”框搜索表单。

[玩转系统] 使用 Out-GridView 在 PowerShell 中查看和选择表数据

您可以直接从 PowerShell 访问 Excel 表中的数据。

您还可以使用添加条件按钮来搜索表格。在下面的屏幕截图中,我创建了最简单的过滤器,其中包含名称中包含 VMW 的正在运行的服务列表。过滤器是直接根据对象属性的值创建的。

[玩转系统] 使用 Out-GridView 在 PowerShell 中查看和选择表数据

或者让我们显示 CPU 利用率最高的前 10 个进程的列表(我已使用 -Title 选项更改了 Out-GridView 窗口的名称):

Get-Process | Sort-Object CPU -Descending | Select -First 10 | Out-GridView -Title "Top 10 CPU processes"

您可以通过单击列标题快速按升序/降序对表格内容进行排序。

[玩转系统] 使用 Out-GridView 在 PowerShell 中查看和选择表数据

带有 PassThru 开关的 Out-GridView cmdlet

然而,最强大的 Out-Gridview 功能是 -PassThru 选项,它为 PowerShell 脚本提供了新级别的用户友好 GUI。

该选项在 PowerShell 3.0 或更高版本中可用,允许用户选择表中的一个或多个对象,并使用标准管道将它们传递到 PowerShell 脚本中的下一个 cmdlet。

例如,以下 PowerShell 脚本显示正在运行的 Windows 服务的列表。用户在列表中选择一项服务并单击“确定”。

Get-Service | Where-Object {$_.status -eq 'running'}| Out-GridView -Title "Select service to restart" -PassThru -OutputMode Multiple | Restart-service -verbose

该脚本将仅重新启动用户选择的服务。

[玩转系统] 使用 Out-GridView 在 PowerShell 中查看和选择表数据

您可以将用户选择的对象保存到变量中:

$Svcs = Get-Service | Where-Object {$_.status -eq 'running'}| Out-GridView -Title "Select services" -PassThru

或者您可以仅保存属性的值。为此,请将以下管道添加到上一个命令中:

| Select -ExpandProperty Name

[玩转系统] 使用 Out-GridView 在 PowerShell 中查看和选择表数据

您可以使用以下选项允许用户仅选择表中的一项或多项:

-OutputMode Single and -OutputMode Multiple

按住Ctrl可选择表中的多行。

如何在 PowerShell 脚本中使用 Out-Gridview 作为 GUI?

以下是一些使用 Out-GridView 的更有趣的示例。

要显示 PowerShell 历史记录中先前命令的列表并再次运行所选命令:

Get-History | Out-GridView -PassThru | Invoke-Expression

要显示其他 Windows 组件的列表并安装选定的组件(例如,RSAT Active Directory 管理工具和 SSH 客户端):

Get-WindowsCapability -Online | Where-Object {$_.State -eq “NotPresent”}| Out-GridView -PassThru |Add-WindowsCapability -Online

[玩转系统] 使用 Out-GridView 在 PowerShell 中查看和选择表数据

要从 RDS 场的连接代理获取 RDP 会话列表并使用 RDP 影子连接连接到用户选择的桌面:

import-module remotedesktop
$cbserver = "munrdsbroker1.a-d.site"
$id = get-rdusersession -ConnectionBroker $cbserver | Out-GridView -title "RD Connection" -PassThru | select hostserver, unifiedsessionid
$id2 = $id | select -ExpandProperty unifiedsessionid
$srv = $id | select -ExpandProperty hostserver
mstsc /v:"$srv" /shadow:"$id2" /control /noconsentprompt

您可以使用 AD PowerShell 模块中的 Get-ADUser cmdlet 显示特定 OU 中已启用用户的列表并重置用户的域密码:

Import-Module ActiveDirectory
$NewPasswd=Read-Host "Enter a new user password" -AsSecureString
Get-ADUser -filter {Enabled -eq "true"} -properties Name, displayname,EmailAddress,pwdLastSet -SearchBase ‘OU=Berlin,OU=DE,DC=a-d,DC=com’| Out-GridView -PassThru -title “Select a user to reset a password”| Set-ADAccountPassword -NewPassword $NewPasswd -Reset

[玩转系统] 使用 Out-GridView 在 PowerShell 中查看和选择表数据

使用 Invoke-Command,您可以从远程计算机获取数据并将其显示在表格中:

Invoke-Command -ComputerName be-dc01, mun-dc01, mun-dc02 -ScriptBlock {Get-Culture} | Select-Object PSComputerName,DisplayName| Out-GridView

遗憾的是,Out-GridView cmdlet 无法在 Windows Server Core 中使用。如果运行它,会出现以下错误:

out-gridview : To use the Out-GridView, install Windows PowerShell ISE by using Server Manager, and then restart this application. (Could not load file or assembly 'Microsoft.PowerShell.GraphicalHost, Version=3.0.0.0, Culture=neutral, PublicKeyToken=xxxxx' or one of its dependencies. The system cannot find the file specified.)

但是,您可以使用许多 cmdlet 必须访问 Server Core 的 -ComputerName 选项。例如:

Get-Service -ComputerName lon-dc02 | Where-Object {$_.status -eq 'running'}| Out-GridView -Title "Select service to restart" -OutputMode Single|Restart-Service -Verbose

由于某种原因,Microsoft 从 PowerShell Core 6.x 中删除了 Out-GridView cmdlet,但在版本 7.0 中又将其返回。如果您使用的是 PowerShell 6.x,请使用以下命令将其更新到最新版本:

iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"

正如您所看到的,Out-GridView 允许向您的 PowerShell 脚本添加漂亮的图形界面。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯