PowerShell - 作为参数系列

发布于 2024-09-10 10:30:44 字数 123 浏览 3 评论 0原文

如何查找或列出 PowerShell 的 -as 系列参数?

到目前为止我发现 -as Type 例如 -as[Int] 我还发现 -asHashTable

我的问题是还有哪些其他 -as 参数可用?

How can I find or list PowerShell's -as family of parameters?

So far I found -as Type e.g. -as[Int]
Also I have found -asHashTable

My questions is what other -as parameters are available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凤舞天涯 2024-09-17 10:30:44

如果有人付出一些努力,这可能会有一个更优雅的解决方案:

PS Home:\> gcm | where { $_.CommandType -eq 'Cmdlet' } | foreach { $_ | Add-Member -PassThru NoteProperty AsParameters ($_.Parameters.GetEnumerator() | ? {$_.Key -cmatch '^As([A-Z]|$)'} | % { $_.Key }) } | where { $_.AsParameters } | select Name,AsParameters

Name                   AsParameters
----                   ------------
ConvertTo-Html         As
ConvertTo-SecureString AsPlainText
ConvertTo-Xml          As
Export-Alias           As
Get-EventLog           {AsBaseObject, AsString}
Get-Unique             AsString
Get-WmiObject          AsJob
Group-Object           {AsHashTable, AsString}
Import-Module          AsCustomObject
Invoke-Command         AsJob
Invoke-WmiMethod       AsJob
New-Module             AsCustomObject
Read-Host              AsSecureString
Remove-WmiObject       AsJob
Restart-Computer       AsJob
Set-WmiInstance        AsJob
Stop-Computer          AsJob
Test-Connection        AsJob

代码剖析:

# find all commands
Get-Command |
  # that are cmdlets (exclude aliases or functions)
  Where-Object { $_.CommandType -eq 'Cmdlet' } |
  ForEach-Object {
    # Add another property that contains all parameters
    # that starts with 'As' – I use a regex here to exclude
    # parameters like -Assembly
    $_ | Add-Member -PassThru NoteProperty AsParameters (
      $_.Parameters.GetEnumerator() |
        Where-Object { $_.Key -cmatch '^As([A-Z]|$)' } |
        ForEach-Object { $_.Key }
    )
  } |
  # Exclude commands that don't have parameters that start with 'As'
  Where-Object { $_.AsParameters } |
  Select-Object Name,AsParameters

This might probably have a more elegant solution if one put any effort into it:

PS Home:\> gcm | where { $_.CommandType -eq 'Cmdlet' } | foreach { $_ | Add-Member -PassThru NoteProperty AsParameters ($_.Parameters.GetEnumerator() | ? {$_.Key -cmatch '^As([A-Z]|$)'} | % { $_.Key }) } | where { $_.AsParameters } | select Name,AsParameters

Name                   AsParameters
----                   ------------
ConvertTo-Html         As
ConvertTo-SecureString AsPlainText
ConvertTo-Xml          As
Export-Alias           As
Get-EventLog           {AsBaseObject, AsString}
Get-Unique             AsString
Get-WmiObject          AsJob
Group-Object           {AsHashTable, AsString}
Import-Module          AsCustomObject
Invoke-Command         AsJob
Invoke-WmiMethod       AsJob
New-Module             AsCustomObject
Read-Host              AsSecureString
Remove-WmiObject       AsJob
Restart-Computer       AsJob
Set-WmiInstance        AsJob
Stop-Computer          AsJob
Test-Connection        AsJob

Code dissected:

# find all commands
Get-Command |
  # that are cmdlets (exclude aliases or functions)
  Where-Object { $_.CommandType -eq 'Cmdlet' } |
  ForEach-Object {
    # Add another property that contains all parameters
    # that starts with 'As' – I use a regex here to exclude
    # parameters like -Assembly
    $_ | Add-Member -PassThru NoteProperty AsParameters (
      $_.Parameters.GetEnumerator() |
        Where-Object { $_.Key -cmatch '^As([A-Z]|$)' } |
        ForEach-Object { $_.Key }
    )
  } |
  # Exclude commands that don't have parameters that start with 'As'
  Where-Object { $_.AsParameters } |
  Select-Object Name,AsParameters
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文