📜 ⬆️ ⬇️

Powershell for testers



Before you "quick start" to work with PowerShell for beginners testers. You work one day and finally come to terms with the fact that man is essentially a lazy being, but tricky enough to make his life easier. And, without hesitation, decide to automate everyday tasks. Naturally, with minimal effort.

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

On it, perhaps, we will finish with the theory and we will start practice. Run the command "Run" and write powershell . A beautiful window of a pleasant color appears. Almost everything in powershelle is done through cmdlets that have familiar functions that are similar to all of us.
')
The most important cmdlet is Get-Help . It displays background information. For example:

Get-Help Get-Help

- will issue a certificate for Get-Help . By the way, in the console, auto-completion works by pressing the Tab key.

The console is, of course, good, but it is not very convenient to write large scripts in it. For this there is a Powershell ISE.
Runs by analogy: “Run” - powershell_ise .

We see a mini IDE c debugging feature and other amenities. In it, we can save our works into 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 scripts are disabled by default. Let's change this - run PowerShell with admin rights and write:

Set-ExecutionPolicy RemoteSigned

Thus, by setting the script execution policy - to allow running scripts, excluding absolutely dubious ones. Not safe, but for the first time will go.

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 items from some folder.
Actually, the folder itself is specified by the -Path parameter.
Include - helps us search by mask,
Recurse - means that you need to search in nested folders.

As a result, we get something like:

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

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

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

Let us examine what we have written here: in turn, we search in each file in turn for what Get-ChildItem gave us a string of matches for the $ text variable . In this variable, we 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 krakozyabrami. -Context (starting with Powershell version 2.0) displays the lines before and after the entry of the required characters.

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

$text = '(Fatal|Error|access|)'
Using a regular expression, we search for all strings where there is either Fatal or Error or acces.

The script, in principle, is ready, but something is missing. We give it a bunch of logs and get a jumble of lines at the output. It is better to comb the output and, if possible, save somewhere for further analysis. The $ _ variable will help us in 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 is the line number where our text matched, and so on. At the output we get:

$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


> - 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 method execution time is written like this - "(128 ms)". Therefore, it is necessary to find everything that runs more than 1000 ms. We change the variable $ text = '(\ d {3,} ms)' - this means we will look for a “bracket”, followed by a number, where there are at least 3 characters, then a space, then the symbols “ms” and one more “bracket” .

We can find the most common error, or method:

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

How it works, I think you can guess it yourself.

Let's finish with logs and consider another task - updating test sites. We divide into two parts - copying new versions to test machines, and directly updating.

It turns out another great Powershell property. From one place we can run scripts that will be used in other places. To do this, just run the command:

Enable-PSRemoting -Force

The configuration must be performed on two machines - the manager and the managed one. We thereby enable WS (http://en.wikipedia.org/wiki/WS-Management). Checked by the command:

Test-WsMan COMPUTER

We can now access the remote computer as follows:

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

Script.ps1 will run on the COMPUTER machine. Thus, with the help of 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:

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


Where computers = @ (“COMPUTER”, “COMPUTER1”, “COMPUTER2”) is a list of our servers,
$ source = "c: \ files" - the folder from which we will copy,
$ dest = "c $" - the directory where we will copy

List of inspirational / helpful articles


Jump Start in PowerShell (Part I)
Jump Start in PowerShell (Part II)
First steps for pauershelshikov
Regular expressions in Powershell
A useful program for analyzing regular expressions.

Well, perhaps that's all. Be healthy, and make backups more often.

Source: https://habr.com/ru/post/252957/


All Articles