Testing a web server with PowerShell

Here’s another little PowerShell script that might be useful. This was knocked up to provide some basic load testing on the search function of a web application

I had written- an ASP.NET form with a text field and submit button. It continuously visits the search page on the website, and submits the search term (“Bob” in this case). The script can be run multiple times simultaneously to simulate several users hitting the search at the same time, it could even be used as the basis of something on a much bigger scale if you want.

This is the core of the script- it calls the page and runs the search function with the term “Bob”.

1$webrequest = invoke-webrequest -uri http://mywebserver/search.aspx
2$form=$webrequest.Forms.Item("form1")
3$form.fields.Item("txtSearch")="Bob"
4$webrequest = invoke-webrequest -uri ("http://mywebserver/search.aspx" -f $form.Action) -Body $form.fields -Method $form.Method

Below is the script in full- to use it just change http://mywebserver/search.aspx to the URL of your search page. You’ll also need to update form1 and txtSearch to the name of the HTML form and text box in your particular webpage. It will also prompt for Credentials (my particular site required a login to access). The script writes out to the console window the number of searches performed and the time (in seconds) the last request took to return (using Measure-Command). Once going it will run continuously until Ctrl-C is pressed, or the PowerShell window is closed.

 1$MyCredentials= Get-Credential
 2$Counter=0
 3while(1 -ne 0) {
 4$Time=Measure-Command {
 5$webrequest = invoke-webrequest -uri http://mywebserver/search.aspx -Credential $MyCredentials
 6$form=$webrequest.Forms.Item("form1")
 7$form.fields.Item("txtSearch")="Bob"
 8$webrequest = invoke-webrequest -uri ("http://mywebserver/search.aspx" -f $form.Action) -Body $form.fields -Method $form.Method -Credential $MyCredentials
 9$Counter=$Counter+1
10}
11write-host $Counter $Time.TotalSeconds "seconds"
12}

This script was helpful in my particular bit of troubleshooting, feel free to take it and use or modify it yourself- just don’t break anything and blame me :)