PowerShell Get-ChildItem -Exclude和-Include not working

时间:2016-07-19 17:00:18

标签: powershell get-childitem

有人能告诉我这里发生了什么吗? 我可以让-Filter * .log工作没问题。

PS C:\logs> Get-ChildItem -Filter *.log -Recurse


    Directory: C:\logs


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         7/19/2016   2:45 PM          0 Moninitor-error-1.log
-a---         7/19/2016   2:45 PM          0 Moninitor-out-1.log
-a---         7/19/2016   2:45 PM          0 Watcher-error-3.log
-a---         7/19/2016   2:52 PM     264810 Watcher-out-3.log
-a---         7/19/2016   2:48 PM          0 FolderWatcher-error-2.log
-a---         7/19/2016   2:52 PM    7768537 FolderWatcher-out-2.log
-a---         7/19/2016   4:34 PM          0 nothing-error.log
-a---         7/19/2016   4:34 PM          0 nothing.log
-a---         7/18/2016   2:38 PM          0 log-error-0.log
-a---         7/19/2016   2:45 PM          0 log-out-0.log

一旦我尝试使用-Include或-Exclude,我就会收到访问错误。

PS C:\logs> Get-ChildItem -Exclude *.log -Recurse
Get-ChildItem : Access is denied
At line:1 char:1
+ Get-ChildItem -Exclude *.log -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetChildItemCommand

PS C:\logs> Get-ChildItem -Include *.log -Recurse
Get-ChildItem : Access is denied
At line:1 char:1
+ Get-ChildItem -Include *.log -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\...tor-error-1.log:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : GetItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access is denied
At line:1 char:1
+ Get-ChildItem -Include *.log -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetChildItemCommand

我想要做的是循环浏览此目录中的文件并删除任何超过0天的文件,并且不要在文件名中包含错误。

我目前的剧本就是这个。

#----- define parameters -----#
#----- get current date ----#
$Now = Get-Date
#----- define amount of days ----#
$Days = "0"
#----- define folder where files are located ----#
$TargetFolder = "C:\logs"
#----- define extension ----#
$Extension = "*.log"
$Skip = "*error*"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)
#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-ChildItem -Path $TargetFolder -Filter $Extension -Exclude $Skip -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files) 
    {
    if ($File -ne $NULL)
        {
        write-host "Deleting File $File" -ForegroundColor "DarkRed"
        Remove-Item $File.FullName | out-null
        }
    else
        {
        Write-Host "No more files to delete!" -foregroundcolor "Green"
        }
    }

但我随时起诉排除或包含它给了我这些访问错误。

- 完美地收集自己的作品......

PS C:\Users\Administrator\.pm2\logs> Get-ChildItem -Recurse


    Directory: C:\logs


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         7/19/2016   2:45 PM          0 Moninitor-error-1.log
-a---         7/19/2016   2:45 PM          0 Moninitor-out-1.log
-a---         7/19/2016   2:45 PM          0 Watcher-error-3.log
-a---         7/19/2016   2:52 PM     264810 Watcher-out-3.log
-a---         7/19/2016   2:48 PM          0 FolderWatcher-error-2.log
-a---         7/19/2016   2:52 PM    7768537 FolderWatcher-out-2.log
-a---         7/19/2016   4:34 PM          0 nothing-error.log
-a---         7/19/2016   4:34 PM          0 nothing.log
-a---         7/18/2016   2:38 PM          0 log-error-0.log
-a---         7/19/2016   2:45 PM          0 log-out-0.log

2 个答案:

答案 0 :(得分:0)

试试运气

$Extension = "*log"
    $TargetFolder = "c:\logs"
    Get-Childitem $TargetFolder -Include $Extension -Recurse 

$Extension = "*txt"
    $TargetFolder = "c:\logs"
    Get-Childitem $TargetFolder -exclude $Extension -Recurse 

答案 1 :(得分:0)

老实说,你想使用-Filter,因为@Guvante指出,它是由文件系统提供商应用的,并且会大大加快速度。之后只需输入Where语句即可删除名称中包含错误的内容,但这些内容还不够大。

Get-ChildItem -Filter *.log -Recurse | Where{$_.name -notlike "*error*" -and $_.lastwritetime -lt [datetime]::Today}

如果您想要创建文件而不是上次修改文件,请替换lastwritetime creationtime