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.
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.
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.
Or could you get it somewhere else? Maybe try [Environment]::UserName ? If this is not automatic script, password from Read-Host and to secure string, maybe?
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.
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.
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.
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
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.