Login credentials to website using powershell v5 - powershell

I am new to powershell and I am trying to figure out how to insert my username and password into a website to login. Below is what I have come up with but failing to get it to work. Also the username and password are AD accounts if that matters.
URL of the site that will be launched.
$Url = “http://www.example.com”
$title = "User Authentication Required"
$message = "Enter user credentials for example.com"
$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.
while ($IE.Busy -eq $true)
{Start-Sleep -Milliseconds 2000;}
The following UsernameElement, PasswordElement, and LoginElement need to be modified first.
# 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
If I run this in v2 it still does not input the username but for the password it gives an error of System.Management.Automation. The enter code to login is working. Can someone please explain how can I get a username and password to input into the website page and the password needs to be hidden/secured.

Related

Website Login Automation and Output STatus

I am trying to automate a task to login and verify or not login is success in a nutshell I am able to create.
$username = "XXXX"
$password = "XXXXXXXX"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate("https://com/login")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById("user_email").value= "$username"
$ie.document.getElementById("user_password").value = "$password"
$ie.document.getElementById("Loginform").submit()
start-sleep 20
$ie.Document.body
Its throwing error
Exception from HRESULT: 0x800A01B6
+ $ie.document.getElementById("user_email").value= "$username"
At line:8 char:1
+ $ie.document.getElementById("user_password").value = "$password"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException
Exception from HRESULT: 0x800A01B6
At line:9 char:1
i can confirm that the HTML id for the login box is user_email and user_password

ExecuteCrmOrganizationRequest fails with PublishThemeRequest

I'm trying to write a powershell script to publish a theme in my on-premise installation of Dynamics CRM.
According to this page it should be really straight forward, I create an object of type PublishThemeRequest which derives from OrganizationRequest and call the method ExecuteCrmOrganizationRequest.
This is the code I'm running:
Import-Module Microsoft.Xrm.Data.Powershell
Add-PSSnapin Microsoft.Xrm.Tooling.Connector
$orgName = "<my organization name>";
$serverUrl = "http://server_url";
$Cred = Get-Credential -UserName "<my username>" -Message "Please Enter admin credentials for CRM"
$conn = Get-CrmConnection -Credential $Cred -OrganizationName $orgName -ServerUrl $serverUrl
$req = New-Object Microsoft.Crm.Sdk.Messages.PublishThemeRequest
$req.Target = New-CrmEntityReference -EntityLogicalName "theme" -Id "DB80D57A-6410-4D11-B784-0093122802AC"
$result = [Microsoft.Crm.Sdk.Messages.PublishThemeResponse]$conn.ExecuteCrmOrganizationRequest($req, $null)
This is what I get when I execute the code above:
Cannot convert argument "req", with value: "Microsoft.Crm.Sdk.Messages.PublishThemeRequest", for "ExecuteCrmOrganizationRequest" to type
"Microsoft.Xrm.Sdk.OrganizationRequest": "Cannot convert the "Microsoft.Crm.Sdk.Messages.PublishThemeRequest" value of type
"Microsoft.Crm.Sdk.Messages.PublishThemeRequest" to type "Microsoft.Xrm.Sdk.OrganizationRequest"."
At C:\Users\xxxxxxxxxx\Desktop\PublishTheme.ps1:21 char:1
+ $result = [Microsoft.Crm.Sdk.Messages.PublishThemeResponse]$conn.Exec ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
I have been reading the documentation and other websites for a couple of hours now but seem to have hit a wall.
Any ideas of what my problem might be?

Unable to login to a website via powershell

I am trying to open a url in IE and then pass username and password to login to the url.But my code is not filling username and password
Following is code
$username = "userhere"
$password = "passhere"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$false
$ie.navigate("https://ameriprisestage.service-now.com/")
#while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById("user_name").value= "$username"
$ie.document.getElementById("user_password").value = "$password"
Error: You cannot call a method on a null-valued expression. At line:7 char:1 + $ie.document.getElementById("user_name").value= "$username" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression. At line:8 char:1 + $ie.document.getElementById("user_password").value = "$password" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
There is a couple of things to note. First, your page is not ready yet. Yuo must wait for load. Your commented line was intended to do that. Second, both fields are in iframe which works as a separate sandbox - has his own document object. I also removed window hiding code to see results. Here is working code:
$username = "userhere"
$password = "passhere"
$ie = New-Object -com InternetExplorer.Application
$ie.navigate("https://ameriprisestage.service-now.com/")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById('gsft_main').contentWindow.document.getElementById('user_name').value = $username
$ie.document.getElementById('gsft_main').contentWindow.document.getElementById('user_password').value = $password
Make sure you close $ie object. You may have tons of them as background processes.

How to reset a Domain User Password with Remote Powershell

I want to reset my own Active Directory Password on a remote Machine (different domain).
If I use the following code snippet locally it works perfectly:
param(
[string]$oldPassword = $(Read-Host "Old password"),
[string]$newPassword = $(Read-Host "New password")
)
$ADSystemInfo = New-Object -ComObject ADSystemInfo
$type = $ADSystemInfo.GetType()
$user = [ADSI] "LDAP://$($type.InvokeMember('UserName', 'GetProperty', $null, $ADSystemInfo, $null))"
$user.ChangePassword($oldPassword, $newPassword)
Running following snippet on the remote machine fails:
$ADSystemInfo = New-Object -ComObject ADSystemInfo
$type = $ADSystemInfo.GetType()
Invoke-Command -Session $Session -ScriptBlock {
param($rtype, $RemoteADSystemInfo, $OldPassword)
$user = [ADSI] "LDAP://$($rtype.InvokeMember('UserName', 'GetProperty', $null, $RemoteADSystemInfo, $null))"
$user.ChangePassword($OldPassword , "TestPa$$w0rd"")
} -ArgumentList $type,$ADSystemInfo,$Password
Error Message: Method invocation failed because
[Deserialized.System.RuntimeType] does not contain a method named
'InvokeMember'.
+ CategoryInfo : InvalidOperation: (InvokeMember:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
+ PSComputerName : test.test.domain The following exception occurred while retrieving member "ChangePassword": "Unknown error (0x80005000)"
+ CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : CatchFromBaseGetMember
+ PSComputerName : test.test.domain

Powershell CMDLET Cross Domain Managment

I'm logged in at domain "domain1" with my account. I wish via powershell to be able to update users in domain "domain2" via my supe ruser account "suaccount" with password "password1". Trust is established between the two.
Running PowerShell 2.0 and .NET 3.5 SP1
I have gotten this far:
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ctype = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$context = New-Object -TypeName
System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ctype, "domain2", "OU=TestOU,DC=domain2", "suaccount", "password1"
$usr = New-Object -TypeName System.DirectoryServices.AccountManagement.UserPrincipal -ArgumentList $context
$usr.Name = "AM Test1"
$usr.DisplayName = "AM Test1"
$usr.GivenName = "AM"
$usr.SurName = "Test1"
$usr.SamAccountName = "AMTest1"
$usr.UserPrincipalName = "amtest1#mtest.test"
$usr.PasswordNotRequired = $false
$usr.SetPassword("errr")
$usr.Enabled = $true
$usr.Save()
Pretty new to PowerShell, any pointers? I want to edit/create users on the "other" domain so to speak.
I get the error:
"Exception calling "Save" with "0" argument(s): "General access denied error
"
At C:\Script\Sandbox\Morris PowerShell Application\includes\mo\mo.ps1:104 char:14
+ $usr.Save <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException"
Any pointers?
From comments: Try using for username this format domain2\username, and always use the FQDN for the domain. – Christian yesterday