首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法在Powershell中预先填写Read-Host?

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

Stack Overflow用户
提问于 2016-12-01 04:56:01
回答 2查看 1.5K关注 0票数 4

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

代码语言:javascript
复制
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的新手,所以如果有任何其他的意见,欢迎他们!

EN

回答 2

Stack Overflow用户

发布于 2019-11-22 01:01:43

来自Super User

代码语言:javascript
复制
[System.Windows.Forms.SendKeys]::SendWait("yes")
Read-Host "Your answer"
票数 1
EN

Stack Overflow用户

发布于 2016-12-01 15:51:08

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

代码语言:javascript
复制
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."}
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40898042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档