【问题标题】:AWS System Manager JSON powershell delete files after X days runpowershellscriptAWS System Manager JSON powershell 在运行 X 天后删除文件 powershellscript
【发布时间】:2018-01-25 07:17:13
【问题描述】:

问题,我需要通过系统管理器文档访问多个服务器。为此,我需要创建一个调用 aws runpowershellscript 的 json,它将调用使用 powershell 脚本在 X 天后查找和删除文件。除了各种通用演练之外,我很难找到我需要的任何东西。下面没有验证为一个好的 json,我假设 powershell 脚本不完整,但我不知道缺少什么。有的是:

{
"schemaVersion": "1.2",
"description": "List information about the .NET Framework version. We recommend exporting results to an Amazon S3 bucket. Output can exceed the maximum.",
"runtimeConfig": {
    "aws:runPowerShellScript": {
        "properties": [
            {
                "id": "0.aws:runPowerShellScript",
                "runCommand": [                     
                  "  Get-ChildItem –Path 'c:\inetpub\* -Recurse -Filter *.log' | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-15))} | Remove-Item"                     
                ]
            }
        ]
    }
}

}

【问题讨论】:

  • 所以我能够让它运行但修改但它忽略了旧标志。

标签: json powershell amazon-web-services system aws-ssm


【解决方案1】:

这是怎么回事? -Path 有不同的字符,\s 没有转义。

{
	"schemaVersion": "1.2",
	"description": "List information about the .NET Framework version. We recommend exporting results to an Amazon S3 bucket. Output can exceed the maximum.",
	"runtimeConfig": {
		"aws:runPowerShellScript": {
			"properties": [{
				"id": "0.aws:runPowerShellScript",
				"runCommand": [
					"Get-ChildItem -Path \"c:\\inetpub\\*\" -Recurse -Filter *.log | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-15))} | Remove-Item"
				]
			}]
		}
	}
}

因此,我发现使用 SSM 编写一个单独的 cmdlet 会更容易,它只需要一个 .ps1 文件,对其进行转义和 json-ify,然后将其放入 SSM JSON。

【讨论】:

  • 谢谢!我会记下这一点。我感到很沮丧,于是直接与 AWS 交谈,并得到了一些不同方法的答案。
【解决方案2】:

我最终使用的作品。看到最后一个人的建议太晚了:

    {
"schemaVersion": "1.2",
"description": "List information about the .NET Framework version. We recommend exporting results to an Amazon S3 bucket. Output can exceed the maximum.",
"runtimeConfig": {
    "aws:runPowerShellScript": {
        "properties": [
            {
                "id": "0.aws:runPowerShellScript",
                "runCommand": [
                  "$Now = Get-Date",
                  "$Days = '14'",
                  "$TargetFolder = 'C:\\inetpub\\*'",
                  "$LastWrite = $Now.AddDays(-$Days)",
                  "$Files = Get-Childitem $TargetFolder -Include *.log*, *.txt* -Recurse | Where {$_.LastWriteTime -le $LastWrite}",
                  "foreach ($File in $Files)", 
                  "    {",
                  "    if ($File -ne $NULL)",
                  "        {",
                  "        write-host 'Deleting File $File' -ForegroundColor 'White'",
                  "        Remove-Item $File.FullName | out-null",
                  "        }",
                  "    else",
                  "        {",
                  "        Write-Host 'No more files to delete!' -foregroundcolor 'Green'",
                  "        }",
                  "    }"
                ]
            }
        ]
    }
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多