【问题标题】:How can I use a PowerShell output in the next command?如何在下一个命令中使用 PowerShell 输出?
【发布时间】:2022-08-14 14:38:06
【问题描述】:

我正在使用 PowerShell 命令来跨驱动器搜索特定文件。我是 PowerShell 的新手,所以我已经拥有的大部分只是我在网上找到的东西。目前我有这个:

$ExclDrives = (\'C\')
>> Get-PSDrive -PSProvider FileSystem | Where-Object {$_.Name -notin $ExclDrives} `
>> | % {write-host -f Green \"Searching \" $_.Root;get-childitem $_.Root -include *MyFile.txt -r `
>> | sort-object Length -descending}

哪个输出:

Searching  D:\\
Searching  E:\\
Searching  F:\\


    Directory: F:\\MyDirectory


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         8/13/2022  12:03 AM              0 MyFile.txt


PS C:\\Windows\\system32>

我想知道如何获取输出中列出的目录并在以下命令中使用它,例如:

cd F:\\MyDirectory

如果这可以通过管道或其他方式实现,我将非常感谢您的回答:)

谢谢阅读

    标签: powershell


    【解决方案1】:

    如果找到多个文件,我真的不确定处理此问题的最佳方法是什么。在脚本运行时,我们无法将目录更改为父文件夹,也无法对所有返回的文件执行此操作,除非我们为每个文件打开新的 PowerShell 窗口。由于您似乎将搜索特定文件,我认为这些文件不会返回太多结果并且不知道您的最终目标,因此我为每个文件打开了一个新的文件资源管理器窗口,文件被突出显示/选择。

    $excludeDrives = ('C')
    Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Name -notin $excludeDrives } |
        ForEach-Object {
            Write-Host -f Green 'Searching ' $_.Root
            Get-ChildItem -Path $_.Root -Recurse -Include *MyFile.txt -ErrorAction SilentlyContinue |
                ForEach-Object {
                    # This line will open a file explorer window with the file highlighted
                    explorer.exe /select, $_
                    # This line will send the file object out through the pipeline
                    $_ 
                } | Sort-Object Length -Descending
            }
    

    要在下一个命令中回答有关如何访问文件目录的问题,您可以使用Foreach-Object$_.Directory

    Get-ChildItem -Path $_.Root -Recurse -Include *MyFile.txt -ErrorAction SilentlyContinue |
        Sort-Object Length -Descending |
            ForEach-Object {
                # Using the pipeline we can pass object along and access them
                # using a special automatic variable called $_
                # a property exists on FileInfo objects called Directory
                'The directory is ' + $_.Directory
            }
    

    更新

    希望这将回答您评论中的问题

    $ExclDrives = ('C')
    Get-PSDrive -PSProvider FileSystem |
        Where-Object { $_.Name -in $ExclDrives } |
            ForEach-Object {
                Write-Host -f Green 'Searching ' $_.Root
                Get-ChildItem $_.Root -Include *MyFile.txt -Recurse -ErrorAction SilentlyContinue |
                    ForEach-Object {
                        # do whatever you want with the file.  Reference using $_
                        Write-Host "Found Filename: $($_.Name)`tDirectory: $($_.Directory)" -ForegroundColor Cyan
                        explorer.exe /select, $_
                        # output the fileinfo object, in this case 
                        # to the next command in the pipeline which is Sort-Object
                        $_  # this line
                    } |
                        Sort-Object Length -Descending
                    }
    

    【讨论】:

    • 谢谢,这解决了我的问题,但我还有一个问题。如何将原始命令的搜索所有驱动器(C 除外)的能力与您的命令(您给我的第二个命令)使用管道传递对象的能力结合起来?
    猜你喜欢
    • 1970-01-01
    • 2021-09-11
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    相关资源
    最近更新 更多