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

Mat*_*008 5 powershell

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

\n\n
while (1)\n{\n    $hashToFind = Read-Host -Prompt "Enter hash to find or type \'file\' for multiple hashes"\n    # Check if user wants to use text file\n    if ($hashToFind -eq "file" )\n    {\n\n        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\n        Start-Sleep -Seconds 3\n        $hashPath = New-Object system.windows.forms.openfiledialog\n        $hashPath.InitialDirectory = \xe2\x80\x9cc:\\\xe2\x80\x9d\n        $hashPath.MultiSelect = $false\n        if($hashPath.showdialog() -ne "OK")\n        {\n            echo "No file was selected. Exiting program."\n            Return\n        }\n        $hashToFind = Get-Content $hashPath.filename\n    }\n\n    # Changes string to array\n    if ( $hashToFind.GetTypeCode() -eq "String")\n    {\n        $hashToFind+= " a"\n        $hashToFind = $hashToFind.Split(" ")\n    }\n\n    if ($hashToFind[0].Length -eq 40){$hashType = "SHA1"; break}\n    elseif ($hashToFind[0].Length -eq 64){$hashType = "SHA256"; break}\n    elseif ($hashToFind[0].Length -eq 96){$hashType = "SHA384"; break}\n    elseif ($hashToFind[0].Length -eq 128){$hashType = "SHA512"; break}\n    elseif ($hashToFind[0].Length -eq 32){$hashType = "MD5"; break}\n    else {echo "Hash length is not of supported hash type."}\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我是 PowerShell 的新手,所以如果有任何其他评论,欢迎提出!

\n

Kir*_*kov 0

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

\n\n
while (1)\n    {\n        $hashToFind = Read-Host -Prompt "Enter hash to find or type \'file\' for multiple hashes. Enter \'R\' for reply input"\n\n        if ($hashToFind -eq \'R\' -and $PreviousInput)\n        {\n            $handle = (Get-Process -Id $PID).MainWindowHandle\n\n            $code = {\n            param($handle,$PreviousInput)\n            Add-Type @"\n      using System;\n      using System.Runtime.InteropServices;\n      public class Tricks {\n         [DllImport("user32.dll")]\n         [return: MarshalAs(UnmanagedType.Bool)]\n         public static extern bool SetForegroundWindow(IntPtr hWnd);\n      }\n    "@\n            [void][Tricks]::SetForegroundWindow($handle)\n            Add-Type -AssemblyName System.Windows.Forms\n            [System.Windows.Forms.SendKeys]::SendWait($PreviousInput)\n            }\n\n            $ps = [PowerShell]::Create()\n            [void]$ps.AddScript($code).AddArgument($handle).AddArgument($PreviousInput)\n            [void]$ps.BeginInvoke()\n        }\n\n        $PreviousInput = $hashToFind\n\n        # Check if user wants to use text file\n        if ($hashToFind -eq "file" )\n        {\n            $PreviousInput = $null\n\n            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\n            Start-Sleep -Seconds 3\n            $hashPath = New-Object system.windows.forms.openfiledialog\n            $hashPath.InitialDirectory = \xe2\x80\x9cc:\\\xe2\x80\x9d\n            $hashPath.MultiSelect = $false\n            if($hashPath.showdialog() -ne "OK")\n            {\n                echo "No file was selected. Exiting program."\n                Return\n            }\n            $hashToFind = Get-Content $hashPath.filename\n        }\n\n        # Changes string to array\n        if ( $hashToFind.GetTypeCode() -eq "String")\n        {\n            $hashToFind+= " a"\n            $hashToFind = $hashToFind.Split(" ")\n        }\n\n        if ($hashToFind[0].Length -eq 40){$hashType = "SHA1"; break}\n        elseif ($hashToFind[0].Length -eq 64){$hashType = "SHA256"; break}\n        elseif ($hashToFind[0].Length -eq 96){$hashType = "SHA384"; break}\n        elseif ($hashToFind[0].Length -eq 128){$hashType = "SHA512"; break}\n        elseif ($hashToFind[0].Length -eq 32){$hashType = "MD5"; break}\n        else {echo "Hash length is not of supported hash type."}\n    }\n
Run Code Online (Sandbox Code Playgroud)\n