我使用 CloudFormation 创建了一个 AWS SSM 文档:
SSMDocument::
Type: AWS::SSM::Document
Properties:
DocumentType: Command
Content:
schemaVersion: '2.2'
description: Run a PowerShell script
parameters:
Node1:
type: String
Node2:
type: String
List:
type: StringList
mainSteps:
- action: aws:runPowerShellScript
name: PreDeploy
precondition:
StringEquals:
- platformType
- Windows
inputs:
timeoutSeconds: '3600'
runCommand:
- |
# Retrieve values from parameters
$IpNode1 = "{{Node1}}"
$IpNode2 = "{{Node2}}"
$List = "{{EnvNames}}"
Run Code Online (Sandbox Code Playgroud)
该文档包含PowerShell在新创建的实例上执行的脚本。我正在传递参数,其中之一($List)具有 type StringList,问题是我收到错误:
Parameter "List" is "STRING_LIST" type and can't be used as …Run Code Online (Sandbox Code Playgroud) 如何从列表中删除匹配的项目? <class 'list'>
snapshot_tag = [{'Key': 'Disaster_Recovery', 'Value': 'Full'}, {'Key': 'Backup Initiator Rule', 'Value': 'Daily-6d-retention'}, {'Key': 'Name', 'Value': 'HOSTNAME'}, {'Key': 'aws:backup:source-resource', 'Value': '000AD618-2D20-CE4B-0000-0000B688C579'}, {'Key': 'Backup_Type', 'Value': 'CROSS-REGION'}]
Run Code Online (Sandbox Code Playgroud)
我需要删除一个元素:
{'Key': 'aws:backup:source-resource', 'Value': '000AD618-2D20-CE4B-0000-0000B688C579'}
Run Code Online (Sandbox Code Playgroud)
如果指定完整的元素名称,则可以将其删除:
snapshot_tag.remove({'Key': 'aws:backup:source-resource', 'Value': '000AD618-2D20-CE4B-0000-0000B688C579'})
Run Code Online (Sandbox Code Playgroud)
或指定特定的索引:
del snapshot_tag[3]
但在我的情况下,元素的“值”始终不同,索引随机化。
我正在运行一个名为 的 Windows 服务my-service。我知道的唯一输入是服务名称。
我可以使用服务名称获取进程标识符:
$id = Get-WmiObject -Class Win32_Service -Filter "name='my-service'" | Select-Object -ExpandProperty ProcessId
Run Code Online (Sandbox Code Playgroud)
要获取侦听端口列表,我可以使用netstat,但我所有的自定义服务都有PID 4,它不等于cmdlet的服务进程标识符Get-WmiObject。
目标是根据服务名称或进程标识符(而不是映像名称)使用 PowerShell 或 CMD 来获取服务正在侦听的端口。