2016-11-30 10 views
2

私は、ファイルハッシュがフォルダ内に存在するかどうかをユーザが見つけるのを助けるスクリプトを持っています。ユーザーがハッシュを入力した後、それはどのタイプのハッシュであるかを判断し、サポートされていない場合、またはユーザーが手紙を見逃した場合、ハッシュを求めるように戻ります。使いやすさのために、以前からユーザーが入力した内容を事前に入力して、最初からやり直す必要はありません。PowershellでRead-Hostを事前に記入する方法はありますか?

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の方が新しいですから、他のコメントがあれば歓迎します!

+0

は、私の知る限り、しかしデータは、コマンド履歴に保存されているプロンプトに入力された読み取りホストダイアログを事前入力する方法はありませんので、すべてのユーザーは以前に入力したハッシュに戻すためには「上向き」の矢印キーを押す必要があり、必要なものを編集できるようになります。しかし、あなたのスクリプトを見渡すと、[パラメータの検証](https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/15/simplify-your-powershell-script-with-parameter-validation/)でよりうまく対応できます。 )これの代わりにループ –

+0

私は知っている限り、それをprefillする方法はありません。 – 4c74356b41

答えて

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."} 
    } 
関連する問題