0

I'm running a playbook against a Windows VM to delete the contents of the recycle bin and the task runs successfully signifying that a change has been made. However, when looking on the server itself, the contents of the recycle bin still remain.

There are several ways to delete the contents of the recycle bin. Here are the following methods I've tried:

Different Ansible playbooks used:

- name: Clean recycle bin
  win_shell: |
    $recycleBin = (New-Object -ComObject Shell.Application).NameSpace(0xa)
    $recycleBin.Items() | ForEach-Object -Process { Remove-Item -Path $_.Path -Force -Recurse }

- name: Clean recycle bin
  win_shell: Clear-RecycleBin -Force

- name: Clean recycle bin
  win_command: cmd.exe /k rd /s /q %systemdrive%\$Recycle.Bin

Using the powershell commands, it will delete everything without prompting me to confirm. I would prefer not to run the cmd.exe command, since it only deletes the contents that pertain to the C:\ drive, unless I specify the drive letter.

All these commands successfully delete the contents of the recycle bin when run on the server itself but when using Ansible, the contents of the recycle bin remain.

mklement0
  • 382,024
  • 64
  • 607
  • 775
fancybard
  • 13
  • 5

1 Answers1

0

So I realized that running the powershell commands only deletes the contents of the recycle bin for that specific user, not all users.

I decided to collect the drive letters using a powershell command since I didn't get all the drive letters using win_disks_facts. Then I would loop through the drive letters and run the cmd.exe command which cleans up the recycle bin for all users.

- name: Get disk drives
  win_shell: |
    $driveinfo = Get-WmiObject -Class Win32_logicaldisk | where {$_.drivetype -eq 3} | select DeviceID
    $driveinfo.DeviceID
- name: Clean recycle bin
  win_command: cmd.exe /k rd /s /q {{ item }}\$Recycle.Bin
  with_items:
    - "{{ disk_drives.stdout_lines }}"
fancybard
  • 13
  • 5
  • Curious why you don't just delete the contents within PowerShell in the `win_shell` snippet and avoid another round trip? – briantist Mar 26 '19 at 02:26
  • I did it for the sake of testing to see how I could get the disk drives but I went ahead and combined the two afterwards – fancybard Mar 26 '19 at 23:27
  • Can you share the final solution? I am having trouble combining the two. I am also fairly new to ansible. – user3487244 Aug 28 '20 at 15:37
  • a small error,should add register: disk_drives at the end of 'Get disk drives' task – HobbyMarks Nov 18 '22 at 12:28