Select 使用 powershell 搜索 return

Select search return with powershell

我正在使用 powershell 构建一个多步骤本地搜索引擎,它还允许您通过电子邮件发送 selected 信息。

我已经搞定了搜索引擎部分和电子邮件部分,我只需要搞定 select 部分。

现在,您打开程序,它会提示您搜索所需内容。如果我输入查询 when,返回的是:

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         1/25/2017   3:29 PM    8357890 01 - Kiss Me When I'm Down.mp3
-a---         1/24/2017   2:15 PM    7189290 09 - When You Love Someone.mp3

现在的目标是 select 比方说 01 - Kiss Me When I'm Down.mp3,因为我随后会将其放入 $attachment 变量中,然后该变量会将歌曲作为附件发送.这可以实现吗?

为清楚起见编辑:

我已经尝试使用 Select-Object 来完成此操作,但我无法让它允许用户 select 他们想要的歌曲。这就是它的目标,允许用户 select 他们想要的输入。

这是一个不优雅的解决方案,它使用 Add-Member cmdlet 添加了 Index NoteProperty。作为示例,我使用了 Get-ChildItem 结果:

$Items = Get-ChildItem C:\
$Index = 1
$Count = $Items.Count
foreach ($Item in $Items) {
    $Item | Add-Member -MemberType NoteProperty -Name "Index" -Value $Index
    $Index++
}
$Items | Select-Object Index, Attributes, LastWriteTime, Name | Out-Host
$Input = Read-Host "Select an item by index (1 to $Count)"
$Selected = $Items[$Input - 1]
Write-Host "You have selected $Selected"

我意识到已经给出了一些很好的答案,但是 OP 的 post 让我开始考虑提取 MP3 的元数据...:[=​​11=]

function getMP3Details() {

    param ( [System.IO.FileInfo] $mp3file = $null )

    [System.__ComObject] $Local:objShell    = $null;
    [System.__ComObject] $Local:objFolder   = $null;
    [System.__ComObject] $Local:objFile     = $null;
    [HashTable]          $Local:objTags     = @{ 0 = 'Name'; 1 = 'Size'; 13 = 'Artists'; 14 = 'Album'; 15 = 'Year'; 16 = 'Genre'; 20 = 'Authors'; 21 = 'Title'; 28 = 'Bit Rate'; }
    [Int32]              $Local:intTagIndex = 0;
    [String]             $Local:strTagName  = '';
    [String]             $Local:strTagValue = '';
    [PSCustomObject]     $Local:objOutput   = $null;

    try {
        if ( $mp3file -ne $null ) {

            $objShell  = New-Object -ComObject Shell.Application;
            $objFolder = $objShell.NameSpace( $mp3file.DirectoryName );
            $objFile   = $objFolder.ParseName( $mp3file.Name );
            $objOutput = New-Object -TypeName PSCustomObject;

            foreach ( $intTagIndex in ($objTags.Keys | Sort-Object) ) {
                $strTagName  = $objTags[$intTagIndex];
                $strTagValue = $objFolder.GetDetailsOf( $objFile, $intTagIndex );
                Add-Member -InputObject $objOutput -MemberType NoteProperty -Name $strTagName -Value ( [String] ($strTagValue -replace '[^ -~]', '') );
            } #foreach

            Write-Output -InputObject $objOutput;

        } #if
    } #try
    catch [System.Exception] {
        # Do something.
        'Error';
    } #catch

return;
}

[String]           $Local:strFolder        = '<PATH TO ALBUM>';
[PSCustomObject[]] $Local:arrMP3Tracks     = @();
[PSCustomObject]   $Local:objSelectedTrack = $null;

try {
    Get-ChildItem -LiteralPath $strFolder -File -Filter *.mp3 | Foreach-Object {
        $arrMP3Tracks += getMP3Details -mp3file $_;
    } #Foreach-Object
$objSelectedTrack = $arrMP3Tracks | Out-GridView -PassThru;
} #try
catch [System.Exception] {
    # Do something.
    'Error';
} #catch

exit 0;