1

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

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...
4

2 回答 2

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 (可能带有 -expand 参数)。

于 2012-06-22T11:58:01.433 回答
0

OP 帖子中的问题是,默认情况下,PowerShell 仅显示此类型重复组中的前 N ​​个值。因此,对于导出的命令,您只能看到前 4 个(默认为 4 个)。要改变这一点,只需为变量 $FormatEnumerationLimit 设置一个更高的值。并且根据 Format-Table 的能力,您可能还需要在指向 FT 的显式管道上指定 -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}

...为简洁起见截断**

于 2012-07-19T09:09:37.087 回答