Prompt for user input in PowerShell

前端 未结 4 810
鱼传尺愫
鱼传尺愫 2020-11-29 15:51

I want to prompt the user for a series of inputs, including a password and a filename.

I have an example of using host.ui.prompt, which seems sensible,

4条回答
  •  执念已碎
    2020-11-29 16:18

    Place this at the top of your script. It will cause the script to prompt the user for a password. The resulting password can then be used elsewhere in your script via $pw.

       Param(
         [Parameter(Mandatory=$true, Position=0, HelpMessage="Password?")]
         [SecureString]$password
       )
    
       $pw = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
    

    If you want to debug and see the value of the password you just read, use:

       write-host $pw
    

提交回复
热议问题