Cmdlet 发现和别名 - AWS Tools for PowerShell (第 4 版)

的版本 5 (V5) AWS Tools for PowerShell 已经发布!

要开始使用新版本的工具,请参阅AWS Tools for PowerShell 用户指南 (V5),尤其是迁移到 V 5 的主题。

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

Cmdlet 发现和别名

本节向您介绍如何列出支持的服务 AWS Tools for PowerShell、如何显示为支持这些服务而提供的 cmdlet 集,以及如何查找备用 cmdlet 名称(也称为别名)来访问这些服务。 AWS Tools for PowerShell

Cmdlet 发现

所有 AWS 服务操作(或 APIs)均记录在每项服务的 API 参考指南中。例如,请参阅 IAM API 参考。在大多数情况下, AWS 服务 API 和 AWS PowerShell cmdlet 之间存在 one-to-one对应关系。要获取与服务 API 名称对应的 cmdlet 名称,请运行带有-ApiOperation参数和 AWS 服务 API 名称的 AWS Get-AWSCmdletName cmdlet。 AWS 例如,要获取基于任何可用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 和服务的名称,则可以将-Service参数与 cmdlet 名词前缀或服务名称的一部分一起包括在内。 AWS 例如,亚马逊的 cmdlet 名词前缀是。 EC2 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 或服务的名称,则可以使用-ApiOperation参数、要匹配的模式和-MatchWithRegex参数。 AWS 例如,要获取包含 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-AWSCmdletNamecmdlet 只需要足够的 AWS CLI 命令名称即可识别服务和 API。 AWS

要获取 PowerShell 核心工具中所有 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

要过滤 cmdlet 返回的 cmdlet,请将输出通过管道传输到 Get-Command cmdlet。 PowerShell Select-String例如,要查看适用于 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 Ev CloudWatch ents 服务的 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 SDK 为该服务提供的方法。但是,由于强制 PowerShell性的命名约定,cmdlet 的名称可能与其所基于的 API 调用或方法的名称不同。例如,Get-EC2Instancecmdlet 基于 Ama EC2 DescribeInstances zon 方法。

在某些情况下,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

要使用获取 S3 对象 AWS Tools for PowerShell,请运行 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
注意

cmdlet 的 cmdlet 帮助提供 AWS 了 cmdlet 所基于的 AWS SDK API 的名称。

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

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

重要

AWS cmdlet 不支持该交换机。-WhatIf

别名

的安装程序会 AWS Tools for PowerShell 安装一个别名文件,其中包含许多 cmdlet 的 AWS 别名。您可能会发现这些别名比 cmdlet 名称更直观。例如,服务名称和 AWS SDK 方法名称会替 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 ...

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

PS > $MaximumAliasCount = 32768

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

PS > $MaximumAliasCount 32768