Welcome to my Getting Started with Windows PowerShell series!
In case you missed the earlier posts, you can check them out here:
We will be exploring:
I've covered using Invoke-WebRequest, as well as Invoke-RestMethod for basic and advanced tasks... why would we need anything else? Sometimes you'll try using Invoke-WebRequest, and you swear the data should be there, yet for some reason you just can't seem to parse it out.
The reasons for this can vary. Typically, it is because the page renders something with a script, and you can only gather it with a browser being opened/controlled. If you're having a hard time parsing the data from Invoke-WebRequest, controlling IE can be a time saving solution. This can come in handy if you need to script up something quick, and then work on a long term solution after you know what's possible.
The first thing we will need to do is instantiate an instance of the Internet Explorer COM object. To do this, we'll use New-Object.
$ieObject will store the object and contains the properties and methods we can work with. Let's take a peek:
We will be using a few of these properties and methods as we work with controlling IE.
By default, the instance of Internet Explorer is not visible. For automation, that's great!
For demoing and testing, not so much!
Let's show the current instance of IE:
There's our window.
After instantiating our object, we can use it to navigate to a website.
To do this, we will utilize the Navigate method, and pass along a string value to navigate to.
Now the browser should be at the website.
Now that we have navigated to the site, we will need to login. To login, we'll first need do some some information gathering.
First, we will store the current page's document in a new variable named $currentDocument.
Then, we will list out all of the tags named input, along with their associated types and names
This gives us enough information to proceed to the next section on filling out the form, however let's take a look at a couple more things.
What if we wanted to gather all the links?
If we wanted to get just the download URL, we could use something like:
Hmm... it appears that there are two identical links to the download page provided. To make sure we only grab one, we will need to use something like:
There we go!
Now that we have parsed out all of the input types, and have their names, we can proceed with filling out the form information.
Here they are for review:
Username
First let's set the username field.
The username field is named user_login, so let's store that field in a variable named $userNameBox.
To set the username, we can change the value property to our username. I have a credential object which contains my username, and I will use that to set it. You can pass along any string value, but I highly recommend using a credential object, or other secure method (more for the password, but it at also omits even showing the raw string for your username in the code).
You can see that this is set on the website now!
Password
Now to set the password. To do this we'll use the same logic as we did for the username, but specify the name password.
The password object will take a string for the password (just as the username object did). I highly recommend using a credential object, and then using the GetNetworkCredential method, and then the Password property.
Looks good!
Clicking Submit ("Log in »")
We've filled out our username and password, what now? Well... that button ain't gonna click itself! Let's click it via PowerShell.
There is no name set on the object for the button, so we'll use its type for uniqueness, and set the variable to store it that way ($submitButton).
Now to click it! There is a method on this object named Click.
Ok but for real now, let's 'click' it!
And if all goes well, you'll see the next page and be logged in.
Now that we're logged in, we could:
It really all depends on what you want to do.
One thing I have done is wrote up a script for a project where I had to rename user IDs. I had a list of old IDs and new IDs, and would iterate through all of them (sometimes 300+), until they were all renamed. All with logging and results output for later review and verification. Once you have the basics down you can really expand into the realm of full automation, and reap the benefits of what PowerShell can do.
!!**Note**!!
After each time you use the Navigate method, or click a button, you'll want to wait for the object's status to not be busy.
Here is one way to accomplish that.
Store this function in memory:
You can store it in the same script, or as part of a module. You can invoke this function whenever you need to now, and keep your code looking clean.
It's also good to always refresh your $currentDocument variable after a button click / page load.
Here's some code for an example of when/how to use it:
Alright, time to clean things up.
Let's cleanup the mess we made.
First we will log out of the website:
Now that we've verified the URL (and only have one thanks to Select-Object along with -First 1), we can logout by navigating to it.
Now to verify...
Now to clean up the COM object we instantiated.
First we'll quit IE:
Our IE window should have went poof.
If you really want to perform the best cleanup, you can follow that up with this (to release the COM object):
Here's some example code. I took what we accomplished above, and broke parts of it out into functions.
To use the code you'll need an account on the Tukui forums.
Here's what it does:
It will create the text files in the current working directory.
I've provided some links for hints.
I hope you've enjoyed this series.
As always, leave a comment if you have any feedback or questions!
No comments yet. Be the first!