How to pass on credentials to windows security pop up for signing in

This topic contains 8 replies, has 3 voices, and was last updated by Profile photo of Andrew Andrew 5 months ago.

  • Author
    Posts
  • #63871
    Profile photo of Sandeep
    Sandeep
    Participant

    Basically there is a requirement, where i need to login to a web portal by passing on credentials through windows security pop up (like what we have to login for tomcat admin service console) & then click on a reload button on the next screen.

    The script for clicking on the Reload button works fine but the problem is i have to manually login through the windows pop up & keep one active session open all the time & this is not possible when i want to add this script to a task scheduler.

    The moment it tries to open http://localhost:9001/test/Admin/RuntimeParameters.jsp this is where it pops up with the windows security box & there is no way known to me to pass on the creds to this section. I need help for this piece.

    
    	$ie = New-Object -com InternetExplorer.Application 
    	$ie.visible=$false
    	$ie.navigate("http://localhost:9001/test/Admin/RuntimeParameters.jsp")
    	start-sleep 4
    	while($ie.ReadyState -ne 4) {start-sleep -m 100} 
    	$ie.document.getElementById("Reload").Click()
    	if ($? -eq "True")
    		{
    		Write-host "Reload of the Admin page completed successfully" -ForegroundColor green
    		Write-output "Reload of the Admin page completed successfully"
    		exit
    		}
    		else
    		{
    		Write-host "Reload of the Admin page Failed" -ForegroundColor red
    		Write-output "Error: Reload of the Admin page Failed"
    		exit
    		}
    				
    
    

    So i need help to know as how can we pass on credentials through PS script to login through a windows security pop up window not sure how PS can interact with this pop up window.

    Thanks for checking & providing expert advise.

  • #64004
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    You can try to pass username and password in the URL.

    Example:

    http://monitoruser:Pa55w0rd@localhost:9001/test/Admin/RuntimeParameters.jsp
    

    Just make sure the username and password don't include the following single characters

    : @ /

    • #64180
      Profile photo of Sandeep
      Sandeep
      Participant

      Thanks Daniel for the response but it seems, current version of IE doesn't support this method of passing on the creds through URL anymore 🙁

  • #64006
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    The other alternative would be to use the Invoke-WebRequest cmdlet if you don't need to display the page on a screen.

    $creds = Get-Credential
    $response = Invoke-WebRequest -UseBasicParsing -Uri http://localhost:9001/test/Admin/RuntimeParameters.jsp -Credential $creds
    # do something with the response
    
    • #64183
      Profile photo of Sandeep
      Sandeep
      Participant

      Hi Daniel, instead of Invoke-WebRequest i tried this which allows to login & allows to fetch the web page code details instead of popping up the web page itself.

      $url = "http://localhost:9001/test/Admin/RuntimeParameters.jsp"
      $user = "username"
      $pass = "pass"
      $secPass = convertTo-secureString $pass -AsPlainText -Force
      $cred = New-Object System.Management.Automation.PSCredential($user,$secPass)
      
      $data = Invoke-RestMethod $url -Credential $cred
      

      However, is there way to open the web page after it logs in like i had $url.visible=$true using $ie = New-Object -com InternetExplorer.Application & the navigating to this URL. I guess we cannot merge two invoke requests in the same method.

      Please advise.

  • #64312
    Profile photo of Sandeep
    Sandeep
    Participant

    I can use both the invoke methods & assume am able to login fine but that's all happening in the background.

    The requirement is to click on a particular button on this web page after i am logged in into this webpage.

    Tried both & get runtime error

    $url = "http://localhost:9001/test/Admin/RuntimeParameters.jsp"
    $user = "username"
    $pass = "pass"
    $secPass = convertTo-secureString $pass -AsPlainText -Force
    $cred = New-Object System.Management.Automation.PSCredential($user,$secPass)
    
    $response = Invoke-WebRequest -UseBasicParsing -Uri $url -Credential $cred
    
    Write-host $response.StatusCode
    $Link=$response.Document.getElementByID("Reload").Click
    

    Also tried –

    $Link=$response.Document.getElementsByTagName("input") | where-object {$_.type -eq "submit"}
    $Link.click()
    

    I am trying to click The actual button from the html code from the web page is

    
    form action = "#" method="POST"
    input type="submit" value="Reload" name="Reload"
    
    
  • #64362
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    Thanks for the update. I think you'll need to look at Selenium which is web browser automation framework (http://www.seleniumhq.org/) and it is free as far as I know.

  • #64510
    Profile photo of Andrew
    Andrew
    Participant

    Have you tried

    Get-Credential | Export-Clixml -Path 
    

    This will bring up the UN/PW windows dialogue box. When you enter your credentials, this will write the UN and PW to an encrypted text file (as stipulated by your given path). For instance

    Get-Credential | Export-Clixml -Path C:\Cred\EncryptedCredentials.txt

    You can then add the content of this file to a variable in your script.

    $MyCredential = Import-Clixml -Path C:\Cred\EncryptedCredentials.txt
    

    Which can later be refeferenced by a -credentials parameter in your script.

    This works fine for me when running scripts that need to authenticate against an externeal FTP server.

    Caveat: The acount you are logged in within when you create the encrypted credentials will need to be the same account you want to run the scripts under.

  • #64524
    Profile photo of Andrew
    Andrew
    Participant

    *need to run the scripts under.

You must be logged in to reply to this topic.