searching a string using powershell i a web page - powershell

I need to validate the username and password of this site. When I type invalid password it should check the page for string 'invalid email'. The message fail to login should be displayed. But 'success' is being displayed.? What is the mistake?
$url = "https://www.jabong.com/customer/account/login/"
$cred = Get-Credential #Read credentials
$username = $cred.username
$username = $username.Replace("\", "")
$password = $cred.GetNetworkCredential().password
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
while ($ie.Busy -eq $true)
{
Start-Sleep 1;
}
$ie.Document.getElementById("LoginForm_email").value = $username
$ie.Document.getElementByID("LoginForm_password").value=$password
$ie.Document.getElementByID("qa-login-button").Click();
while($ie.busy) {sleep 1}
$webClient = new-object System.Net.WebClient
$webClient.Headers.Add("user-agent", "PowerShell Script")
$output = $webClient.DownloadString("https://www.jabong.com/customer/account/login/")
if ($output -like "*Invalid mailid*") {
"login failed"
} else {
"success"
}
sleep(300)

Whilst I generally agree with #Alexander Obersht, you are only one step away from success. By creating $webClient = new-object System.Net.WebClient you establish another session to the website, when in fact you should be polling your current session inside $ie object.
Try this:
$url = "https://www.jabong.com/customer/account/login/"
$cred = Get-Credential #Read credentials
$username = $cred.username
$username = $username.Replace("\", "")
$password = $cred.GetNetworkCredential().password
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
while ($ie.Busy -eq $true)
{
Start-Sleep 1;
}
$ie.Document.getElementById("LoginForm_email").value = $username
$ie.Document.getElementByID("LoginForm_password").value=$password
$ie.Document.getElementByID("qa-login-button").Click();
while($ie.busy) {sleep 1}
$output = $ie.Document.Body.innerHTML
if ($output -like "*Invalid mail*") {
"login failed"
} else {
"success"
}
I also changed your match to Invalid mail.

Related

Website Login using Powershell - Click method missing

I'm trying to automate the way I login to my amazon account. Currently, I'm stuck on not being able to automate left-click on continue button after entering username.
#Here's my code:
$username = "1#2.com"
$password = "test123"
$ie = New-Object -ComObject InternetExplorer.Application;
$ie.visible=$true
$ie.navigate("https://www.amazon.com/ap/signin")
while($ie.Busy) { Start-Sleep -seconds 5 }
($ie.document.getElementById("ap_email") |select -first 1).value = $username
Start-Sleep -seconds 5
while($ie.Busy) { Start-Sleep -seconds 5 }
$continueButton = $ie.document.getElementById("continue").Click()
Write-Output Script_Complete.
pause
$ie.quit()
What about something like this?
$username = "username"
$password = "password"
$ie = New-Object -com "InternetExplorer.Application"
$ie.visible=$true
$ie.navigate("https://login.amazon.com/agreement")
while($ie.Busy) { Start-Sleep -seconds 2 }
($ie.document.getElementById("ap_email") |select -first 1).value = $username
($ie.document.getElementById("ap_password") |select -first 1).value = $password
while($ie.Busy) { Start-Sleep -seconds 2 }
$continueButton = $ie.document.getElementById("signInSubmit")
$continueButton.Click()
Write-Output Script_Complete.
pause
$ie.quit()

Check if login failed

I have the following code, which logs into a site that I use frequently:
param(
[string]$interval
)
function Refresh-WebPages {
"Neptun refresh every $interval seconds."
"press any key to exit."
$shell = New-Object -ComObject Shell.Application
do {
Start-Sleep -Seconds $interval
'Refreshing'
$shell.Windows() |
Where-Object { $_.Document.url } |
ForEach-Object { $_.Refresh() }
} until ( [System.Console]::KeyAvailable )
[System.Console]::ReadKey($true) | Out-Null
}
$username = "asd";
$SecureString = Read-Host "password" -AsSecureString;
$BSTR =
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString);
$PlainPassword =
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$IE=new-object -com internetexplorer.application;
$IE.navigate2("https://hallgato.neptun.elte.hu/login.aspx");
$IE.visible=$true;
While ($IE.Busy -eq $true) {Start-Sleep -Milliseconds 500}
$IE.Document.getElementById('user').value = "$username";
$IE.Document.getElementById('pwd').value = "$PlainPassword";
($IE.document.getElementById('btnSubmit') |select -first 1).click();
Refresh-WebPages -interval
I want to check if the login have failed or succeeded. Can I check if the URL is different somehow?

Login to another domain from powershell

I wrote a script to logout the citrix user and it is working fine, if I am accessing it with same domain, but if I try to run that script from some other computer where the local domain is different from the one citrix connects to it is failing, please let me know how I can connect to the citrix domain from the other local domain.
To give context I am attaching the code below,
Please help.
Regards,
AVs
Code:
# Import the Active Directory module for the Get-ADComputer CmdLet
Import-Module ActiveDirectory
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
#Form to take username and password
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Citrix User Session Disconnection"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
#Data Label
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(10,130)
$Label.Size = New-Object System.Drawing.Size(250,150)
$Label.ForeColor = "White"
$Label.BackColor = "Blue"
$Label.Text = "***Give your Citrix username and password to logoff the user from the server.***"
$objForm.Controls.Add($Label)
#Username Label
$userLabel = New-Object System.Windows.Forms.Label
$userLabel.Location = New-Object System.Drawing.Size(10,20)
$userLabel.Size = New-Object System.Drawing.Size(80,30)
$userLabel.Text = "User Name"
$objForm.Controls.Add($userLabel)
#Username Textbox
$userTextBox = New-Object System.Windows.Forms.TextBox
$userTextBox.Location = New-Object System.Drawing.Size(130,20)
$userTextBox.Size = New-Object System.Drawing.Size(150,20)
$objForm.Controls.Add($userTextBox)
#Password Label
$PassLabel = New-Object System.Windows.Forms.Label
$PassLabel.Location = New-Object System.Drawing.Size(10,60)
$PassLabel.Size = New-Object System.Drawing.Size(80,30)
$PassLabel.Text = "Password"
$objForm.Controls.Add($PassLabel)
#Password Textbox
$PassTextBox2 = New-Object System.Windows.Forms.MaskedTextBox
$PassTextBox2.PasswordChar = '*'
$PassTextBox2.Location = New-Object System.Drawing.Size(130,60)
$PassTextBox2.Size = New-Object System.Drawing.Size(150,20)
$objForm.Controls.Add($PassTextBox2)
#Disconnect Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(200,100)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.ForeColor = "Red"
$OKButton.Text = "Disconnect"
$OKButton.Add_Click({$username=$userTextBox.Text;$objForm.Close()})
$OKButton.Add_Click({$Password=$PassTextBox2.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$objForm.ShowDialog()
Read-Host "DOMAIN\USERNAME" -AsSecureString | ConvertFrom-SecureString | Out-File C:\SecureData\SecureString.txt
#SharePoint Admin Account
$SPAdmin = "DOMAIN\ADMIN"
$Password = Get-Content C:\SecureDate\securestring.txt | convertto-securestring
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $SPAdmin, $Password
Get-WmiObject -Class Win32_Service -ComputerName "Server" -Filter "Name='ServiceName'" -Credential $Credential
#Authenticaton
$Domain = $env:USERDOMAIN
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext $ct,$Domain
$auth = $pc.ValidateCredentials($userName,$Password)
# Get today's date for the report
$today = Get-Date
#Setup email parameters
#$subject = "ACTIVE SERVER SESSIONS REPORT - " + $today
#$priority = "Normal"
#$smtpServer = "mail.itechnologies.com.au"
#$emailFrom = "rayithy#itechnologies.com.au"
#$emailTo = "rayithy#itechnologies.com.au"
# Create a fresh variable to collect the results. You can use this to output as desired
$SessionList = "ACTIVE SERVER SESSIONS REPORT - " + $today + "`n`n"
# Query Active Directory for computers running a Server operating system
#$Servers = Get-ADComputer -Filter {OperatingSystem -like "*server*"}
$Servers = Import-Csv C:\powershell\Test.csv
if ($auth -eq "True")
{
# Loop through the list to query each server for login sessions
ForEach ($Server in $Servers) {
$ServerName = $Server.Name
# When running interactively, uncomment the Write-Host line below to show which server is being queried
# Write-Host "Querying $ServerName"
# Run the qwinsta.exe and parse the output
$queryResults = (qwinsta /SERVER:$ServerName | foreach { (($_.trim() -replace "\s+",","))} | ConvertFrom-Csv)
# Pull the session information from each instance
ForEach ($queryResult in $queryResults) {
$RDPUser = $queryResult.USERNAME
$sessionType = $queryResult.SESSIONNAME
# We only want to display where a "person" is logged in. Otherwise unused sessions show up as USERNAME as a number
If (($RDPUser -match $username) -and ($RDPUser -ne $NULL)) {
# When running interactively, uncomment the Write-Host line below to show the output to screen
# Write-Host $ServerName logged in by $RDPUser on $sessionType
$SessionList = $SessionList + "`n`n" + $ServerName + " logged in by " + $RDPUser + " on " + $sessionType
logoff $sessionType /server:$ServerName
Write-Host $RDPUser "LoggedOff"
}
}
}
}
else {
$Font = New-Object System.Drawing.Font("Times New Roman",14,[System.Drawing.FontStyle]::Italic)
#Form to display the error
$objForm2 = New-Object System.Windows.Forms.Form
$objForm2.Text = "Citrix User Session Disconnection"
$objForm2.Size = New-Object System.Drawing.Size(300,200)
$objForm2.StartPosition = "CenterScreen"
$objForm2.BackColor = "Yellow"
#Error message
$errorLabel = New-Object System.Windows.Forms.Label
$errorLabel.Location = New-Object System.Drawing.Size(10,20)
$errorLabel.Size = New-Object System.Drawing.Size(250,150)
$errorLabel.Text = "'Username/Password is not correct' Or 'User Not Logged in the Server'"
$errorLabel.Font = $Font
$errorLabel.forecolor = "Red"
$objForm2.Controls.Add($errorLabel)
$objForm2.ShowDialog()
}
In the Authentication section of your script:
#Authenticaton
$Domain = $env:USERDOMAIN
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext $ct,$Domain
$auth = $pc.ValidateCredentials($userName,$Password)
You're passing in $Domain as the current user's Domain. If you modify this to specify the domain Citrix auths against, you should be able to connect

login into a web page using powershell

I need to log on to a web page with credentials from the get-credential window. But I am getting error- Property 'Value' cannot be found on this object; make sure it exists and is settable.I have provided my powershell source code here..
$url = "ameriprisestage.service-now.com/"
$cred = Get-Credential
$username = $cred.username
$username = $username.Replace("\", "")
$password = $cred.GetNetworkCredential().password
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
while ($ie.ReadyState -ne 4 -or $ie.Busy)
{
start-sleep -milliseconds 100
}
#$uname=$ie.Document.getElementsByTagName("input")
#$unameBox=$uname | where { $_.name -eq "user_name" }
$unameBox=$ie.Document.getElementById("user_name")
$unameBox.value = $username
$pass=$ie.Document.getElementsByTagName("input") | ? { $_.id -eq "user_password" }
$pass.value = $password
$pass.select
$buttn=$ie.Document.getElementsByTagName("button") | ? { $_.id -eq "sysverb_login" }
$buttn.click()
while($ie.busy) {
Start-sleep 1;
}
There are multiple windows in the web page. Try to redirect it to the relevant window.
cd HKCU:\"Software\Microsoft\Windows\CurrentVersion\Internet Settings"
set-itemproperty . ProxyEnable 1
$url = "https://ameriprisestage.service-now.com/"
#function call
#$cred = Get-Credential
$username = "asset_tester02"
$username = "asset_tester02"
$password = "tester02"
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
while ($ie.Busy -eq $true)
{
Start-Sleep 1;
}
$usr=$ie.document.getElementbyID("gsft_main").contentWindow.document.getElementbyID("user_name").value=$username
$pass=$ie.document.getElementbyID("gsft_main").contentWindow.document.getElementById("user_password").value= $password
$buttn=$ie.document.getElementbyID("gsft_main").contentWindow.document.getElementById("sysverb_login").click()

How to use obtained credentials in powershell to find groups of the Authenticated user?

I wrote a script, which on execution asks for credentials; it goes like this;
$cred = Get-Credential #Read credentials
$username = $cred.username
$password = $cred.GetNetworkCredential().password
$CurrentDomain = "LDAP://" + ([ADSI] "" ).distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain, $UserName, $Password)
if ($domain.name -eq $null)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show("Authentication failed - please verify your username and password.")
exit
}
else
{
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show("Authentication Success")
$Groups = [System.Security.Principal.WindowsIdentity]::GetCurrent().Groups
-> I recognized my mistake later, the last line collects the groups of user who is logged into the windows machine.I need the groups of the person who authenticated via script; how to change this and obtain the groups for the person who authenticated into script rather the one using windows authentication?
Please do let me know of any questions or clarifications.
You can try this:
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")$cred = Get-Credential #Read credentials
$username = $cred.username
$password = $cred.GetNetworkCredential().password
$CurrentDomain = "LDAP://" + ([ADSI] "" ).distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain, $UserName, $Password)
if ($domain.name -eq $null)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show("Authentication failed - please verify your username and password.")
exit
}
else
{
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$cred = Get-Credential #Read credentials
$username = $cred.username
$password = $cred.GetNetworkCredential().password
$CurrentDomain = "LDAP://" + ([ADSI] "" ).distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain, $UserName, $Password)
if ($domain.name -eq $null)
{
[System.Windows.Forms.MessageBox]::Show("Authentication failed - please verify your username and password.")
exit
}
else
{
[System.Windows.Forms.MessageBox]::Show("Authentication Success")
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$user = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($ct, $username)
$groups = $user.GetGroups()
foreach($i in $groups){
$i.SamAccountName
}
}
}
if you're able to install quest activesrole it will be as simple as :
(get-qaduser $user).memberof
otherwise you can use invoke-command and provide the credentials:
$groups=Invoke-Command -ComputerName $env:COMPUTERNAME -Credential $cred -ScriptBlock {
[System.Security.Principal.WindowsIdentity]::GetCurrent().Groups
}