首页 > 解决方案 > Get-Command 的过滤输出

问题描述

我是一个 powershell 新手,发现自己经常使用Get-Command <cmdlet> -syntax它来了解有关参数的更多信息。我的问题是如何过滤Get-Command Set-ADUser -syntax特定字符串的输出?

我试图用管道Where-Objectfilter,但没有任何运气。

标签: powershell

解决方案


这只是我为参加我的自动化课程/会议/活动的人们提供的“掌握 PowerShell 帮助”文件的一部分。也许它会帮助你。

# Get specifics for a module, cmdlet, or function
(Get-Command -Name Set-ADUser).Parameters
(Get-Command -Name Set-ADUser).Parameters.Keys
Get-help -Name Set-ADUser -Examples
Get-help -Name Set-ADUser -Full
Get-help -Name Set-ADUser -Online

# Find all cmdlets / functions with a target parameter
Get-Command -CommandType Cmdlet |
Where-Object {
    Try {$PSItem.parameters.keys -match 'credential'}
    Catch{} 
}|
Out-GridView -PassThru -Title '
Available cmdlets which have a specific parameter'

Get-Command -CommandType Function |
Where-Object {
    Try {$PSItem.parameters.keys -match 'credential'}
    Catch{} 
}|
Out-GridView -PassThru -Title '
Available functions which have a specific parameter'

# Get property enums/options for a specifc cmdlet/function
(Get-Service | Select-Object -First 1).Status.GetType()
[System.ServiceProcess.ServiceControllerStatus]::
GetNames([System.ServiceProcess.ServiceControllerStatus])

为什么这样做...

Get-Command Set-ADUser -Syntax

...与这个...

(Get-Command -Name Set-ADUser).Parameters.Keys | 
Out-GridView -PassThru -Title 'Select an item to view its properties and methods'| 
Get-Member -Force | 
Out-GridView

(OGV 允许实时过滤。)

..或者只是这样做

Show-Command -Name Set-ADUser

如果您在 ISE 中,只需使用命令选项卡并双击感兴趣的命令,而不是使用原始文本。如果您在 VSCode 中,请选择命令资源管理器并双击感兴趣的命令,然后单击帮助图标,而不是使用原始文本。

但是,如果您只想留在控制台主机中并使用文本,那么这...

<#
List of all parameters that a given cmdlet supports along with a short 
description:
#>
Get-Help Set-ADUser -para '*' | 
Format-Table Name, { $PSItem.Description[0].Text } -wrap 

Get-Help Set-ADUser -para '*' | 
Where-Object -Property Name -Match 'Office' | 
Format-Table Name, { $PSItem.Description[0].Text } -wrap 

使用 OGV 进行实时过滤和选择,但在控制台文本中获得结果。

Get-Help Set-ADUser -para '*' | 
Where-Object -Property Name -Match ((Get-Command -Name Set-ADUser).Parameters.Keys | 
Out-GridView -PassThru -Title 'Select an item to view its syntax details') | 
Format-Table Name, { $PSItem.Description[0].Text } -wrap 

更新

根据你的评论...

如何为参数的变量类型添加另一列?看看办公室的例子,我怎么能添加一个包含

......你可以这样做。

Get-Help Set-ADUser -Parameter '*' | 
Where-Object -Property Name -Match ((Get-Command -Name Set-ADUser).Parameters.Keys | 
Out-GridView -PassThru -Title 'Select an item to view its syntax details') | 
ForEach {
    [PSCustomObject]@{
        Name        = $PSItem.Name
        Type        = $PSItem.Type.Name
        Description = $PSItem.Description[0].Text
    }
} | 
Format-Table Name, Type, Description -wrap

# Results
<#
Name        Type   Description                                                                                                                               
----        ----   -----------                                                                                                                               
Office      String Specifies the location of the user's office or place of business. This parameter sets the Office property of a user object. The LDAP      
                   display name (ldapDisplayName) of this property is "office".                                                                              
                                                                                                                                                         
OfficePhone String Specifies the user's office telephone number. This parameter sets the OfficePhone property of a user object. The LDAP display name        
                   (ldapDisplayName) of this property is "telephoneNumber".   
#>

推荐阅读