Powershell for testers

Here is a “quick start” for working with PowerShell for beginner testers. You work and one day, finally, resign yourself to the fact that a person is essentially a lazy creature, but at the same time cunning enough to make his life easier. And without hesitation, you decide to automate everyday tasks. Naturally, with minimal effort.

I have exactly the same problem, so let's get started together. Almost every modern version of Windows already has "an extensible automation tool from Microsoft, consisting of a shell with a command line interface and an accompanying scripting language." It uses the "classes" from .NET. In practice, this means that we can work with objects.

On this, perhaps, we finish with the theory and begin to practice. Run the “Run” command and writepowershell . A beautiful window appears in a pleasant color. Almost everything in powershelle is done through cmdlets that have familiar features that are similar to all of us.

The most important cmdlet is Get-Help . It displays reference information. For example:

Get-Help Get-Help

- gives help on Get-Help . By the way, the console runs auto-completion by pressing the Tab key.

The console is, of course, good, but writing large scripts in it is not very convenient. There is Powershell ISE for this.
It starts by analogy: “Run” - powershell_ise .

We see a mini IDE with debugging capabilities and other amenities. In it, we can save our work in ready-made scripts with the ps1 extension.

We write our first script, save and try to execute. Loss-loss - nothing happens. The fact is that script execution is disabled by default. Let's change this - run PowerShell with admin rights and write:

Set-ExecutionPolicy RemoteSigned

Thus, by setting the script execution policy, allow scripts to be run, excluding the very doubtful ones. Not safe, but for the first time it will.

Now it's time for more useful things. First, we will analyze the logs. We use Get-ChildItem , which, as the name implies, gives us children, including nested elements from some folder.
Actually, the folder itself is specified by the -Path parameter .
Include - helps us search by mask,
Recurse- means what to look for in subfolders.

As a result, we get something like:

Get-ChildItem -Path “D:\Logs” -Include *.log -Exclude "!*" –Recurse

Here we look for all files with the .log extension, excluding files starting with!, In the D: \ Logs folder. For further work, you need to transfer all the objects that Get-ChildItem found for processing. This is done by the operator | - it is called a conveyor.

Get-ChildItem -Path $input_path -Include *.log -Exclude "!*" -Recurse | select-string -Pattern $text -Encoding "Default" -Context 0,10

Let's look at what we wrote here: we look in each file one by one for what Get-ChildItem gave us a match string for the $ text variable . In this variable we will write the lines that we want to find in the logs. -Encoding is needed so that the Russian text, if it is in our logs, is displayed normally, but not by crackers. -Context(since Powershell version 2.0) displays the lines before and after the search for the characters you are looking for.

Now about $ text . As you have already noticed, variables must begin with the “$” character.

$text = ‘(Fatal|Error|access|)’
Using a regular expression, we look for all lines where there is either Fatal or Error or acces.

The script, in principle, is ready, but something is missing. We give him a bunch of logs and at the end we get a hash of lines. It is better to comb the conclusion and, if possible, save it somewhere for further analysis. The variable $ _ will help us with this - roughly speaking, the current object passed to us. In our case, it will be a specific log file. For example, $ _. FileName is the file name, $ _. LineNumber- the line number where our text matches, and so on. At the output we get: > - writes the output to the specified file. You can modify our script for various needs. For example, you need to determine where the method has been running for too long. We know that in our log the runtime of the method is written like this - "(128 ms)". Therefore, you need to find everything that runs more than 1000 ms. Change the variable $ text = '(\ d {3,} ms)' - this means we will look for a “bracket”, followed by a number, where at least 3 characters, then a space, then the characters “ms” and another “bracket” . We can find the most common error, or method: How it works, I think you will guess for yourself.

$text = ‘(Fatal|Error|access)’
Get-ChildItem -Path $input_path -Include *.log -Exclude "!*" -Recurse |
select-string -Pattern $text -Encoding "Default" -Context 0,10 |
foreach {@($_.FileName), @($_.LineNumber), @($_.Line), @($_.Context.PostContext)} > $output_file

Select-string -Pattern "data\d$" -Path input.txt | Group-Object Line | Sort-Object Count -Descending | Select Count,Name -First 2 > out.txt

Finish with the logs and consider another task - updating the test sites. We divide it into two parts - copying new versions to test machines, and directly updating.

This is another great Powershell property. From one place we can run scripts that will be used in other places. To do this, it is enough to execute the command:

Enable-PSRemoting -Force

Setup must be performed on two machines - the control and the managed. Thus, we include WS (http://en.wikipedia.org/wiki/WS-Management). It is checked by the command:


We can now access the remote computer like this:

Invoke-Command -ComputerName COMPUTER -FilePath "d:\SCRIPT\script.ps1"

Script.ps1 will be executed on the COMPUTER machine. Thus, using Start-Process , and other commands that can install our software, we will update the test site.

But before that, we need to copy the necessary file. Let's do it like this: Where, computers = @ (“COMPUTER”, “COMPUTER1”, “COMPUTER2”) is the list of our servers, $ source = “c: \ files” is the folder from which we will copy, $ dest = “c $ "- the directory where we will copy

foreach ($computer in $computers) {
if (test-Connection -Cn $computer -quiet) {
Copy-Item $source -Destination \\$computer\$dest -Recurse
} else {
"$computer is not online"

List of inspirational / helpful articles

Jump Start in PowerShell (Part I)
Jump Start in PowerShell (Part II)
First Steps for Powershell
Regular Expressions in Powershell
Useful Program for Regular Expression Analysis

Well, that’s probably it. Be healthy, and more often do backups.

Also popular now: