有没有办法在Powershell中预先填写Read-Host?

时间:2016-11-30 20:56:01

标签: powershell

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

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的新手,所以如果有任何其他意见,欢迎他们!

2 个答案:

答案 0 :(得分: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."}
    }

答案 1 :(得分:0)

来自Super User

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