9. Manipulate IE with Powershell
$a = new-object –com "Internet Explorer"
$a.visible = $true
# $a.silent = $true
$a.navigate http://www.somewebsite.com
function wait{
start-sleep –milliseconds 100
}
$b = $false
do{
while($a.busy –or $a.readyState –lt 4){wait}
$c = "myElementID"
$d = $a.document
$e = $d.IHTMLDocument3_getElementById($c)
if($e –ne $null){
$f = "(function(a, b){"
$f += "var c = document.createEvent('CustomEvent');"
$f += "c.initCustomEvent('change', !0, !1, undefined);"
$f += "$(a).val(b)[0].dispatchEvent(c);"
$f += "})('#$c', 'password')"
$d.parentWindow.execScript($f, "javascript")
}
if($e.value.length –gt 0){$b = $true}
}until($b –eq $true)
Opens Internet Explorer and goes to
http://www.somewebsite.com
The code keeps checking 10 times per second
whether the page has fully loaded or fully
rendered
When the page has finished rendering it
searches for a text field with id =
“myElementID”
Then it sets “password” as the value of that
text field and fires the change DOM event
@albangerome
10. Manipulate IE with Powershell
$a = new-object –com "Internet Explorer"
$a.visible = $true
# $a.silent = $true
$a.navigate http://www.somewebsite.com
function wait{
start-sleep –milliseconds 100
}
$b = $false
do{
while($a.busy –or $a.readyState –lt 4){wait}
$c = "myElementID"
$d = $a.document
$e = $d.IHTMLDocument3_getElementById($c)
if($e –ne $null){
$f = "(function(a, b){"
$f += "var c = document.createEvent('CustomEvent');"
$f += "c.initCustomEvent('change', !0, !1, undefined);"
$f += "$(a).val(b)[0].dispatchEvent(c);"
$f += "})('#$c', 'password')"
$d.parentWindow.execScript($f, "javascript")
}
if($e.value.length –gt 0){$b = $true}
}until($b –eq $true)
Every variable must start with $. That’s how
Powershell distinguishes between functions, also
called cmdlet, and variables.
You assign a value to a variable using the equal
sign. You cannot use the equal sign to test when
two variables are equal
You can concatenate strings with the plus sign
and build a string over several lines using the
plus-equal combo (+=)
You can include a variable inside a string without
needing extra string concatenating code
@albangerome
11. Manipulate IE with Powershell
$a = new-object –com "Internet Explorer"
$a.visible = $true
# $a.silent = $true
$a.navigate http://www.somewebsite.com
function wait{
start-sleep –milliseconds 100
}
$b = $false
do{
while($a.busy –or $a.readyState –lt 4){wait}
$c = "myElementID"
$d = $a.document
$e = $d.IHTMLDocument3_getElementById($c)
if($e –ne $null){
$f = "(function(a, b){"
$f += "var c = document.createEvent('CustomEvent');"
$f += "c.initCustomEvent('change', !0, !1, undefined);"
$f += "$(a).val(b)[0].dispatchEvent(c);"
$f += "})('#$c', 'password')"
$d.parentWindow.execScript($f, "javascript")
}
if($e.value.length –gt 0){$b = $true}
}until($b –eq $true)
Booleans: $true, $false, $null
Operators:
• -or: or
• -ne: is not equal to
• -eq: is equal to
• -lt: is less than
• -gt: is greater than
• -match: does a regular expression match
• and many more
@albangerome
12. Manipulate IE with Powershell
$a = new-object –com "Internet Explorer"
$a.visible = $true
# $a.silent = $true
$a.navigate http://www.somewebsite.com
function wait{
start-sleep –milliseconds 100
}
$b = $false
do{
while($a.busy –or $a.readyState –lt 4){wait}
$c = "myElementID"
$d = $a.document
$e = $d.IHTMLDocument3_getElementById($c)
if($e –ne $null){
$f = "(function(a, b){"
$f += "var c = document.createEvent('CustomEvent');"
$f += "c.initCustomEvent('change', !0, !1, undefined);"
$f += "$(a).val(b)[0].dispatchEvent(c);"
$f += "})('#$c', 'password')"
$d.parentWindow.execScript($f, "javascript")
}
if($e.value.length –gt 0){$b = $true}
}until($b –eq $true)
Controlled loops:
• while – keeps looping until the expected
condition is no longer true
• do-until – keeps looping until the expected
condition is true
• for – loops for a predefined number of times.
The loop can break on demand if a specific
condition is met
• foreach – loops through each item of a list
Powershell can execute Javascript, jQuery code
(if the page has loaded jQuery) or a blend of both
vanilla Javascript and jQuery. That code can
simulate browser actions such as clicks on
specific buttons for example
@albangerome
13. Manipulate IE with Powershell
$a = new-object –com "Internet Explorer"
$a.visible = $true
# $a.silent = $true
$a.navigate http://www.somewebsite.com
function wait{
start-sleep –milliseconds 100
}
$b = $false
do{
#step 1
}until($b –eq $true)
do{
#step 2
}until($b –eq $true)
do{
#step 3
}until($b –eq $true)
…
You can create complex scenarios where each
do-until block targets manipulates a page
element such as:
• a link
• a text field
• a drop-down
… and assign an action to this element such as:
• a click
• passing a value
• selecting a pre-existing value
@albangerome
14.
15. What I use Powershell for
• Takes screen shots
• Create, read, update, delete, rename, move, zip, unzip files
• Make IE go through minute scenarios and interact with the pages
• And so much more I have not tried yet
• Send emails
• Start Excel, Word, Powerpoint and start VBA macros in them
• Interface with a database and run SQL on it
@albangerome
16.
17. The good and the bad
I don’t like so much:
• The syntax can be complex but I am on the
start of my learning curve still
• Somewhat cryptic error messages
• Some commands require adding extra
brackets to clarify the scope of what the
function executes to
• Powershell books are often humongous
• Some Powershell scripts require running
them in admin mode which may be disabled
• Admins often disable features so your
mileage may vary
I like:
• Short command names (often <5 letters)
• Command-chaining aka piping
• Flexible, case-insensitive syntax
• You can call functions with or without
brackets around the parameters
• You can create your own functions
• It comes preinstalled on Windows machines
• It can save you a ton of time and reduce
human error drastically
@albangerome
18.
19. Starting Powershell
@albangerome
• Type powershell in the search bar near the start menu
• Start MS-DOS by typing cmd and then powershell
• Searching for powershell.exe – there are several versions