【问题标题】:Filtering output of Get-CommandGet-Command 的过滤输出
【发布时间】:2020-11-17 15:14:00
【问题描述】:

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

我尝试通过管道传输 Where-Objectfilter,但运气不佳。

【问题讨论】:

  • 你将把所有的东西都作为一个大的 honkin' 字符串取回,所以你需要做的就是进一步处理拆分它,例如(Get-Command Get-Command -Syntax).Split("rn") | Select-String verb.
  • 您将所有内容都作为一个字符串返回是对的。我没想到。谢谢您的帮助。运行Get-Command Set-ADUser -Syntax.Split("r n") | Select-String Office 返回匹配!请问"r n"这一行在split中有什么意义?
  • 哎呀,忘记了SO的转义规则。当然我的意思是.Split("`r`n"),使用反引号来转义PowerShell中的特殊字符。目的是在换行符上拆分。 (再想一想,也许使用 [Environment]::NewLine 会更容易,而且可能更便携,因为 Linux 上的 PowerShell 已经成为一种东西。
  • 再次感谢。环境更容易使用。我非常感谢您的解释和帮助。
  • 至于 [现在可能更便携,因为 Linux 上的 PowerShell 是一件事。],这... ["rn"] 只是正则表达式。因此,无论操作系统如何,都不仅仅是 PowerShell。

标签: powershell


【解决方案1】:

这只是我为参加我的自动化课程/会议/活动的人们提供的“掌握 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".   
#>

【讨论】:

  • 一切看起来都很棒。我从未考虑过使用 OGV 进行过滤。在您的 OGV 过滤并在控制台中显示时,我想您忘记了 | 附近的详细信息。
  • 是的,粘贴时忘记了那个管道(已修复),但我老了,它发生了。 ;-} ,你知道 OPP(老年人问题)。
  • 我喜欢网格视图,但是如何为参数的变量类型添加另一列?查看办公室的示例,我如何添加包含&lt;string&gt; 的列?
  • 在 Select 过程中添加对其他感兴趣的项目的调用,类似于我目前显示的计算属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 1970-01-01
  • 2014-06-15
  • 2021-07-02
  • 1970-01-01
  • 2012-12-15
相关资源
最近更新 更多