PowerShell, Selenium and Browser Automation

Selenium is a portable software-testing framework for web applications.  It’s pretty cool (in a geeky way).  It’s primarily used to test web applications, but in this instance we’re using it to launch Internet Explorer, load a web-based helpdesk dashboard on the intranet, log into it and click a few buttons to customise the view.

You’ll need to download IEDriverServer.exe.  Go to this location:

http://selenium-release.storage.googleapis.com/index.html

Choose the folder for the most recent version, and download IEDriverServer_Win32_[version].zip

Then download Selenium.WebDriver Nuget package from here: https://www.nuget.org/packages/Selenium.WebDriver/

and Selenium.Support Nuget package from here: https://www.nuget.org/packages/Selenium.Support/

Rename the extensions of each file from .nupkg to .zip, and extract them as you would normally.  If you then look in the Lib folder of each archive you will find:

WebDriver.dll and WebDriver.Support.dll.

An Example of Browser Automation

Thi example demonstrates Internet Explorer browser automation since it’s probably the most prevalent browser in corporate environments.  However Selenium does have libraries for Firefox and Chrome too.

So for this example, put IEDriverServer.exe, WebDriver.dll, WebDriver.Support.dll and this PS1 file all in the same folder.  Read the inline comments in the script for an explanation.  To get the website element IDs you’ll need to use the DOM explorer of the browser, but I won’t go into detail of how to do that here.

 

Invoke-Sqlcmd returning unwanted columns such as RowError, HasErrors

I’ve been performing some SQL queries recently using PowerShell and Invoke-SqlCmd.  Here is a simple example of returning a list of devices (a single column of data) from a database:

Of course I pipe this into Export-CSV like so:

When we read the exported data in the CSV we expect one column of data called ‘Device’.  However, instead we can also see columns called RowError, HasErrors and others!  And whilst I’m not sure why these appear, we can omit them like so:

and of course if we wanted to pipe this into a CSV we can do so like this:

And voila.  We only get the columns of data that we asked for!

Manipulate Column Data with Select-Object and PowerShell

A couple of days ago I wanted to check which products were installed on a computer using PowerShell.  And throughout my journey I discovered how we can dynamically maniuplate column data and format the output.  Take this basic example where we query the ‘Uninstall’ registry key and output the registry data for displayname, displayversion, installdate:

But what if we wanted to join/concatenate the displayname and displayversion columns into one column?  Or what if we wanted to give the column a custom name?  We can do so like his:

So we’ve joined the displayname and displayversion columns into one column called ‘Name and Version’.  Great.  Now by default the installdate registry data is stored in the yyyyMMdd format, or,  as an example 20180807.

This isn’t very readable.  So we can also manipulate this format (ok, we don’t do any casting as a date in this example – just basic string manipulation) and call the column ‘Formatted Date’ like so:

This was quite a crude example, but hopefully it demonstrates how we can manipulate column data dynamically using PowerShell.

Wait For Process in a Batch File

A while ago I needed to wait for a process to end in a batch file, before the script continued.  I did it by using an If statement to create a ‘spoof’ loop, with a delay between each loop iteration of 5 seconds.  This example waits for the ‘Java.exe’ process to end before the script continues:

 

Move or Copy AD users

This is a simple PowerShell script to either copy AD users or move AD users from one AD group to another:

Move AD Users

Copy AD Users

 

Add And Remove Services Entries

This is a quick script I’ve used to add and remove entries to the windows services file. It ignores commented lines (lines starting with a hash (#)), and it also doesn’t validate the format of the host or port/protocol so use with care!

Usage

cscript.exe services.vbs “Add” “localhost” “8080/http”
cscript.exe services.vbs “Remove” “localhost” “8080/http”

and here’s an example of how it can be called from another vbscript:

 

Install and Uninstall an MSI using PowerShell

This is an example of how to install and uninstall an MSI using PowerShell.

It passes in a string array as the msiexec arguments. So you can add more arguments as you see fit. Pay close attention to the quotes around the file paths (in case they contain spaces).

 

Add and Remove Hosts Entries

This is a quick script I’ve used to add and remove entries to the windows hosts file. It ignores commented lines (lines starting with a hash (#)), and it also doesn’t validate the format of the host or IP address so use with care!

Usage

cscript.exe hosts.vbs “Add” “10.10.10.10” “example”
cscript.exe hosts.vbs “Remove” “10.10.10.10”

 

Search for a File Inside a Merge Module

Description

Sometimes I need to find if a file exists inside a merge module.  Since I don’t know which specific merge module I’m looking for, I wrote this script to iterate through every merge module in a specific folder to find a specific file in the File table.

Usage

CScript.exe {Script} {FileToSearchFor}

 

Clean Up the Windows Start Menu

Sometimes after an install or an uninstall we want to clean up the start menu – usually this involves removing shortcuts to uninstall the application, shortcuts that link to help files and/or shortcuts that attempt to install additional components.  This tidy script (tested on Windows 7) can be used to populate file(s) and folder(s) names that require deleting.