Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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
Powershell 在递归文件搜索期间排除文件夹_Powershell_Powershell 3.0_Powershell Ise - Fatal编程技术网

Powershell 在递归文件搜索期间排除文件夹

Powershell 在递归文件搜索期间排除文件夹,powershell,powershell-3.0,powershell-ise,Powershell,Powershell 3.0,Powershell Ise,几个小时以来,我一直在努力让powershell执行以下操作: 使用基本目录C:\Users并递归浏览所有文件夹,不包括每个用户目录的AppData文件夹,也不包括名为: $exclude = @('Administrator', 'All Users', 'Default', 'Default User', 'Public', 'TEMP') 然后,输出应列出具有特定扩展名的所有文件 更新 如何将这些附加语句添加到输出中 Get-Childitem $Path -Include $exte

几个小时以来,我一直在努力让powershell执行以下操作:

使用基本目录C:\Users并递归浏览所有文件夹,不包括每个用户目录的AppData文件夹,也不包括名为:

$exclude = @('Administrator', 'All Users', 'Default', 'Default User', 'Public', 'TEMP')
然后,输出应列出具有特定扩展名的所有文件

更新

如何将这些附加语句添加到输出中

 Get-Childitem $Path -Include $extensions -Recurse -Force  -ErrorAction SilentlyContinue |
  Select Name,Directory,@{Name="Owner";Expression={(Get-ACL $_.Fullname).Owner}},CreationTime,LastAccessTime,  Length
2。更新

为了与网络的远程主机匹配,设置模式的正确方法是什么

$exclude_pattern = "\\hostname\\c$\\Users\\(" + ($exclude -join '|') + ")";
如果我用计算机名替换主机名,则不匹配。与本地模式相比,我看不出哪条线是错误的:

C:\\Users\\(Administrator|All Users|Default|Default User|Public|TEMP|Saargummi|dvop|leasingadmin|cenit|cadop|[^\\]+\\AppData)
\\hostname\\c$\\Users\\(Administrator|All Users|Default|Default User|Public|TEMP|Saargummi|dvop|leasingadmin|cenit|cadop|[^\\]+\\AppData) 
3。更新

是否有一种聪明的方法来包含以大写字母编写的扩展? 或者我需要写“.wav”和“.wav”吗

$exclude = @("Administrator",
             "All Users",
             "Default",
             "Default User",
             "Public",
             "TEMP",
             "[^\\]+\\AppData")

$extensions = "*.ini" # replace this with your extension
$hostname = "hostname" # replace this with your hostname

$exclude_pattern = "\\\\{0}\\C[$]\\Users\\({1})" `
    -f $hostname, ($exclude -join '|')
$path = "\\{0}\C$\Users" -f $hostname

Get-ChildItem -Path $path -Force -Recurse -Include $extensions `
    -ErrorAction "SilentlyContinue" `
    | Where-Object { $_.PSIsContainer -eq $false } `
    | Where-Object { $_.FullName -notmatch $exclude_pattern } `
    | ForEach-Object {
        $_ | Select-Object -Property @(
            "Name",
            "Directory",
            @{Label="Owner";Expression={(Get-ACL $_.Fullname).Owner}},                      
            "CreationTime",
            "LastAccessTime",
            "Length"
        )
    }
[更新1]:根据更新的问题添加了输出格式。通过在一个正则表达式模式中合并所有排除项来缩短

[更新2]:更改为UNC路径。请注意,所有者可能显示为SID。查找SID到用户名的转换


[更新3]:PowerShell默认不区分大小写,因此您无需多次添加相同的扩展。

感谢您提供的脚本。我得到一个错误:对C:\Users\TEMP的访问被拒绝。知道为什么不排除在外吗?很奇怪<代码>-错误操作“SilentlyContinue”应抑制拒绝访问和其他非中断错误。极好。非常感谢您的更新。使用管理员权限运行ISE使错误消失。Alexander你能看看我的第二次更新吗。我无法获得正确的路径以匹配远程主机C$共享。非常感谢。