简体   繁体   English

如何使用 Ansible 删除回收站中的内容

[英]How to delete contents in Recycle Bin using Ansible

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.我正在针对 Windows VM 运行剧本以删除回收站的内容,并且任务成功运行,表明已进行更改。 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:使用了不同的 Ansible 剧本:

- 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.使用 powershell 命令,它将删除所有内容而不提示我确认。 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.我不想运行cmd.exe命令,因为它只会删除与 C:\\ 驱动器相关的内容,除非我指定驱动器号。

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.所有这些命令在服务器本身上运行时都成功删除了回收站的内容,但在使用 Ansible 时,回收站的内容仍然存在。

So I realized that running the powershell commands only deletes the contents of the recycle bin for that specific user, not all users.所以我意识到运行 powershell 命令只会删除该特定用户的回收站中的内容,而不是所有用户。

I decided to collect the drive letters using a powershell command since I didn't get all the drive letters using win_disks_facts.我决定使用 powershell 命令收集驱动器号,因为我没有使用 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.然后我将遍历驱动器号并运行cmd.exe命令,该命令为所有用户清理回收站。

- 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 }}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM