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".
答案 0 :(得分:3)
Recurse表示它的作用,请重复调用可到达对象的命令,否则,为什么要指定它。
如果仅传递父级名称而不进行递归,则会删除父级和所有子级。
在没有-Recurse ...的情况下执行此操作
Remove-Item -Path C:\ParentFolder -Force -WhatIf
... PowerShell会警告您。
'C:\ ParentFolder上的项目具有子代和Recurse参数 未指定。如果继续,所有子项都将被删除 该项目。您确定要继续吗?
如果您真的想查看命令/代码的堆栈,请执行以下操作:
Trace-Command -Name metadata,parameterbinding,cmdlet -Expression {Remove-Item -Path C:\ParentFolder -Force} -PSHost -Verbose
# Results
<#
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Remove-Item]
DEBUG: ParameterBinding Information: 0 : BIND arg [C:\ParentFolder] to parameter [Path]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.String[]]
DEBUG: ParameterBinding Information: 0 : Trying to convert argument value from System.String to System.String[]
DEBUG: ParameterBinding Information: 0 : ENCODING arg into collection
DEBUG: ParameterBinding Information: 0 : Binding collection parameter Path: argument type [String], parameter type [System.String[]], collection type Array, element type [System.String], coerceElementType
DEBUG: ParameterBinding Information: 0 : Creating array with element type [System.String] and 1 elements
DEBUG: ParameterBinding Information: 0 : Argument type String is not IList, treating this as scalar
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.String]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : Adding scalar element of type String to array position 0
DEBUG: ParameterBinding Information: 0 : BIND arg [System.String[]] to param [Path] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND arg [True] to parameter [Force]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Management.Automation.SwitchParameter]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [True] to param [Force] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Remove-Item]
DEBUG: ParameterBinding Information: 0 : BIND cmd line args to DYNAMIC parameters.
DEBUG: ParameterBinding Information: 0 : DYNAMIC parameter object: [Microsoft.PowerShell.Commands.FileSystemProviderRemoveItemDynamicParameters]
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Remove-Item]
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing
#>
Trace-Command -Name metadata,parameterbinding,cmdlet -Expression {Remove-Item -Path C:\ParentFolder -Recurse -Force } -PSHost -Verbose
# Results
<#
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Remove-Item]
DEBUG: ParameterBinding Information: 0 : BIND arg [C:\ParentFolder] to parameter [Path]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.String[]]
DEBUG: ParameterBinding Information: 0 : Trying to convert argument value from System.String to System.String[]
DEBUG: ParameterBinding Information: 0 : ENCODING arg into collection
DEBUG: ParameterBinding Information: 0 : Binding collection parameter Path: argument type [String], parameter type [System.String[]], collection type Array, element type [System.String], coerceElementType
DEBUG: ParameterBinding Information: 0 : Creating array with element type [System.String] and 1 elements
DEBUG: ParameterBinding Information: 0 : Argument type String is not IList, treating this as scalar
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.String]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : Adding scalar element of type String to array position 0
DEBUG: ParameterBinding Information: 0 : BIND arg [System.String[]] to param [Path] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND arg [True] to parameter [Recurse]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Management.Automation.SwitchParameter]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [True] to param [Recurse] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND arg [True] to parameter [Force]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Management.Automation.SwitchParameter]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [True] to param [Force] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Remove-Item]
DEBUG: ParameterBinding Information: 0 : BIND cmd line args to DYNAMIC parameters.
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Remove-Item]
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing
#>
尽管如此,PowerShell仍然无法在任何操作系统上使用任何命令删除非空文件夹。因此,即使未指定递归,也将意味着破坏性命令。
如果只想在日志文件中看到父母,则必须为此编写代码。
更新 至于你的评论
您能指导我如何编辑日志输出以仅显示那些顶级文件夹吗?
...您可以执行以下操作。记录读取结果,而不是删除结果。
Get-ChildItem -directory 'C:\ParentFolder*' |
ForEach{
"Performing the operation on target $($PSItem.FullName)" 4>&1 |
Add-Content -Path "c:\logs\$(Get-Date -Format dd-MM-yyyy)-auto_deletes.log"
$null = Remove-Item -Path $PSItem -Force
}
Get-Content -Path 'C:\logs\09-10-2020-auto_deletes.log'
# Results
<#
Performing the operation on target C:\ParentFolder
Performing the operation on target C:\ParentFolder - Copy
Performing the operation on target C:\ParentFolder - Copy - Copy
Performing the operation on target C:\ParentFolder - Copy - Copy - Copy
#>