The last time we started to writing our first PowerShell script - well to be more precise we actually don't write anything yet, but we are preparing for that. If You missed that first part, You Can get it here: https://read.cash/@Otek/software-testing-create-a-powershell-script-part-1-bb9d9df4.
So let's start with our first script - it will create a new Windows user :) So first of all let's of course run Windows PoweShell ISE.
Let's start our code with some kind of description:
<# .DESCRIPTION Create a new Windows User. .EXAMPLE C:\PS> Set-ExecutionPolicy remotesigned C:\PS> Import-Module createNewUser.ps1 C:\PS> CreateUser -Name TestUserScript1 #>
As You can see the description block starts with <# and ends with #>. For PowerShell it means that everything between those <# #> characters is some kind of comment and should be ignored when the script is running. Even if that part isn't executed, it can be very helpful for other people that will use our script - that is a reason why it is a good practice to have such a description in our scripts.
Description - just describe what is the idea behind our script and what it should do. Example shows a user how to run our script - what those lines mean?\
C:\PS> Set-ExecutionPolicy remotesigned
That lines Set execution policy. In other words, it set with what permissions should our script be run. It is a safety feature that controls the conditions under which PowerShell loads configuration files and runs scripts. This feature helps prevent the execution of malicious scripts on our machine.
There are a few of them to choose: AllSigned, Bypass, Default, RemoteSigned, Restricted, Undefined, Unrestricted. If we don't set any of them by default, it will use a Default execution policy. Because we want to run our script also on Windows server version we will choose RemoteSigned :)
Each of they is described with details here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.1
C:\PS> Import-Module createNewUser.ps1
The second line shows how import our created script. After command Import-Module we should give our script name - look at the 'ps1' file extension - that is a standard PowerShell file extension.
C:\PS> CreateUser -Name TestUserScript1
The last line in our Example block describes how to run our script. 'CreateUser' is a name of a function that we want to run. Script Can have multiply functions - so we need to give PowerShell which one he should run.
After the name of a function, there is a parameter called '-Name'. Again every script could have many parameters, some of them Can be mandatory others not. Our first script will have just one mandatory parameter, '-Name'. 'TestUserScript1' is a place where users Can give info on what name should have a newly created user.
So to sum it up, when someone will run our script exactly in the way like in our Example it will create a new Windows user named TestUserScript1.
How our script should like at this moment? Similar to that one screenshot below:
Those eight lines are a nice start to our script - isn't it? :) To be honest it still isn't do anything, but we are one step further to that :)
You already learn what is an execution policy and why it is so important. You also know how to import our script and run it with some parameters. So, let meet in next episode of Software Testing series when we will add first function in our script :).