有没有一种方法可以在Powershell中预填充Read-Host?

4

我有一个脚本,可以帮助用户查找文件哈希是否存在于文件夹中。用户输入哈希后,我会确定它是什么类型的哈希,如果不支持或者用户错过了一个字母,它将返回要求重新输入哈希。为了方便使用,我希望能够预先填写出用户之前输入的内容,这样他们就不需要重新开始。

while (1)
{
    $hashToFind = Read-Host -Prompt "Enter hash to find or type 'file' for multiple hashes"
    # Check if user wants to use text file
    if ($hashToFind -eq "file" )
    {

        Write-Host "Be aware program will only support one has type at a time. Type is determined by the first hash in the file." -ForegroundColor Yellow
        Start-Sleep -Seconds 3
        $hashPath = New-Object system.windows.forms.openfiledialog
        $hashPath.InitialDirectory = “c:\”
        $hashPath.MultiSelect = $false
        if($hashPath.showdialog() -ne "OK")
        {
            echo "No file was selected. Exiting program."
            Return
        }
        $hashToFind = Get-Content $hashPath.filename
    }

    # Changes string to array
    if ( $hashToFind.GetTypeCode() -eq "String")
    {
        $hashToFind+= " a"
        $hashToFind = $hashToFind.Split(" ")
    }

    if ($hashToFind[0].Length -eq 40){$hashType = "SHA1"; break}
    elseif ($hashToFind[0].Length -eq 64){$hashType = "SHA256"; break}
    elseif ($hashToFind[0].Length -eq 96){$hashType = "SHA384"; break}
    elseif ($hashToFind[0].Length -eq 128){$hashType = "SHA512"; break}
    elseif ($hashToFind[0].Length -eq 32){$hashType = "MD5"; break}
    else {echo "Hash length is not of supported hash type."}
}

我对PowerShell比较新,如果有其他意见欢迎提出!


据我所知,没有办法预填充read-host对话框,但是在提示中输入的数据会保存在命令历史记录中,因此用户只需要按“向上”箭头键即可返回先前键入的哈希值,并能够编辑他们需要的任何内容。然而,通过查看您的脚本,您可能最好使用参数验证而不是这个while循环。请参考:https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/15/simplify-your-powershell-script-with-parameter-validation/ - Mike Garuccio
据我所知,无法预先填充。 - 4c74356b41
2个回答

3

来自于超级用户

[System.Windows.Forms.SendKeys]::SendWait("yes")
Read-Host "Your answer"

0

我想出了这样的解决方案:

while (1)
    {
        $hashToFind = Read-Host -Prompt "Enter hash to find or type 'file' for multiple hashes. Enter 'R' for reply input"

        if ($hashToFind -eq 'R' -and $PreviousInput)
        {
            $handle = (Get-Process -Id $PID).MainWindowHandle

            $code = {
            param($handle,$PreviousInput)
            Add-Type @"
      using System;
      using System.Runtime.InteropServices;
      public class Tricks {
         [DllImport("user32.dll")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool SetForegroundWindow(IntPtr hWnd);
      }
    "@
            [void][Tricks]::SetForegroundWindow($handle)
            Add-Type -AssemblyName System.Windows.Forms
            [System.Windows.Forms.SendKeys]::SendWait($PreviousInput)
            }

            $ps = [PowerShell]::Create()
            [void]$ps.AddScript($code).AddArgument($handle).AddArgument($PreviousInput)
            [void]$ps.BeginInvoke()
        }

        $PreviousInput = $hashToFind

        # Check if user wants to use text file
        if ($hashToFind -eq "file" )
        {
            $PreviousInput = $null

            Write-Host "Be aware program will only support one has type at a time. Type is determined by the first hash in the file." -ForegroundColor Yellow
            Start-Sleep -Seconds 3
            $hashPath = New-Object system.windows.forms.openfiledialog
            $hashPath.InitialDirectory = “c:\”
            $hashPath.MultiSelect = $false
            if($hashPath.showdialog() -ne "OK")
            {
                echo "No file was selected. Exiting program."
                Return
            }
            $hashToFind = Get-Content $hashPath.filename
        }

        # Changes string to array
        if ( $hashToFind.GetTypeCode() -eq "String")
        {
            $hashToFind+= " a"
            $hashToFind = $hashToFind.Split(" ")
        }

        if ($hashToFind[0].Length -eq 40){$hashType = "SHA1"; break}
        elseif ($hashToFind[0].Length -eq 64){$hashType = "SHA256"; break}
        elseif ($hashToFind[0].Length -eq 96){$hashType = "SHA384"; break}
        elseif ($hashToFind[0].Length -eq 128){$hashType = "SHA512"; break}
        elseif ($hashToFind[0].Length -eq 32){$hashType = "MD5"; break}
        else {echo "Hash length is not of supported hash type."}
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接