0

问题是,我需要通过系统管理器文档访问多个服务器。为此,我需要创建一个调用 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"                     
                ]
            }
        ]
    }
}

}

4

2 回答 2

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。

于 2018-01-22T18:46:45.333 回答
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'",
                  "        }",
                  "    }"
                ]
            }
        ]
    }
}
}
于 2018-01-24T23:17:13.910 回答