简体   繁体   English

使用 AWS Systems Manager 在多个 Windows EC2 实例中启动或停止服务

[英]Start or Stop services in multiple Windows EC2 Instances using AWS Systems Manager

我们在Organization下的不同账户下有多个EC2实例,我们需要在Windows中停止一个服务,请指导最简单的内置服务来执行这个活动

AWS Run command , which is part of System Manager can be used to execute PowerShell ( Start-Service | Stop-Service ) commands on the instances. AWS Run command是 System Manager 的一部分,可用于在实例上执行 PowerShell ( Start-Service | Stop-Service ) 命令。 You can target multiple instances in the same Org account via instance tags or Instance Ids.您可以通过实例标签或实例 ID 将同一组织账户中的多个实例作为目标。 I dont know of a way to do this across multiple org accounts that could be described as "easiest" or with "built in" tools.我不知道有什么方法可以跨多个组织帐户执行此操作,可以将其描述为“最简单”或“内置”工具。 But...但...

A fairly easy option is to invoke Run command via PowerShell locally/on a server (you need to install the AWS Powershell Tools on this machine).一个相当简单的选择是在本地/服务器上通过 PowerShell 调用 Run 命令(您需要在这台机器上安装AWS Powershell 工具)。

Below command will attempt to:下面的命令将尝试:

  • stop a service named NameOfMyService停止名为NameOfMyService的服务
  • Only on instances with a tag called ROLE and with a value of WEBSERVER仅在标签名为ROLE且值为WEBSERVER实例上
  • using credential named OrgAccountX (ie created with Set-AWSCredential command) you need one profile per account.使用名为OrgAccountX凭证(即使用Set-AWSCredential命令创建)您需要每个账户一个配置文件。
$runPSCommand = Send-SSMCommand `
    -ProfileName OrgAccountX
    -Target "Key=tag:ROLE,Values=WEBSERVER" `
    -DocumentName "AWS-RunPowerShellScript" `
    -Parameter @{'commands'=@('Stop-Service NameOfMyService')}

(adapted from aws docs here ) (改编自此处的 aws 文档

You can easily loop through an array of Profile names like this:您可以轻松地循环遍历一组 Profile 名称,如下所示:

$awsOrgProfiles = @('OrgAccount1', 'OrgAccount2', 'OrgAccountX');

$awsOrgProfiles | % {
   Send-SSMCommand -ProfileName $_ ...
}

( $_ will contain one the ProfileName, rest of the command is the same as above) $_将包含一个 ProfileName,其余命令同上)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM