Find-Command

在模块中查找 PowerShell 命令。

语法

Find-Command
    [[-Name] <String[]>]
    [-ModuleName <String>]
    [-MinimumVersion <String>]
    [-MaximumVersion <String>]
    [-RequiredVersion <String>]
    [-AllVersions]
    [-AllowPrerelease]
    [-Tag <String[]>]
    [-Filter <String>]
    [-Proxy <Uri>]
    [-ProxyCredential <PSCredential>]
    [-Repository <String[]>]
    [<CommonParameters>]

说明

Find-Command cmdlet 查找 PowerShell 命令,例如 cmdlet、别名、函数和工作流。 Find-Command 在已注册的存储库中搜索模块。

对于找到 Find-Command的每个命令,将返回 PSGetCommandInfo 对象。 可以将 PSGetCommandInfo 对象向下Install-Module发送到 cmdlet。 Install-Module 安装包含命令的模块。

示例

示例 1:查找指定存储库中的所有命令

Find-Command cmdlet 在已注册的存储库中搜索模块。

Find-Command -Repository PSGallery | Select-Object -First 10

Name                                Version    ModuleName          Repository
----                                -------    ----------          ----------
Disable-AzureRmDataCollection       5.8.3      AzureRM.profile     PSGallery
Disable-AzureRmContextAutosave      5.8.3      AzureRM.profile     PSGallery
Enable-AzureRmDataCollection        5.8.3      AzureRM.profile     PSGallery
Enable-AzureRmContextAutosave       5.8.3      AzureRM.profile     PSGallery
Remove-AzureRmEnvironment           5.8.3      AzureRM.profile     PSGallery
Get-AzureRmEnvironment              5.8.3      AzureRM.profile     PSGallery
Set-AzureRmEnvironment              5.8.3      AzureRM.profile     PSGallery
Add-AzureRmEnvironment              5.8.3      AzureRM.profile     PSGallery
Get-AzureRmSubscription             5.8.3      AzureRM.profile     PSGallery
Connect-AzureRmAccount              5.8.3      AzureRM.profile     PSGallery

Find-Command 使用 存储库 参数指定已注册的存储库名称。 对象会向下发送管道。 Select-Object 接收对象并使用 First 参数显示前 10 个结果。

示例 2:按名称查找命令

Find-Command 可以使用命令的名称在存储库中查找模块。 命令名称可能存在于多个 ModuleNames 中。

Find-Command -Repository PSGallery -Name Get-TargetResource

Name                  Version    ModuleName                      Repository
----                  -------    ----------                      ----------
Get-TargetResource    3.1.0.0    xPowerShellExecutionPolicy      PSGallery
Get-TargetResource    1.0.0      xInternetExplorerHomePage       PSGallery
Get-TargetResource    1.2.0.0    SystemLocaleDsc                 PSGallery

Find-Command 使用 存储库 参数搜索 PSGalleryName 参数指定命令Get-TargetResource

示例 3:按名称查找命令并安装模块

Find-Command 可以找到命令和模块,然后将对象发送到 Install-Module。 如果命令包含在多个模块中,请使用 Find-Command cmdlets ModuleName 参数。 否则,可能会安装不想安装的模块。

PS> Find-Command -Name Get-TargetResource -Repository PSGallery -ModuleName SystemLocaleDsc |
    Install-Module

PS> Get-InstalledModule

Version   Name               Repository   Description
-------   ----               ----------   -----------
1.2.0.0   SystemLocaleDsc    PSGallery    This DSC Resource allows configuration of the Windows...

Find-Command 使用 Name 参数指定命令 Get-TargetResource存储库参数搜索 PSGalleryModuleName 参数指定要安装的模块 SystemLocaleDsc。 该对象将发送到管道 Install-Module 并安装模块。 安装完成后,可用于 Get-InstalledModule 显示结果。

示例 4:查找命令并保存其模块

PS> Find-Command -Name Invoke-ScriptAnalyzer -Repository PSGallery | Save-Module -Path C:\Test\Modules -Verbose

VERBOSE: Downloading 'https://www.powershellgallery.com/api/v2/package/PSScriptAnalyzer/1.18.0'.
VERBOSE: Completed downloading 'https://www.powershellgallery.com/api/v2/package/PSScriptAnalyzer/1.18.0'.
VERBOSE: Completed downloading 'PSScriptAnalyzer'.
VERBOSE: Module 'PSScriptAnalyzer' was saved successfully to path 'C:\Test\Modules\PSScriptAnalyzer\1.18.0'.

Find-Command使用 NameRepository 参数搜索 PSGallery 存储库中的命令Invoke-ScriptAnalyzer。 将对象向下发送到管道 Save-ModulePath 参数确定保存模块的位置。 详细 是可选参数,但在 PowerShell 控制台中显示状态输出。 详细输出有利于故障排除。

参数

-AllowPrerelease

在结果中包含标记为预发行版的模块。

类型:SwitchParameter
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-AllVersions

指示此 cmdlet 获取模块的所有版本。

类型:SwitchParameter
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-Filter

根据 PackageManagement 提供程序的搜索语法查找模块。 例如,指定要在 ModuleNameDescription 属性中搜索的字词。

类型:String
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-MaximumVersion

指定要包含在结果中的模块的最大版本。 MaximumVersionRequiredVersion 参数不能在同一命令中使用。

类型:String
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-MinimumVersion

指定要包含在结果中的模块的最低版本。 MinimumVersionRequiredVersion 参数不能在同一命令中使用。

类型:String
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-ModuleName

指定要搜索命令的模块的名称。 默认值为所有模块。

类型:String
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-Name

指定要在存储库中搜索的命令名称。 使用逗号分隔命令名称数组。

类型:String[]
Position:0
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-Proxy

为请求指定代理服务器,而不是直接连接到 Internet 资源。

类型:Uri
Position:Named
默认值:None
必需:False
接受管道输入:True
接受通配符:False

-ProxyCredential

指定有权使用 Proxy 参数指定的代理服务器的用户帐户。

类型:PSCredential
Position:Named
默认值:None
必需:False
接受管道输入:True
接受通配符:False

-Repository

指定要搜索命令的存储库。 使用逗号分隔存储库名称数组。 默认值为所有存储库。

类型:String[]
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-RequiredVersion

指定要包含在结果中的模块版本。

类型:String
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-Tag

指定对存储库中的模块进行分类的标记。 使用逗号分隔标记数组。

类型:String[]
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

输出

PSGetCommandInfo

Find-Command 输出 PSGetCommandInfo 对象。

备注

重要

截至 2020 年 4 月,PowerShell 库不再支持传输层安全性(TLS)版本 1.0 和 1.1。 如果未使用 TLS 1.2 或更高版本,则尝试访问 PowerShell 库时会收到错误。 使用以下命令确保使用的是 TLS 1.2:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

有关详细信息,请参阅 PowerShell 博客中的 公告