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<Begin>, C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\Microsoft.PowerShell.Archive\\Microsoft.PowerShell.Archive.psm1: line 135at <ScriptBlock>, C:\\Users\\ansible\\AppData\\Local\\Temp\\ansible-tmp-1629885879.915333-3177-229706684643369\\hmscript.ps1: line 5at <ScriptBlock>, <No file>: 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<Begin>, C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\Microsoft.PowerShell.Archive\\Microsoft.PowerShell.Archive.psm1: line 135at <ScriptBlock>, C:\\Users\\ansible\\AppData\\Local\\Temp\\ansible-tmp-1629885879.915333-3177-229706684643369\\hmscript.ps1: line 5at <ScriptBlock>, <No file>: 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 --- - name: test windows powershell script hosts: windows tasks: - name: trying powershell win_shell: script: | D:\dmlogs\hm\hm-amp2-trace-2021mmdd-hhmm.log $dir = " D:\dmlogs\hm" $filter="hm-amp2-trace-*.log" $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') register: win_output - debug: var=win_output.stdout [ansible@ip-172-31-25-114 playbooks]$ [ansible@ip-172-31-25-114 playbooks]$ ansible-playbook test_win_script.yml PLAY [test windows powershell script] ********************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************************* ok: [172.31.4.147] TASK [trying powershell] *********************************************************************************************************************************************** fatal: [172.31.4.147]: FAILED! => {"changed": false, "msg": "Get-AnsibleParam: Missing required argument: _raw_params"} PLAY RECAP ************************************************************************************************************************************************************* 172.31.4.147 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0 [ansible@ip-172-31-25-114 playbooks]$ -- You received this message because you are subscribed to the Google Groups "Ansible Development" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-devel+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-devel/CANPH-qwjVr%2BT7tig_JSZdidoN_p%3DzwVS%3DiG%3DLCPw%2Ber8Pjn3cw%40mail.gmail.com.