【发布时间】:2020-10-10 07:29:05
【问题描述】:
Powershell 是否必须始终遍历子文件夹并首先删除它们?它不仅可以删除顶级目录,还会删除其中的所有内容吗?我问的原因是我写了一个脚本来删除超过 120 天的文件夹并将这些文件夹输出到日志文件。
get-childitem -directory h:\agencydata\* |
where { (get-date) - $_.lastwritetime -gt 120. } |
remove-item -force -recurse -Verbose 4>&1 | Add-Content H:\logs\$(Get-Date -Format dd-MM-yyyy)-auto_deletes.log
但日志也包含数百个子目录。如果我们不能强制 Powershell 只删除顶级文件夹,那么我们可以强制它在日志中只包含顶级文件夹吗?
我目前通过脚本获得的日志输出示例:
Performing the operation "Remove Directory" on target "H:\agencydata\255233".
Performing the operation "Remove Directory" on target "H:\agencydata\255233\_Extracted".
Performing the operation "Remove Directory" on target "H:\agencydata\255233\_Extracted\Broker-7671-02242020080758-49".
Performing the operation "Remove Directory" on target "H:\agencydata\255233\_Extracted\Broker-7671-02242020080758-49\7671".
Performing the operation "Remove Directory" on target "H:\agencydata\255233\_Extracted\Broker-7671-02242020080758-49\7671\Accounts".
Performing the operation "Remove Directory" on target "H:\agencydata\255233\_Extracted\Broker-7671-02242020080758-49\7671\Accounts\Professional Contract Services Inc. (PCSI) - 1055312".
Performing the operation "Remove Directory" on target "H:\agencydata\255233\_Extracted\Broker-7671-02242020080758-49\7671\Accounts\Professional Contract Services Inc. (PCSI) - 1055312\2004 - 2016".
Performing the operation "Remove Directory" on target "H:\agencydata\255233\_Extracted\Broker-7671-02242020080758-49\7671\Accounts\Professional Contract Services Inc. (PCSI) - 1055312\2004 - 2016\2011".
Performing the operation "Remove Directory" on target "H:\agencydata\255233\_Extracted\Broker-7671-02242020080758-49\7671\Accounts\Professional Contract Services Inc. (PCSI) - 1055312\2004 - 2016\2011\MIP".
Performing the operation "Remove Directory" on target "H:\agencydata\255233\_Extracted\Broker-7671-02242020080758-49\7671\Accounts\Professional Contract Services Inc. (PCSI) - 1055312\2004 - 2016\2011\MIP\Quotes".
Performing the operation "Remove Directory" on target "H:\agencydata\255233\_Extracted\Broker-7671-02242020080758-49\7671\Accounts\Professional Contract Services Inc. (PCSI) - 1055312\2004 - 2016\2011\MIP\Quotes\Medical Quotes".
Performing the operation "Remove Directory" on target "H:\agencydata\255233\_Extracted\Broker-7671-02242020080758-49\7671\Accounts\Professional Contract Services Inc. (PCSI) - 1055312\2004 - 2016\2011\MIP\Quotes\Medical Quotes\Fully Insured Quotes".
Performing the operation "Remove Directory" on target "H:\agencydata\255233\_Extracted\Broker-7671-02242020080758-49\7671\Accounts\Professional Contract Services Inc. (PCSI) - 1055312\2004 - 2016\2011\MIP\Quotes\Medical Quotes\Fully Insured Quotes\2011-03-01 Trustmark medical quote".
Performing the operation "Remove Directory" on target "H:\agencydata\255233\_Extracted\Broker-7671-02242020080758-49\7671\Accounts\Professional Contract Services Inc. (PCSI) - 1055312\2004 - 2016\2011\MIP\Quotes\Medical Quotes\Fully Insured Quotes\2011-03-01 Trustmark medical quote\2011-03-01 geo access reports".
我想要的示例:
Performing the operation "Remove Directory" on target "H:\agencydata\255233".
Performing the operation "Remove Directory" on target "H:\agencydata\25523323".
Performing the operation "Remove Directory" on target "H:\agencydata\25523343534".
Performing the operation "Remove Directory" on target "H:\agencydata\2552331".
Performing the operation "Remove Directory" on target "H:\agencydata\25523355".
Performing the operation "Remove Directory" on target "H:\agencydata\25523314".
【问题讨论】:
标签: powershell scripting