Powershell for testers

Before you "quick start" for working with PowerShell for beginners. You work and one day finally accept the fact that people are inherently lazy creature, but it is quite tricky to make your life easier. And, without hesitation, decide to automate routine tasks. Of course, minimum effort.
I have exactly the same problem, so let's start together. Almost every modern version of Windows already installed "extensible automation tool from Microsoft, consisting of a shell with a command line interface and accompanying scripting language". It uses "classy" of .NET. In practice, this means that we can work with objects.
This, perhaps, finish with the theory and begin to practice. Run the command "Run" and write powershell. A beautiful window nice colors. Almost everything in powershelle done using cmdlets, which are similar to all of us familiar features.
The most important cmdlet is Get-Help. It displays help information. For example:
Get-Help Get-Help
– give help for Get-Help. By the way, the console works the autocompletion by pressing the Tab key.
The console is all well and good, but to write large scripts it is not very convenient. For this is the Powershell ISE.
Started by an analogy: "Execute" — powershell_ise.
See a IDE with debugging and other amenities. In it, we can save our work in the finished scripts with a. ps1 extension.
Write our first script to be stored and are trying to accomplish. Loss-loss — nothing happens. The fact that is disabled by default script execution. Change this by launching PowerShell with admin rights and write:
Set-ExecutionPolicy RemoteSigned
Thereby, setting the script execution policy to allow scripts to run except very very questionable. Not safe, but for the first time will go.
Now it is time to more useful things. For a start we will analyse the logs. Use Get-ChildItem, which, as the name implies, gives us subsidiary, including sub-elements from any folder.
Actually, the location is specified by the parameter Path.
Include — helps us to look at the mask
Recurse mean that we should search subfolders.
As a result, we get something of the form:
Get-ChildItem -Path “D:\Logs” -Include *.log -Exclude "!*" –Recurse
Here we are looking for all files with the extension .log exclude files starting with !, in the folder D:\Logs. For further work need to transfer all the objects that found Get-ChildItem for processing. This is done by the operator | — it's called a pipeline.
Get-ChildItem -Path $input_path -Include *.log -Exclude "!*" -Recurse | select-string -Pattern $text -Encoding "Default" -Context 0,10
Let us examine what we have here has been written: alternately looking at each file that gave us Get-ChildItem line matched variable $text. In this variable we will write the lines that you want to find in the logs. -Encoding need in order for Russian text, if it is in our logs were displayed fine but not the bugs. -Соntext (starting with Powershell version 2.0) brings you a line before and after entering the desired characters.
Now $text. As you can see, variables must begin with "$".
$text = ‘(Fatal|Error|access|)’
Using regular expression we're looking for all rows where there is or Fatal or Error or acces.
The script is in principle ready, but something was missing. We give him a bunch of logs and the output is a jumble of lines. Better to brush conclusion and, if possible, somewhere to keep for further analysis. This will help us a variable $_ roughly speaking, the current object passed to us. In our case this will be a specific log file. For example, $_.FileName — the name of the file $_.LineNumber — the line number where matched our text, and so on. The output is:
$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 needs. For example, it is necessary to determine where the method was carried out too long. We know that in our log the execution time of the method written as — "(128 MS)". So we need to find everything that runs more than 1000 MS. Change the variable $text='(\d{3,} MS)' means, we look for "brackets" for her number, where at least 3 characters, then a space, then the symbols "MS" and another "brackets".
We can find the most frequent error, or method:
the
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 for yourself.
Done with the logs and look at another task — updates of the test sites. Divide into two parts – copying new versions on a test machine, and directly update.
It turns out another remarkable feature of Powershell. From one place we can run scripts that will be applied elsewhere. To do this, simply run the command:
the
Enable-PSRemoting -Force
Setup must be run on two machines — managing and guided. Thus, we include WS (http://en.wikipedia.org/wiki/WS-Management). Checked with the command:
the
Test-WsMan COMPUTER
To the remote computer we can now turn this:
the
Invoke-Command -ComputerName COMPUTER-FilePath "d:\SCRIPT\script.ps1"
Script.ps1 will run on the machine COMPUTER. Thus, using the Start-Process, and other teams who can install our software, we update the test platform.
But before that we need to copy the required file. Will do so:
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”) — the list of our servers,
$source = "c:\files" is the folder where we will copy
$dest = "c$" is the directory where we will copy
a List of inspirational/helpful articles
Jump Start into PowerShell (part I)
Jump Start into PowerShell (part II)
First steps for powersensitive
a Regular expression in Powershell
Useful program for the analysis of regular expressions
Well, perhaps, and all. Stay healthy and learn how to make backups.
Комментарии
Отправить комментарий