powershell

Steps

  1. get the latest file name , file name format : ‘.txt’
  2. check the file content , upto line containing AMP , updated with today’s logs
  3. archive/compress the file
  4. delete the original file

[ansible@ip-172-31-25-114 playbooks]$ cat hmscript.ps1
$dir = “C:\Users\ansible\Documents\logs"
$filter=”*.txt"
$latest = Get-ChildItem -Path $dir -Filter $filter | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.name
Compress-Archive “$dir$latest” -DestinationPath ($dir + (get-date -Format yyyyMMdd) + ‘.zip’)
[ansible@ip-172-31-25-114 playbooks]$

[ansible@ip-172-31-25-114 playbooks]$ cat test_win_script2.yml

  • name: Run powershell script
    hosts: windows
    gather_facts: false
    tasks:
  • name: Run powershell script
    script: ./hmscript.ps1
    register: result
  • debug: var=result.stdout

TASK [Run powershell script] *******************************************************************************************************************************************
fatal: [172.31.4.147]: FAILED! => {“changed”: true, “msg”: “non-zero return code”, “rc”: 1, “stderr”: “The archive file C:\Users\ansible\Documents\logs\20210825.zip already exists. Use the -Update parameter to update the existing archive file or use the -Force parameter to overwrite the existing archive file.At C:\Users\ansible\AppData\Local\Temp\ansible-tmp-1629885879.915333-3177-229706684643369\hmscript.ps1:5 char:1+ Compress-Archive "$dir\$latest" -DestinationPath ($dir + (get-date -F …+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (C:\Users\ansibl…gs\20210825.zip:String) [Compress-Archive], IOException + FullyQualifiedErrorId : ArchiveFileExists,Compress-ArchiveScriptStackTrace:at ThrowTerminatingErrorHelper, C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1: line 1197at Compress-Archive, C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1: line 135at , C:\Users\ansible\AppData\Local\Temp\ansible-tmp-1629885879.915333-3177-229706684643369\hmscript.ps1: line 5at , : line 1System.IO.IOException: The archive file C:\Users\ansible\Documents\logs\20210825.zip already exists. Use the -Update parameter to update the existing archive file or use the -Force parameter to overwrite the existing archive file. at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)”, “stderr_lines”: [“The archive file C:\Users\ansible\Documents\logs\20210825.zip already exists. Use the -Update parameter to update the existing archive file or use the -Force parameter to overwrite the existing archive file.At C:\Users\ansible\AppData\Local\Temp\ansible-tmp-1629885879.915333-3177-229706684643369\hmscript.ps1:5 char:1+ Compress-Archive "$dir\$latest" -DestinationPath ($dir + (get-date -F …+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (C:\Users\ansibl…gs\20210825.zip:String) [Compress-Archive], IOException + FullyQualifiedErrorId : ArchiveFileExists,Compress-ArchiveScriptStackTrace:at ThrowTerminatingErrorHelper, C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1: line 1197at Compress-Archive, C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1: line 135at , C:\Users\ansible\AppData\Local\Temp\ansible-tmp-1629885879.915333-3177-229706684643369\hmscript.ps1: line 5at , : line 1System.IO.IOException: The archive file C:\Users\ansible\Documents\logs\20210825.zip already exists. Use the -Update parameter to update the existing archive file or use the -Force parameter to overwrite the existing archive file. at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)”], “stdout”: “hm-amp2-trace-20210825-0001.txt\r\n”, “stdout_lines”: [“hm-amp2-trace-20210825-0001.txt”]}

[ansible@ip-172-31-25-114 playbooks]$ cat test_win_script.yml