查询子集合

时间:2012-06-22 11:35:11

标签: powershell powershell-v3.0

我正在使用Powershell v3.0的RC 我想知道如何查询和显示返回对象中的列表(如Get-Module -ListAvailable结果中的“ExportedCommands”属性):

PS H:\> get-module -ListAvailable


    Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules


ModuleType Name                                ExportedCommands
---------- ----                                ----------------
Manifest   AppLocker                           {Set-AppLockerPolicy, Get-AppLockerPolicy, Test-AppLockerPolicy, Get-...
Manifest   BitsTransfer                        {Add-BitsFile, Remove-BitsTransfer, Complete-BitsTransfer, Get-BitsTr...
Manifest   CimCmdlets                          {Get-CimAssociatedInstance, Get-CimClass, Get-CimInstance, Get-CimSes...

2 个答案:

答案 0 :(得分:1)

嗯..一切都取决于两个因素:

  • 你的收藏实际上是什么(在这个命令中 - 你可以字典)
  • 你想它还是行动

在这种情况下,知道这是一个字典集合,你可以:

Get-Module -ListAvailable | 
    Format-List Name, @{ 
        Name = 'ExportedCommands'
        Expression = { 
            $_.ExportedCommands.Keys -join "`n" 
        }
    }

..或者你可以找到那些钥匙 - 但是你会忘记模块。 或者你可以改变它:

Get-Command | Sort Module | Format-Table Name -GroupBy Module

但同样:这只是显示数据。如果你想对它进行操作,format- *对你没有帮助,你应该使用select(可能是wit -expand参数)。

答案 1 :(得分:0)

OP的帖子中的问题是,PowerShell默认只显示此类重复组中的前N个值。因此,对于导出的命令,您只能看到前4个(默认值为4)。要更改此设置,只需将更高的值设置为varible $ FormatEnumerationLimit。根据Format-Table的功能,您可能还需要在显式管道上指定-wrap,如下所示:

PSH [C:\foo]: $FormatEnumerationLimit = 99
PSH [C:\foo]: get-module -ListAvailable | ft -wrap


    Directory: C:\Users\tfl.COOKHAM\Documents\WindowsPowerShell\Modules


ModuleType Name                                ExportedCommands
---------- ----                                ----------------
Manifest   Audit
Script     authenticode                        {New-PoshCode, Get-PoshCode, Get-PoshCodeUpgrade, Get-WebFile, Set-DownloadFlag,
                                               Remove-DownloadFlag, Get-DownloadFlag, Test-DownloadFlag, block, unblock,
                                               Search-PoshCode}
Script     DotNet                              {Get-Type, Get-ProgID, Get-CommandWithParameterType}
Manifest   DTW.PS.FIleSystem                   Get-DTWFileEncoding
Manifest   DTW.PS.PrettyPrinterV1              Get-DTWFileEncoding
Script     EZOut                               {Add-FormatData, Clear-FormatData, Remove-FormatData, Out-FormatData,
                                               Show-CustomAction, Write-FormatView, Write-CustomAction, Write-FormatTableView,
                                               Get-FormatFile, Find-FormatView, Get-PropertySet, Add-TypeData, Clear-TypeData,
                                               Remove-TypeData, Out-TypeData, Write-TypeView}
Script     FileSystem                          {Copy-ToZip, Get-DuplicateFile, Get-FreeDiskSpace, Get-SHA1, New-Zip,
                                               Mount-SpecialFolder, Rename-Drive, Resolve-ShortcutFile, Start-FileSystemWatcher}
Manifest   FileTransfer                        {Add-BitsFile, Remove-BitsTransfer, Complete-BitsTransfer, Get-BitsTransfer,
                                               Start-BitsTransfer, Resume-BitsTransfer, Set-BitsTransfer, Suspend-BitsTransfer}
Manifest   hyperv                              {Add-ZIPContent, ConvertTo-Enum, Copy-ZipContent, Get-ZIPContent, Select-Item,
                                               Select-List, Select-EnumType, Out-Tree, Select-Tree, Test-Admin,
                                               Convert-DiskIDtoDrive, Get-FirstAvailableDriveLetter, New-Zip, Test-WMIJob,
                                               Test-WMIResult}
Script     IsePack                             {Add-ForeachStatement, Add-IfStatement, Add-InlineHelp, Add-IseMenu,
                                               Add-Parameter, Add-PInvoke, Add-SwitchStatement, Close-AllOpenedFiles,
                                               ConvertTo-ShortcutKeyTable, Copy-Colored, Copy-ColoredHTML, Export-FormatView,
                                               Get-CurrentOpenedFileToken, Get-CurrentToken, Get-FunctionFromFile,
                                               Get-TokenFromFile, Invoke-Line, Move-ToLastGroup, Move-ToLastPowerShellTab,
                                               Move-ToNextGroup, Move-ToNextPowerShellTab, New-IseScript,
                                               New-ScriptModuleFromCurrentLocation, Push-CurrentFileLocation,
                                               Save-IseFileWithAutoName, Select-AllInFile, Select-CurrentText,
                                               Select-CurrentTextAsCommand, Select-CurrentTextAsType,
                                               Select-CurrentTextAsVariable, Show-HelpForCurrentSelection, Show-Member,
                                               Show-SyntaxForCurrentCommand, Show-TypeConstructor,
                                               Show-TypeConstructorForCurrentType, Split-IseFile, Switch-CommentOrText,
                                               Switch-SelectedCommentOrText, Write-ColorizedHTML}

...为了简洁而剪断**