【问题标题】:Searching with Get-ChildItem over the network通过网络使用 Get-ChildItem 搜索
【发布时间】:2023-03-17 13:52:01
【问题描述】:

我在通过网络使用 PowerShell 进行搜索时遇到问题;程序在执行Get-ChildItem时卡住了。

# creating search string
$date = "*2018-01-10*"
$format = ".avi"
$toSearch = $date + $format
echo $toSearch

# Verifying A: drive to be disconnected
net use /d A: /y

# connecting Network drive to local drive A:
if (Test-Connection -ComputerName PROC-033-SV -Quiet) {net use A: \\PROC-033-SV\c$}

# getting list of directories to search on
$userList = Get-ChildItem -Path A:\users\*.* -Directory

# verifying list of directories prior to search
echo $userList

# searching through Network, on A:\Users\ subdirectories for $toSearch variable
Get-ChildItem -Path $userList -Include $toSearch -Recurse -Force

# *** HERE's where the program get stuck, it nevers stop searching
# it nevers reach pause

pause

有谁知道为什么Get-ChildItem 一直在循环并且永远不会停止? 我正在使用 PS v4 没有 -Depth 选项可用于 -Recurse 参数;我怀疑这可能是问题所在。

【问题讨论】:

  • 你读过错误吗?这在我看来是预期的。
  • 实际上正在工作,但需要很长时间......我想我需要更新到 V5 以使用 -Depth 以使其更快。这些文件是 5 个深度级别; \Users\AppData\Temp\FolderTarget

标签: powershell scripting powershell-4.0


【解决方案1】:

如果您想在 PowerShell v4 及更早版本中限制递归深度,您可以将 Get-ChildItem 包装在自定义函数中,例如像这样:

function Get-ChildItemRecursive {
    [CmdletBinding()]
    Param(
        [Parameter(
            Mandatory=$false,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true
        )]
        [string[]]$Path = $PWD.Path,

        [Paramter(Mandatory=$false)]
        [string]$Filter = '*.*',

        [Parameter(Mandatory=$false)]
        [int]$Depth = 0
    )

    Process {
        Get-ChildItem -Path $Path -Filter $Filter
        if ($Depth -gt 0) {
            Get-ChildItem -Path $Path |
                Where-Object { $_.PSIsContainer } |
                Get-ChildItemRecursive -Filter $Filter -Depth ($Depth - 1)
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2011-10-26
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多