Good day, I will continue my information on basic Powershell commands that can help you get further into more complicated scripts or to the very least give you an insight into the logic behind it. Look here if you have not read part 1 yet. Part 2 will continue explaining a few of the most-used and popular fuctions and uses of the Powershell language. #1 - If, Ifelse and Else We can use all three of these commands when comparing multiple objects. “If“ is required while “Ifelse“ and “Else“ are optional. We can see examples in Example 1. “If“ is set up in the way of If(comparison){what to do when the comparison is True}. “Ifelse“ is set up in the same way, which ends up as IfElse(comparison){what to do when the comparison is True}. This needs to used in combination with “If“ because of the creation of logical steps. If the “If” function does not come up with a True.. like shown in example 1 whereby “Test1” does not match “Test2”, it will do 1 of 3 things. 1. If there is an “Ifelse“, it will check the comparisons start at the “IfElse“s and run actions accordingly. After it has gone through the “Ifelse“s, it will go to the “Else“ function (if existing). 2. If there is an “Else“, it will run the actions that are set there. 3. If there is no further “Ifelse“ or “Else“ function, it will do nothing at all. “Else“ happens when all above “If“ and “Ifelse“s have come up False in their comparisons. It will execute the actions. No further events will happen. #2 - $Null $null represents nothing. Unlike some coding languages or real-life situations, nothing can be useful. Example 2.1 shows an example of a simple comparison. “Test1” is a text string and is something, thus the comparison will be obviously False. Example 2.2 however shows 2 scenarios whereby $null can be very useful. Let’s say that you have a chance that a variable is empty because of a failing function or something else (For example, the variable being cleared), by comparing $null to the variable, you can find out whether or not it is empty and have the script react on it. The same goes for $error. As this is the variable that gets all errors saved into it, you can let it check whether there are errors in there and have actions executed depending on the result. #3 - Pipeline A pipeline is created by using the pipe button “|” in between parts of a script. The idea behind it is that all objects you select or use on the left side of the pipe, the script continues using on the right side. In Example 3, it first makes a list of services and filters it on services with a specific displayname. By putting in a pipe, I do not have to make use of any foreach commands, put it into variables or use other selection options. This way I can select something (In this case services starting with Test) and the next command in the pipeline (to the right side of the pipe) will make use of only the selected objects. So in this case it selects all services starting with Test in the display name, sets only those to disabled in the startup type and then stops them. A pipeline always follows the commands from the left to the right and fully executes the piped parts one by one. #4 - Tee-Object Tee-object allows you to output something to both the console output and for example a .txt file (or anything similar). Normally when creating a list of services and then outputting to a file as shown in Example 4, it will simply output to the file and nothing else. When using Tee-Object however it will also show the output on the console itself. This makes it easier when console-output is needed by for example third-party software, rest of the script or visual confirmation for an user or employee. Code Editor
#5 - ForEach-Object
When going through a pipeline, you can have a script ran for every single selected object coming through the pipeline. In Example 5 we can see basic use of this. It retrieves a list of services and for each service it executes 3 commands (Begin, Process and End). These 3 parameters are all executed as separate scripts. You can add more than 1 command in 1 parameter by separating the commands with “;” as seen in the example. In the example it first gets the current date/time, it then puts the name in a variable and write-host’s the name as text string before finishing by getting the current date/time at the point of ending. This can allow for a lot of commands to be executed within a single line of script without having to select objects more than once. #6 - Jobs Jobs allow you to run commands in the background without having to worry about them. This allows you to run multiple commands simultaneous. When you think the commands should be done, you can retrieve their results. In Example 6.1, we start a job called “Test” and let it run 2 commands (As with Example 5, split with “;”) We can later retrieve the results of this job with the Recieve-Job command. The “-Keep” can be used if you want to show/use the job results multiple times, as by default it will remove the job results after retrieving it the first time. There are a good few more parameters possible. More information can be found by using the help command to retrieve more information regarding the job commands. (Ex. Get-Help Start-Job) #7 - Get-Random Allows you to put more randomness in your script. This takes whatever variable, text or other objects that you put in and picks a random one. With Example 7, I let it pick a single line from a text file. #8 - While While allows you to loop one or multiple commands until a certain condition has been cleared. In example 8 we have a counter ($number) that starts at 0 and each time it executes the command, it adds +1. After 10 runs, the $number clears the condition and will not loop any more (This will produce 10 write-host’s). I have used this multiple times with great success. You could for example let it loop a command with a comparison whereby your $error contains an error (and let it clear it at the start of each loop). This way it keeps looping a part of your script until it can execute it with no errors occurring. I use this in my Random Veeam Restore script so it keeps looping through different servers until it finds an OS that allows for a Veeam Restore (Meaning, when a specific command does not create an error). Hopefully any of these commands have given some more insight on the logic going on within scripts. - Powershellder Categories: Powershell, Information, Basics
0 Comments
Leave a Reply. |
AuthorPatrick Berger AKA Powershellder. Categories
All
Archives
December 2020
|
[ i ] Parallax section below. Click on the section below to upload image. Don't worry if it looks weird in the Weebly editor. It'll look normal on your published site.
To edit or delete your image, press the "toggle" button below. Then, hover over your image until a popup appears with the "edit" and "delete" options. If you don't want a white content section, leave it blank. It will disappear on your live website.
