AWS System Manager JSON powershell 在 X 天运行 powershellscript 后删除文件
AWS System Manager JSON powershell delete files after X days runpowershellscript
这个问题,我需要通过系统管理器文档转到多个服务器。为此,我需要创建一个 json 来调用 aws runpowershellscript,它将调用使用 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"
]
}
]
}
}
}
这个怎么样? -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.
我最终使用的作品。看到最后一个人的建议太晚了:
{
"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'",
" }",
" }"
]
}
]
}
}
}
这个问题,我需要通过系统管理器文档转到多个服务器。为此,我需要创建一个 json 来调用 aws runpowershellscript,它将调用使用 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"
]
}
]
}
}
}
这个怎么样? -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.
我最终使用的作品。看到最后一个人的建议太晚了:
{
"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'",
" }",
" }"
]
}
]
}
}
}