AWS System Manager JSON powershell在X天runpowershellscript之后删除文件

时间:2018-01-17 21:49:09

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

问题是,我需要通过System Manager Document访问多个服务器。为此,我需要创建一个调用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"                     
                ]
            }
        ]
    }
}

}

2 个答案:

答案 0 :(得分:0)

这是怎么回事? -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。

答案 1 :(得分:0)

我最终使用的是什么作品。最后看到最后一个人的建议:

    {
"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'",
                  "        }",
                  "    }"
                ]
            }
        ]
    }
}
}