# PowerShell script in windows host

**URL:** <https://forum.ansible.com/t/powershell-script-in-windows-host/35675>\
**Category:** Ansible Developer\
**Tags:** windows\
**Created:** [August 25, 2021, 10:25am UTC](https://forum.ansible.com/t/powershell-script-in-windows-host/35675 "2021-08-25T10:25:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sakshi\_Jain](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Sakshi\_Jain](https://forum.ansible.com/u/Sakshi_Jain)\
**Post date:** [August 25, 2021, 10:25am UTC](https://forum.ansible.com/t/powershell-script-in-windows-host/35675/1 "2021-08-25T10:25:23Z")

</div>

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](http://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
