How to wait for user to enter Password and proceed with next steps in Power shel

Hi All,

I am passing the username using the script, but for the password part, i want to do that interactively, once the password is entered by the user then the script should proceed with next steps. I am using the below script, once username is populated in the website then sleep for 2 mins after that click on login. But its not working, its not coming out of sleep. Kindly let me know if i miss anything or any other alternative option


Create an Internet Explorer object

$ie = New-Object -ComObject ‘internetExplorer.Application’
$ie.Visible= $true # Make it visible

Open all websites

$ie.Navigate(“mywebpage”)

Script to wait till webpage is downloaded into the browsers.

while ($ie.Busy -eq $true){Start-Sleep -seconds 4;}

Feed in your credentials to the input fields on the web page

$usernamefield = $ie.Document.getElementByID(‘j_username’)
$usernamefield.value = ‘user’
Start-Sleep -seconds 120 ----(User enters the Password during this period)

After user enters the password

Click Login

$submitButton = $ie.document.getElementById(“loginData”).submit()


My advice is to get the information at the start of the script. Adding a param() block at the beginning of your script to prompt for username and password would do the trick. The advantage is that users could store the credentials securely in an encrypted form on disk and reuse while passing the Credential parameter.

param (
  [Parameter(Mandatory)]
  [System.Management.Automation.CredentialAttribute()]
  [PSCredential]
  $Credential
)
# ...

# Access the credential details provided
$Credential.UserName
$Credential.GetNetworkCredential().Password

# ...

Hi Daniel,

Thanks for the reply.

I am new to Powershell, Could you please help me on this.
How do i pass the password information as parameter/variable so that i can automate my login proceess.

Hi Daniel,

I am able to do it using the below, please confirm if it is a secure way or not?

#$SecureString = Read-Host -AsSecureString
#$EncryptedPW = ConvertFrom-SecureString -SecureString $SecureString
Set-Content -Path “C:\mypw.txt” -Value $EncryptedPW

$EncryptedPW = Get-Content -Path “C:\mypw.txt”
$SecureString = ConvertTo-SecureString -String $EncryptedPW
$pswd=[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString))

Thanks