Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 如何在调用Remove Item之前排除特定文件_File_Powershell_Powershell 4.0 - Fatal编程技术网

File 如何在调用Remove Item之前排除特定文件

File 如何在调用Remove Item之前排除特定文件,file,powershell,powershell-4.0,File,Powershell,Powershell 4.0,我遇到的问题是在脚本中插入-Exclude命令,以帮助避免使用“.pst”或任何其他指定的文件类型。我刚刚确定了如何在Where Object字段中包含$exclude $limit = (Get-Date).AddDays(2555) $path = "\\File Path" $log = "C:\Log output" $exclude = ".pst" # Delete files older than the $limit. <Use -WhatIf when you want

我遇到的问题是在脚本中插入
-Exclude
命令,以帮助避免使用“.pst”或任何其他指定的文件类型。我刚刚确定了如何在
Where Object
字段中包含
$exclude

$limit = (Get-Date).AddDays(2555)
$path = "\\File Path"
$log = "C:\Log output"
$exclude = ".pst"

# Delete files older than the $limit. <Use -WhatIf when you want to see what files/folders will be deleted before>

Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit} >$log

Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } >> $log

Get-ChildItem -Path $path  -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit}| Remove-Item -Force -WhatIf

Get-ChildItem -Path $path  -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force -WhatIf 

# Delete any empty directories left behind after deleting the old files. <Use -WhatIf when you want to see what files/folders will be deleted before>

Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null }  >> $log

Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse -WhatIf 
$limit=(获取日期).AddDays(2555)
$path=“\\File path”
$log=“C:\log输出”
$exclude=“.pst”
#删除早于$limit的文件。
获取ChildItem-Path$Path-Recurse-Force | Where对象{!$\u.PSIsContainer-和$\u.LastWriteTime-lt$limit}>$log
获取ChildItem-Path$Path-Recurse-Force | Where对象{!$.PSIsContainer-和$\创建时间-lt$limit}>$log
Get ChildItem-Path$Path-Recurse-Force | Where对象{!$\u.PSIsContainer-和$\u.LastWriteTime-lt$limit}| Remove Item-Force-WhatIf
获取ChildItem-Path$Path-Recurse-Force | Where对象{!$.PSIsContainer-和$\.CreationTime-lt$limit}|删除项-Force-WhatIf
#删除旧文件后留下的所有空目录。
Get-ChildItem-Path$Path-Recurse-Force | Where对象{$$\ PSIsContainer-和(Get-ChildItem-Path$\ FullName-Recurse-Force | Where对象{!$\ PSIsContainer})-eq$null}>$log
Get-ChildItem-Path$Path-Recurse-Force | Where对象{$$\ PSIsContainer-和(Get-ChildItem-Path$\ FullName-Recurse-Force | Where对象{!$\ PSIsContainer})-eq$null}删除项-Force-Recurse-WhatIf

非常感谢您的任何想法。

要回答您的特定问题,您可以在查看文件扩展名的
Where对象中添加另一个子句。注意,这是有效的,因为您只有一个扩展名。如果要添加更多,则需要更改运算符

Get-ChildItem -Path $path -Recurse -Force | 
    Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit -and $_.Extension -ne $exclude } > $log
但是,您应该在代码中查看更好的选项。让windows文件系统完成大部分工作,而不是使用
Where Object
进行后期处理,可以节省时间和复杂性。您甚至可以根据需要组合前几行。由于您有v4,因此可以使用
-File
-Directory
开关仅拉取相应的项目

Get-ChildItem -Path $path -Recurse -Force -File | 
    Where-Object {$_.LastWriteTime -lt $limit -and $_.CreationTime -lt $limit} | 
    Add-Content $log
虽然不完全是你的前几行在做什么,但我认为它做了你想做的。注意
-File
开关和组合日期子句

如果您想记录您正在删除的内容,还可以使用删除一些重复内容(这不是解决此问题的唯一方法)

我不知道您在哪里需要它,但您也可以使用
Get ChildItem
-Exclude
忽略pst文件

Get-ChildItem -Path $_.FullName -Exclude $exclude -Recurse -Force

要回答您的特定问题,您可以在查看文件扩展名的
Where对象中添加另一个子句。注意,这是有效的,因为您只有一个扩展名。如果要添加更多,则需要更改运算符

Get-ChildItem -Path $path -Recurse -Force | 
    Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit -and $_.Extension -ne $exclude } > $log
但是,您应该在代码中查看更好的选项。让windows文件系统完成大部分工作,而不是使用
Where Object
进行后期处理,可以节省时间和复杂性。您甚至可以根据需要组合前几行。由于您有v4,因此可以使用
-File
-Directory
开关仅拉取相应的项目

Get-ChildItem -Path $path -Recurse -Force -File | 
    Where-Object {$_.LastWriteTime -lt $limit -and $_.CreationTime -lt $limit} | 
    Add-Content $log
虽然不完全是你的前几行在做什么,但我认为它做了你想做的。注意
-File
开关和组合日期子句

如果您想记录您正在删除的内容,还可以使用删除一些重复内容(这不是解决此问题的唯一方法)

我不知道您在哪里需要它,但您也可以使用
Get ChildItem
-Exclude
忽略pst文件

Get-ChildItem -Path $_.FullName -Exclude $exclude -Recurse -Force

如果需要,这可以很容易地合并。您有什么PowerShell版本?首先,您应该看看
Tee对象
。为什么要等到
Where Object
?为什么不将
-Exclude
Get ChildItem
一起使用呢。您的最后一行是删除空文件夹吗?我使用的是PS4.0版。如果需要,可以很容易地合并。您有什么PowerShell版本?首先,您应该看看
Tee对象
。为什么要等到
Where Object
?为什么不将
-Exclude
Get ChildItem
一起使用呢。您的最后一行是删除空文件夹吗?我使用的是PS4.0版。这非常有用。我正在考虑以任何可能的方式使用exclude文件类型,这非常有用。我正在考虑以任何可能的方式使用exclude文件类型。