📜 ⬆️ ⬇️

How to easily start writing on PowerShell or simple automation to manage Active Directory

image

Learn the basics of PowerShell


This article is a text version of the lesson from our free video course PowerShell and Active Directory Basics (for full access, use the secret word "blog").

This video course turned out to be extremely popular around the world and it will take you through all the steps to create a complete set of tools for managing the Active Directory directory service from the very beginning.

PowerShell Encoding


At first, this may seem like a difficult task to start working with PowerShell, especially if over the years you have become accustomed to working with the cmd.exe command line and the so-called batch files (files with the .bat and .cmd extensions). In this article, written on the materials of the second lesson of our video course, we will explain how and why you should update your skills with PowerShell, as well as analyze the basics of launching the PowerShell editor, learn how to auto-complete commands and how to get up to date in any situation help and examples.

Team Execution


image
')
PowerShell is an interactive environment that allows you to run various commands in real time. You do not need to edit the script in Notepad first and only then run it on the command line, which also saves you time.

If you now work in any organization that does not exist for more than one day, then you probably already have several small scripts for each day that you run from the cmd.exe command line. And this is great news! So you can just as easily do all this from PowerShell. It was truly a wise design decision from Microsoft, so they made the transition to a new solution easier for administrators.

image

In appearance, the PowerShell editor looks and functions exactly the same as the cmd.exe command line environment. Techniques and skills that you already own will work without changes in PowerShell. And if you also want to improve your skills and are working on the transition from performing one-time tasks, in order to provide more automated administration, then the habit of running PowerShell rather than the command line is a great way to start.

All of your commonly used utilities, such as ping, ipconfig, nslookup, and so on, will work exactly as you expect.

image

How to find PowerShell commands


People love PowerShell because it’s so, well, powerful! But this power comes from a completely insane amount of built-in features. It's just not possible, but probably not practical, for someone to remember all sorts of commands, cmdlets, flags, filters, and other ways to tell PowerShell what to do and how.

Fortunately, several tools are built right into the editor to help you cope with this.

Auto-completion of commands by pressing Tab

No need to memorize various commands or the exact spelling of a command. Dial

get-c 

Now by pressing the Tab key - you can go through all the available commands, starting with the characters that you have already entered. And it works in any part of the body of the command to which you are trying to refer: in the names and flags of commands and even when auto-completing full paths on the file system.

image

Get-Command

Even though Tab completion is excellent, what happens if you don’t know the correct command name you need? In this case, you can use the command to find other available commands: Get-Command.

In finding the name of a command, it is important to keep in mind that there is a syntax for them: Verb-Noun. As a rule, Verbs such as - Get (Get), Set (Set), Add (Add), Clear (Clear), Read (Read) and Write (Write) and Nouns - files, servers or other entities in your network and applications .

Get-Command is a tool for finding and learning the commands available on your computer.

image

Command Syntax in PowerShell


Someone once described Perl's scripting language as “noise from executable lines of code” (orig. “Executable line noise”) is an incredibly useful tool, with a wildly opaque syntax and accordingly a high input threshold for learning.

Although if you figure it out and the traditional command line in Windows is not so far gone from this. Consider the general problem of how to find all the files in a directory whose names begin with 'foo'.

 CMD: FOR /D /R %G IN (“Foo*”) DO @ECHO %G 

FOR and DO indicate that this is a loop.
The / D flag indicates that this is a cycle across all folders.
The / R flag indicates that all files and folders should be included, including subdirectories.
A search pattern that identifies a set of files of interest to us is denoted by “IN”
ECHO indicates that the script should display the result of each cycle and, finally,
% G is a “forced parameter” and is chosen because Microsoft programmers have previously used the letters A, D, F, N, P, S, T, and X when developing. Therefore, starting with G is a good tone, t. to. this gives you the largest set of unused letters for the path formats of the variables returned (G, H, I, J, K, L, M) - in other words, it is a life hack.

Compare now the equivalent on PowerShell:

 PowerShell: Get-ChildItem -Path C:\ -Filter 'Foo*' 

The result is the same, but even with such a rather trivial example, it will be much easier to understand what is happening. It becomes immediately obvious what each element in the team is doing and how they can be changed. Well, except that the wildcard '*' is used in both examples, but I think that you already know very well that it means that all elements must begin with 'Foo' and at the end something else.

Feel like your mood is gradually improving? And now, what if you want to know how to select only files (and not folders) on the way? Can you dig into the manuals, or your best friend Google to help you, or can try to figure out from the command line? A small hint: if you are in PowerShell, enter “-” and press the Tab key, go through the necessary flags until the obvious solution appears.

image

One Long Line vs. Object


Sites are not needed if they are not online. That is why people spend a huge amount of time pretending that they are sonar operators on a submarine and “ping” the availability of their servers (yes, that's why it is called that ).

Although the output of the Ping command is useful (and you can use ping with the same success in the PowerShell console), in the end it’s just a big and long line — a series of letters and numbers with some breaks in between.

PowerShell has a command that is similar to Ping, but returns data in a structured way. This is the Test-Connection command.

Below is the result of the work of this packet exchange command with the 'DC' server in a completely different, fully structured form:

image

Not to mention the fact that it is much easier to read, but the main thing is that now you can pass this information to the entrance to another team, give it even greater utility (our full video course is designed for this purpose) or simply set it up so that it made more sense.

Built-in help


image

Up to this point, we have focused on executing certain commands using the Tab supplement, but as soon as you start working with PowerShell, more and more commands become more and more complex with even more complex parameters. While the Verb-Noun syntax (Verb-Noun) helps, but the presence at hand helps:

1. Current documentation
2. Abundance of examples

Help by cmdlets


In practice, you should combine the Get-Command command (to find what you should use), and then use Get-Help to learn how to use this particular command.

A practical example of how to do this: let's say you need to determine all the running Windows services on a computer.

For starters, you can search for commands to interact with services:

 Get-Command service 

Which will tell you immediately that you are on the right track. When thinking about returning to the standard syntax of PowerShell Verb-Noun, you would like to find out how to use the 'Get-Service' command correctly.

Microsoft Documentation on the Get-Service Command
To do this, use the command 'Get-Help'. Start typing
“Get-Help -” and then press Tab
You will quickly find the available options, most clearly one “Name” fits, so it's worth a try:

 Get-Help -Name Get-Service 

Here you will get the full syntax of the command (and that you can include or exclude from the options on the basis of filters).

image

If you want to explore the command more deeply and detail it down to each parameter, then type:

 Get-Help -Name Get-Service - Parameter Name 

Help with PowerShell examples


We are all human and, without being offended by the Google bot, we all have our own psychological obstacles that we need to overcome when we learn something unfamiliar and bring it into a form that we can understand in order to accomplish what we need.

Entering “-examples” or adding the “-detail” flag to the “Get-Help” command will give you a set of examples for using the command.
Here, for example, the output for the command:

 Get-Help -Name Get-Service -Examples 

image

Staying up to date


What could be more awful than a non-working example, or an example with an error in the documentation. Often this happens either because of outdated documentation, or because of incorrect examples, or because of updated libraries.

To get around these problems and at the same time get new examples and fixes, type:

 update-help 

and start the process of downloading the updated contextual help.

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


All Articles