Login Credentials to website

This topic contains 11 replies, has 3 voices, and was last updated by Profile photo of Jarkko Vepsäläinen Jarkko Vepsäläinen 1 week, 5 days ago.

  • Author
    Posts
  • #58090
    Profile photo of Cory
    Cory
    Participant

    New to the PowerShell world but thought I would give it a try.

    Currently I am trying to figure out how to setup a script that will ask for my username and password(password needs to be hidden) and then input that onto a website to login.

    I currently have this setup to enter my info

    #Get Admin Credentials
    Function Get-Login {
    Clear-Host
    Write-Host "Please provide Username and Password"
    $Global:Credential = Get-Credential
    }
    Get-Login

    However I am not for sure what the next step is to get the credentials to insert into the website.

  • #58094
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Yeah, and that's not going to work because it's difficult to extract just the password from a Credential object. You probably want to use Read-Host to prompt for a SecureString (there's a parameter for that) instead.

    But... what you're wanting to do is nontrivial. You're going to have to use the IE COM automation object, which is ancient and fragile. You tell it to pull up the page, and then do some Javascript-ish stuff to insert your values into the web page's text boxes, and then to click whatever login button the webpage offers. Think really, really hard about how badly you want to do this, because it's pretty delicate.

    Alternately, you could use Invoke-WebRequest to submit a page directly to the website – but again, this is going to take some solid knowledge of HTML and HTTP headers. It's a lot more than just PowerShell scripting.

  • #58099
    Profile photo of Cory
    Cory
    Participant

    I should add that the script would need to launch IE and go to the website to then automatically login.

  • #58103

    Hi

    Invoke-WebRequest is what you are after for, here's a great link https://msdn.microsoft.com/powershell/reference/5.1/microsoft.powershell.utility/Invoke-WebRequest

    Saving response to a variable and looking it with FL and especially with Get-Member is a great way to search and look what you can do with it.

    Jake

  • #58124
    Profile photo of Cory
    Cory
    Participant

    Jake

    Correct me if I am wrong but looks like I have to hard code the username and password into the script. Which I do not want to do as I want each user to enter their own credentials to login. I need the script to prompt for username to be entered and then password but hide the password so no one else can see it. Then once those are enter it will launch the website and use that info to log in. I have more steps after that I need to do but just trying to get the login to work first.

  • #58126
    Profile photo of Cory
    Cory
    Participant

    Here is what is coded so far...

    $R=Invoke-WebRequest http://www.MYSITE.com -SessionVariable test
    $test
    $Form = $R.Forms[0]
    $Form | Format-List
    $Form.fields
    $Form.Fields["userid"]="name"
    $Form.Fields["password"]="pass"
    $R=Invoke-WebRequest -Uri ("http://www.mysite.com" + $Form.Action) -WebSession $test -Method POST -Body $Form.Fields

    USERID is the name of the input field and Password is the name for the second input field
    I am getting this error

    Cannot index into a null array.
    At C:\logon test.ps1:6 char:1
    + $Form.Fields["userid"]="name"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

    Cannot index into a null array.
    At C:\logon test.ps1:7 char:1
    + $Form.Fields["password"]="pass"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

  • #58343
    Profile photo of Cory
    Cory
    Participant

    Thank you again for the help. I was able to get the pop up to request the username but it is not putting it in the fields. Below is what I have. The website opens up and goes to the main page.

    # URL of the site that will be launched.

    $Url = “http://www.mysite.com”

    $title = "User Authentication Required"

    $message = "Enter user credentials for MYSITE"

    $user = ""

    $user_creds = $host.ui.PromptForCredential($title, $message, $user, "")

    $IE = New-Object -com internetexplorer.application;

    $IE.visible = $true;

    $IE.navigate($url);

    # Wait a few seconds and then launch the executable.

    while ($IE.Busy -eq $true)

    {

    Start-Sleep -Milliseconds 2000;

    }

    # The following UsernameElement, PasswordElement, and LoginElement need to be modified first. See the notes at the top
    # of the script for more details.

    $IE.Document.getElementById(“userid”).value = $User_creds.Username

    $IE.Document.getElementByID(“password”).value = $User_creds.password

    $IE.Document.getElementById(“button-1034-btnIconEl”).Click()

    This is the error I am getting now...

    The property 'value' cannot be found on this object. Verify that the property
    exists and can be set.
    At C:\logon test.ps1:27 char:1
    + $IE.Document.getElementById("userid").value = $User_creds.Username
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

    The property 'value' cannot be found on this object. Verify that the property
    exists and can be set.
    At C:\logon test.ps1:28 char:1
    + $IE.Document.getElementByID("password").value = $User_creds.password
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

    • #58436

      Hi

      Here's one example for adding the credentials that you can use with your current script, http://www.westerndevs.com/simple-powershell-automation-browser-based-tasks/ or maybe just ask only password with $password = Read-Host -Prompt "Enter password of user ${username}:" -AsSecureString not possibly best choise but worth of trying.

      If you don't need to open IE I would recommend to use the Invoke-WebRequest and trying New-Object System.Management.Automation.PSCredential for username and password.

      Jake

  • #58450
    Profile photo of Cory
    Cory
    Participant

    Thanks Jake. That is the code I am trying to use but it keeps coming back with a value error so trying to find my mistake.

    I first would like to get the IE open page to work then later I might decide to go the other route and not launch IE and just do everything in the background.

  • #58466
    Profile photo of Cory
    Cory
    Participant

    OK here is an update. Using my code above will not work on v5 powershell keeps giving me the error that is above with wrong variable.

    However if I run it with a v2 powershell client it still does not work but it no longer gives me a viable error but instead it displays nothing in the userid field and in the password field it shows System.Management.Automation

    Any ideas

  • #58469

    Hi

    The property 'value' cannot be found on this object. Verify that the property exists and can be set.

    This looks like this $IE.Document.getElementById(“userid”).value = $User_creds.Username is not existing. Look your $IE with Get-Member and FL and to to look where those username and password might be. I would also start to look the Invoke-WebRequest if this is going to be automated anyway so there is no reason to open IE anyways.

    Jake

You must be logged in to reply to this topic.