read.cash Log in
@Otek

Software Testing - create a PowerShell script part 3 Hi all, Welcome to another episode of the Software Testing series. Today, we will continue our work with PowerShell script that allows us to automatically create a new Windows user. If You miss that - check the first episode of the series: https://read.cash/@Otek/software-testing-create-a-powershell-script-part-1-bb9d9df4 . Last time when We finished our script look s that way: <# .DESCRIPTION Create a new Windows User. .EXAMPLE C:\PS> Set-ExecutionPolicy remotesigned C:\PS> Import-Module createNewUser.ps1 C:\PS> CreateUser -Name TestUserScript1 #> So let's go further with that, :) Our next lines will look like in the code block above: function CreateUser { [CmdletBinding(SupportsShouldProcess=$true)] param( # Specifies a user name [Parameter(Mandatory=$true, Position=0, ParameterSetName="Name", HelpMessage="Name of new User")] [Alias("UserName")] [ValidateNotNullOrEmpty()] [String] $Name ) } Like always, we will discuss it line by line. function CreateUser { First-line start our function. Each script can have multiple of them. Our function name is: CreateUser (it is a good practice to the naming of the function that will clearly show what that function will do). Whole lines between {} will belong to CreateUser function. Next line: [CmdletBinding(SupportsShouldProcess=$true)] is our function attribute. The CmdletBinding attribute is an attribute of functions that makes them operate like compiled cmdlets written in C#. It provides access to the features of cmdlets - so in a short words it allows us to create more complicated scripts and use a cmdlts functions which are very useful. param( Here we specify what parameters our function will have and have they will work. # Specifies a user name Here we just create a comment to someone who will use our script later - it just informs what parameter we specify above - it can be extremely useful when our script will have more of them. [Parameter(Mandatory=$true, Next line we will start set attributes of our function Parameter. Each of them are separated by ",". The first one is **Mandatory=$true** here we decide if our parameter will be mandatory (values that we can set are true and false) - in our case it should be mandatory - cause script needs to know with what name he should create a new user. **Position=0,** that one is very useful when the function will have multiple parameters. If the user gives them e.g. CreateUser 'firstParameter' 'secondParameter' position 0 means that script will take 'firstParameter' here. **ParameterSetName="Name",** - here we give our parameter name :) **HelpMessage="Name of new User")] -** if script user will use help function, information we set here will display to him :) It is important to not forget to end the whole Parameter section with "]" character. [Alias("UserName")] Here, we set an alias for our parameter. It is an alternate name or nickname for a cmdlet or for a command element, such as a function, script, file, or executable file. You can use the alias instead of the command name in any PowerShell commands. [ValidateNotNullOrEmpty()] The ValidateNotNullOrEmpty attribute specifies that the parameter value can't be $null or be empty as well. If that happened PowerShell generates an error. [String] That is very important part - here we decide what format or more what type parameter should be. It can be a numeric value, text, table etc. In programming convention String value will treat all parameters provide by user as a text. $Name Here is just define of how our parameter will be called in our script. Ok, let's sum that all up.

Today we created the function **CreateUser.** It got one **mandatory** parameter which name is **"Name"** and it is a text (**String**) type. Next time we will use that parameter in our script and create a new user based on it :)

No comments yet

Log in to join in Reading is open to everyone. Replying needs an account.