Cmdlet 发现和别名 - AWS Tools for PowerShell

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

Cmdlet 发现和别名

此部分介绍如何列出AWS Tools for PowerShell支持的服务,如何显示AWS Tools for PowerShell为支持这些服务而提供的 cmdlet 集,以及如何查找用于访问这些服务的替代 cmdlet 名称(也称为别名)。

Cmdlet 发现

所有AWS服务操作(或 API)都记录在每项服务的 API 参考指南中。例如,请参阅 IAM API 参考。在大多数情况下,AWS服务 API 和 AWS PowerShell cmdlet 之间存在一对一的对应关系。要获取与 AWS 服务 API 名称对应的 cmdlet 名称,请使用 -ApiOperation 参数和AWS服务 API 名称运行 AWS Get-AWSCmdletName cmdlet。例如,要获取基于任何可用的 DescribeInstances AWS服务 API 的所有可能的 cmdlet 名称,请运行以下命令:

PS > Get-AWSCmdletName -ApiOperation DescribeInstances CmdletName ServiceOperation ServiceName CmdletNounPrefix ---------- ---------------- ----------- ---------------- Get-EC2Instance DescribeInstances Amazon Elastic Compute Cloud EC2 Get-GMLInstance DescribeInstances Amazon GameLift Service GML

-ApiOperation 参数是默认参数,因此您可以省略参数名称。以下示例等同于前一个示例:

PS > Get-AWSCmdletName DescribeInstances

如果您知道 API 和 AWS 服务的名称,则可以将 -Service 参数以及 cmdlet 名词前缀包含为AWS服务名称的一部分。例如,Amazon EC2 的 cmdlet 名词前缀是 EC2。要在 Amazon EC2 服务中获取与 DescribeInstances API 对应的 cmdlet 名称,请运行以下命令之一。它们都会产生相同的输出:

PS > Get-AWSCmdletName -ApiOperation DescribeInstances -Service EC2 PS > Get-AWSCmdletName -ApiOperation DescribeInstances -Service Compute PS > Get-AWSCmdletName -ApiOperation DescribeInstances -Service "Compute Cloud" CmdletName ServiceOperation ServiceName CmdletNounPrefix ---------- ---------------- ----------- ---------------- Get-EC2Instance DescribeInstances Amazon Elastic Compute Cloud EC2

这些命令中的参数值不区分大小写。

如果您不知道所需AWS服务 API 或AWS服务的名称,可以使用 -ApiOperation 参数以及要匹配的模式和 -MatchWithRegex 参数。例如,要获取包含 SecurityGroup 的所有可用的 cmdlet 名称,请运行以下命令:

PS > Get-AWSCmdletName -ApiOperation SecurityGroup -MatchWithRegex CmdletName ServiceOperation ServiceName CmdletNounPrefix ---------- ---------------- ----------- ---------------- Approve-ECCacheSecurityGroupIngress AuthorizeCacheSecurityGroupIngress Amazon ElastiCache EC Get-ECCacheSecurityGroup DescribeCacheSecurityGroups Amazon ElastiCache EC New-ECCacheSecurityGroup CreateCacheSecurityGroup Amazon ElastiCache EC Remove-ECCacheSecurityGroup DeleteCacheSecurityGroup Amazon ElastiCache EC Revoke-ECCacheSecurityGroupIngress RevokeCacheSecurityGroupIngress Amazon ElastiCache EC Add-EC2SecurityGroupToClientVpnTargetNetwrk ApplySecurityGroupsToClientVpnTargetNetwork Amazon Elastic Compute Cloud EC2 Get-EC2SecurityGroup DescribeSecurityGroups Amazon Elastic Compute Cloud EC2 Get-EC2SecurityGroupReference DescribeSecurityGroupReferences Amazon Elastic Compute Cloud EC2 Get-EC2StaleSecurityGroup DescribeStaleSecurityGroups Amazon Elastic Compute Cloud EC2 Grant-EC2SecurityGroupEgress AuthorizeSecurityGroupEgress Amazon Elastic Compute Cloud EC2 Grant-EC2SecurityGroupIngress AuthorizeSecurityGroupIngress Amazon Elastic Compute Cloud EC2 New-EC2SecurityGroup CreateSecurityGroup Amazon Elastic Compute Cloud EC2 Remove-EC2SecurityGroup DeleteSecurityGroup Amazon Elastic Compute Cloud EC2 Revoke-EC2SecurityGroupEgress RevokeSecurityGroupEgress Amazon Elastic Compute Cloud EC2 Revoke-EC2SecurityGroupIngress RevokeSecurityGroupIngress Amazon Elastic Compute Cloud EC2 Update-EC2SecurityGroupRuleEgressDescription UpdateSecurityGroupRuleDescriptionsEgress Amazon Elastic Compute Cloud EC2 Update-EC2SecurityGroupRuleIngressDescription UpdateSecurityGroupRuleDescriptionsIngress Amazon Elastic Compute Cloud EC2 Edit-EFSMountTargetSecurityGroup ModifyMountTargetSecurityGroups Amazon Elastic File System EFS Get-EFSMountTargetSecurityGroup DescribeMountTargetSecurityGroups Amazon Elastic File System EFS Join-ELBSecurityGroupToLoadBalancer ApplySecurityGroupsToLoadBalancer Elastic Load Balancing ELB Set-ELB2SecurityGroup SetSecurityGroups Elastic Load Balancing V2 ELB2 Enable-RDSDBSecurityGroupIngress AuthorizeDBSecurityGroupIngress Amazon Relational Database Service RDS Get-RDSDBSecurityGroup DescribeDBSecurityGroups Amazon Relational Database Service RDS New-RDSDBSecurityGroup CreateDBSecurityGroup Amazon Relational Database Service RDS Remove-RDSDBSecurityGroup DeleteDBSecurityGroup Amazon Relational Database Service RDS Revoke-RDSDBSecurityGroupIngress RevokeDBSecurityGroupIngress Amazon Relational Database Service RDS Approve-RSClusterSecurityGroupIngress AuthorizeClusterSecurityGroupIngress Amazon Redshift RS Get-RSClusterSecurityGroup DescribeClusterSecurityGroups Amazon Redshift RS New-RSClusterSecurityGroup CreateClusterSecurityGroup Amazon Redshift RS Remove-RSClusterSecurityGroup DeleteClusterSecurityGroup Amazon Redshift RS Revoke-RSClusterSecurityGroupIngress RevokeClusterSecurityGroupIngress Amazon Redshift RS

如果您知道AWS服务的名称,但不知道AWS服务 API 的名称,请同时添加 -MatchWithRegex 参数和 -Service 参数以将搜索范围限定为单个服务。例如,要仅在 Amazon EC2 服务中获取包含 SecurityGroup 的所有 cmdlet 名称,请运行以下命令

PS > Get-AWSCmdletName -ApiOperation SecurityGroup -MatchWithRegex -Service EC2 CmdletName ServiceOperation ServiceName CmdletNounPrefix ---------- ---------------- ----------- ---------------- Add-EC2SecurityGroupToClientVpnTargetNetwrk ApplySecurityGroupsToClientVpnTargetNetwork Amazon Elastic Compute Cloud EC2 Get-EC2SecurityGroup DescribeSecurityGroups Amazon Elastic Compute Cloud EC2 Get-EC2SecurityGroupReference DescribeSecurityGroupReferences Amazon Elastic Compute Cloud EC2 Get-EC2StaleSecurityGroup DescribeStaleSecurityGroups Amazon Elastic Compute Cloud EC2 Grant-EC2SecurityGroupEgress AuthorizeSecurityGroupEgress Amazon Elastic Compute Cloud EC2 Grant-EC2SecurityGroupIngress AuthorizeSecurityGroupIngress Amazon Elastic Compute Cloud EC2 New-EC2SecurityGroup CreateSecurityGroup Amazon Elastic Compute Cloud EC2 Remove-EC2SecurityGroup DeleteSecurityGroup Amazon Elastic Compute Cloud EC2 Revoke-EC2SecurityGroupEgress RevokeSecurityGroupEgress Amazon Elastic Compute Cloud EC2 Revoke-EC2SecurityGroupIngress RevokeSecurityGroupIngress Amazon Elastic Compute Cloud EC2 Update-EC2SecurityGroupRuleEgressDescription UpdateSecurityGroupRuleDescriptionsEgress Amazon Elastic Compute Cloud EC2 Update-EC2SecurityGroupRuleIngressDescription UpdateSecurityGroupRuleDescriptionsIngress Amazon Elastic Compute Cloud EC2

如果您知道 AWS Command Line Interface (AWS CLI) 命令的名称,可以使用 -AwsCliCommand 参数和所需的 AWS CLI 命令名称以获取基于相同 API 的 cmdlet 名称。例如,要在 Amazon EC2 服务中获取与 authorize-security-group-ingress AWS CLI 命令调用对应的 cmdlet 名称,请运行以下命令:

PS > Get-AWSCmdletName -AwsCliCommand "aws ec2 authorize-security-group-ingress" CmdletName ServiceOperation ServiceName CmdletNounPrefix ---------- ---------------- ----------- ---------------- Grant-EC2SecurityGroupIngress AuthorizeSecurityGroupIngress Amazon Elastic Compute Cloud EC2

Get-AWSCmdletName cmdlet 仅需要具有 AWS CLI 命令名称即可识别服务和AWS API。

要在 Tools for PowerShell Core 中获取所有 cmdlet 的列表,请运行 PowerShell Get-Command cmdlet,如以下示例所示。

PS > Get-Command -Module AWSPowerShell.NetCore

您可以使用 -Module AWSPowerShell 运行相同的命令以查看 AWS Tools for Windows PowerShell 中的 cmdlet。

Get-Command cmdlet 按字母顺序生成 cmdlet 列表。请注意,默认情况下,此列表按照 PowerShell 命令动词而不是 PowerShell 名词排序。

要改为按服务对结果排序,请运行以下命令:

PS > Get-Command -Module AWSPowerShell.NetCore | Sort-Object Noun,Verb

要筛选 Get-Command cmdlet 返回的 cmdlet 列表,请将输出输送到 PowerShell Select-String cmdlet。例如,要查看适用于AWS区域的一组 cmdlet,请运行以下命令:

PS > Get-Command -Module AWSPowerShell.NetCore | Select-String region Clear-DefaultAWSRegion Copy-HSM2BackupToRegion Get-AWSRegion Get-DefaultAWSRegion Get-EC2Region Get-LSRegionList Get-RDSSourceRegion Set-DefaultAWSRegion

您还可以通过筛选 cmdlet 名词的服务前缀来查找特定服务的 cmdlet。要查看可用服务前缀的列表,请运行 Get-AWSPowerShellVersion -ListServiceVersionInfo。以下示例返回支持 Amazon CloudWatch Events 服务的 cmdlet。

PS > Get-Command -Module AWSPowerShell -Noun CWE* CommandType Name Version Source ----------- ---- ------- ------ Cmdlet Add-CWEResourceTag 3.3.563.1 AWSPowerShell.NetCore Cmdlet Disable-CWEEventSource 3.3.563.1 AWSPowerShell.NetCore Cmdlet Disable-CWERule 3.3.563.1 AWSPowerShell.NetCore Cmdlet Enable-CWEEventSource 3.3.563.1 AWSPowerShell.NetCore Cmdlet Enable-CWERule 3.3.563.1 AWSPowerShell.NetCore Cmdlet Get-CWEEventBus 3.3.563.1 AWSPowerShell.NetCore Cmdlet Get-CWEEventBusList 3.3.563.1 AWSPowerShell.NetCore Cmdlet Get-CWEEventSource 3.3.563.1 AWSPowerShell.NetCore Cmdlet Get-CWEEventSourceList 3.3.563.1 AWSPowerShell.NetCore Cmdlet Get-CWEPartnerEventSource 3.3.563.1 AWSPowerShell.NetCore Cmdlet Get-CWEPartnerEventSourceAccountList 3.3.563.1 AWSPowerShell.NetCore Cmdlet Get-CWEPartnerEventSourceList 3.3.563.1 AWSPowerShell.NetCore Cmdlet Get-CWEResourceTag 3.3.563.1 AWSPowerShell.NetCore Cmdlet Get-CWERule 3.3.563.1 AWSPowerShell.NetCore Cmdlet Get-CWERuleDetail 3.3.563.1 AWSPowerShell.NetCore Cmdlet Get-CWERuleNamesByTarget 3.3.563.1 AWSPowerShell.NetCore Cmdlet Get-CWETargetsByRule 3.3.563.1 AWSPowerShell.NetCore Cmdlet New-CWEEventBus 3.3.563.1 AWSPowerShell.NetCore Cmdlet New-CWEPartnerEventSource 3.3.563.1 AWSPowerShell.NetCore Cmdlet Remove-CWEEventBus 3.3.563.1 AWSPowerShell.NetCore Cmdlet Remove-CWEPartnerEventSource 3.3.563.1 AWSPowerShell.NetCore Cmdlet Remove-CWEPermission 3.3.563.1 AWSPowerShell.NetCore Cmdlet Remove-CWEResourceTag 3.3.563.1 AWSPowerShell.NetCore Cmdlet Remove-CWERule 3.3.563.1 AWSPowerShell.NetCore Cmdlet Remove-CWETarget 3.3.563.1 AWSPowerShell.NetCore Cmdlet Test-CWEEventPattern 3.3.563.1 AWSPowerShell.NetCore Cmdlet Write-CWEEvent 3.3.563.1 AWSPowerShell.NetCore Cmdlet Write-CWEPartnerEvent 3.3.563.1 AWSPowerShell.NetCore Cmdlet Write-CWEPermission 3.3.563.1 AWSPowerShell.NetCore Cmdlet Write-CWERule 3.3.563.1 AWSPowerShell.NetCore Cmdlet Write-CWETarget 3.3.563.1 AWSPowerShell.NetCore

cmdlet 命名和别名

AWS Tools for PowerShell 中适用于每个服务的 cmdlet 基于该服务的 AWS 开发工具包提供的方法。但是,由于 PowerShell 的必须遵守的命名约定,cmdlet 名称可能与它基于的 API 调用或方法名称不同。例如,Get-EC2Instance cmdlet 基于 Amazon EC2 DescribeInstances 方法。

在某些情况下,cmdlet 名称可能与方法名称类似,但实际上可能执行不同的功能。例如,Amazon S3 GetObject 方法检索 Amazon S3 对象。但是,Get-S3Object cmdlet 返回有关 Amazon S3 对象的信息,而非对象本身。

PS > Get-S3Object -BucketName text-content -Key aws-tech-docs ETag : "df000002a0fe0000f3c000004EXAMPLE" BucketName : aws-tech-docs Key : javascript/frameset.js LastModified : 6/13/2011 1:24:18 PM Owner : Amazon.S3.Model.Owner Size : 512 StorageClass : STANDARD

要使用AWS Tools for PowerShell获取 S3 对象,请运行 Read-S3Object cmdlet:

PS > Read-S3Object -BucketName text-content -Key text-object.txt -file c:\tmp\text-object-download.text Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 11/5/2012 7:29 PM 20622 text-object-download.text
注意

AWS cmdlet 的 cmdlet 帮助提供了该 cmdlet 基于的AWS开发工具包 API 名称。

有关标准 PowerShell 动词及其含义的更多信息,请参阅批准的 PowerShell 命令动词

使用 Remove 动词的所有AWS cmdlet(以及在添加 -Terminate 参数时使用的 Stop-EC2Instance cmdlet)都会提示您在继续前进行确认。要绕过确认,请在命令中添加 -Force 参数。

重要

AWS cmdlet 不支持 -WhatIf 开关。

Aliases

AWS Tools for PowerShell 安装程序会安装一个别名文件,其中包含很多AWS cmdlet 的别名。您可能会发现这些别名比 cmdlet 名称更直观。例如,在某些别名中,服务名称和AWS开发工具包方法名称替换 PowerShell 动词和名词。例如 EC2-DescribeInstances 别名。

其他别名使用动词,尽管不遵循标准 PowerShell 约定,但它们可以更好地描述实际操作。例如,别名文件将别名 Get-S3Content 映射到 cmdlet Read-S3Object

PS > Set-Alias -Name Get-S3Content -Value Read-S3Object

别名文件位于AWS Tools for PowerShell安装目录中。要将别名加载到您的环境中,请对该文件使用 dot-source。下面是一个基于 Windows 的示例。

PS > . "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowershell\AWSAliases.ps1"

对于 Linux 或 macOS shell,它可能看起来像这样:

. ~/.local/share/powershell/Modules/AWSPowerShell.NetCore/3.3.563.1/AWSAliases.ps1

要显示所有AWS Tools for PowerShell别名,请运行以下命令。该命令在 PowerShell Where-Object cmdlet 和 Source 属性中使用 ? 别名以筛选仅来自 AWSPowerShell.NetCore 模块的别名。

PS > Get-Alias | ? Source -like "AWSPowerShell.NetCore" CommandType Name Version Source ----------- ---- ------- ------ Alias Add-ASInstances 3.3.343.0 AWSPowerShell Alias Add-CTTag 3.3.343.0 AWSPowerShell Alias Add-DPTags 3.3.343.0 AWSPowerShell Alias Add-DSIpRoutes 3.3.343.0 AWSPowerShell Alias Add-ELBTags 3.3.343.0 AWSPowerShell Alias Add-EMRTag 3.3.343.0 AWSPowerShell Alias Add-ESTag 3.3.343.0 AWSPowerShell Alias Add-MLTag 3.3.343.0 AWSPowerShell Alias Clear-AWSCredentials 3.3.343.0 AWSPowerShell Alias Clear-AWSDefaults 3.3.343.0 AWSPowerShell Alias Dismount-ASInstances 3.3.343.0 AWSPowerShell Alias Edit-EC2Hosts 3.3.343.0 AWSPowerShell Alias Edit-RSClusterIamRoles 3.3.343.0 AWSPowerShell Alias Enable-ORGAllFeatures 3.3.343.0 AWSPowerShell Alias Find-CTEvents 3.3.343.0 AWSPowerShell Alias Get-ASACases 3.3.343.0 AWSPowerShell Alias Get-ASAccountLimits 3.3.343.0 AWSPowerShell Alias Get-ASACommunications 3.3.343.0 AWSPowerShell Alias Get-ASAServices 3.3.343.0 AWSPowerShell Alias Get-ASASeverityLevels 3.3.343.0 AWSPowerShell Alias Get-ASATrustedAdvisorCheckRefreshStatuses 3.3.343.0 AWSPowerShell Alias Get-ASATrustedAdvisorChecks 3.3.343.0 AWSPowerShell Alias Get-ASATrustedAdvisorCheckSummaries 3.3.343.0 AWSPowerShell Alias Get-ASLifecycleHooks 3.3.343.0 AWSPowerShell Alias Get-ASLifecycleHookTypes 3.3.343.0 AWSPowerShell Alias Get-AWSCredentials 3.3.343.0 AWSPowerShell Alias Get-CDApplications 3.3.343.0 AWSPowerShell Alias Get-CDDeployments 3.3.343.0 AWSPowerShell Alias Get-CFCloudFrontOriginAccessIdentities 3.3.343.0 AWSPowerShell Alias Get-CFDistributions 3.3.343.0 AWSPowerShell Alias Get-CFGConfigRules 3.3.343.0 AWSPowerShell Alias Get-CFGConfigurationRecorders 3.3.343.0 AWSPowerShell Alias Get-CFGDeliveryChannels 3.3.343.0 AWSPowerShell Alias Get-CFInvalidations 3.3.343.0 AWSPowerShell Alias Get-CFNAccountLimits 3.3.343.0 AWSPowerShell Alias Get-CFNStackEvents 3.3.343.0 AWSPowerShell ...

要在该文件中添加您自己的别名,您可能需要将 PowerShell 的 $MaximumAliasCount 首选项变量值提高到大于 5500 的值。默认值为 4096;您可以将其提高到最多 32768。为此,请运行以下命令。

PS > $MaximumAliasCount = 32768

要验证您的更改是否成功,请输入变量名称以显示其当前值。

PS > $MaximumAliasCount 32768