2016-11-30 44 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

AFAIK有沒有辦法預先填充讀主機對話,但數據進入提示保存在命令歷史記錄,所以全部用戶需要做的是爲了回到以前輸入的哈希將是'上'箭頭鍵,他們將能夠編輯他們需要的任何。然而,查看你的腳本,你可能會更好地與[參數驗證](https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/15/simplify-your-powershell-script-with-parameter-validation/ )而不是這個while循環 –

+0

無法預先填充它,據我所知。 – 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."} 
    } 
相關問題