
We can apply the same logic and figure out the path in our script based on the switch decided by the end-user. If the user tries to use two parameters and leaves the VMName parameters out of the picture, the mandatory option will protect us. We performed all possible scenarios, and we see the results in action. If we see the number of parameters higher ( gt meaning Greater Than) two, the assumption is that the end-user entered -protect and -restore. We forced that as mandatory, so the user can technically add another two parameters. We know for a fact that one parameter is going to be the VMName. If we know that we have a hash table with all the data that is coming from the command line.

As you can see in the image below, every parameter used will be added to the hash table with its value. We added as the first line of our script to see what kind of information we can gather when using it. We can take advantage of $PSBoundParameters to control what the user is doing from the command line. Our next step is to understand what the end-user is providing for backup/restore options. Param (Īt this point, we have a robust $VMName in-place. If it does not, then we don’t need to even bother to execute the script.
#INPUT PARAMETERS PYCHARM WINDOWS CODE#
In the code below, we are checking to see if the VM actually exists. Param (Ī simple and even cooler example is to combine with real-time validation of your environment in Microsoft Azure. The content is the same that we have been using, just in a different format. We are adding too many attributes to our $VMName, so an excellent way to organize is to add one attribute per line. Let’s say that we don’t have a VM name with less than five (5) characters, we can use to validate that during the input of the parameters, and that will save us some line of code in the script to validate down the road. We can use smart validations using ValidateScript attribute. Validating the PowerShell parameter on the fly When the user types – and hits, only the parameter names will be displayed. Note: The alias does not show up on the auto-complete. As long as some of those are typed, they are all going to be stored in the $VMName variable. In our example below, I added virtualmachine, machine, alien and ChupaCabra. We can assign several names that we believe our end-users may use instead of VMName as a parameter.

Some users have their way to refer to an object, and you can accommodate those types of requests using Alias. Using alias to support end-user requirements The script code and the action that we have just described is depicted in the image below. However, we can type !? and the HelpMessage that we configured will be displayed. Param (Īs an end-user, when you run the script, and no parameters are provided, the VMName will be required (because we configured as mandatory). If you are a good person and want to perform a good deed for the users of your code, you can use the HelpMessage to provide a short explanation of what you are looking for on those specific parameters. We can force a parameter to be mandatory by performing a slight change at the beginning of the line that we have defined the $VMName in the previous section. Otherwise, we can’t even start our operations. If I do the same thing for a switch parameter (-protect in our case), everything goes well. When I try to pass -VMName without any string, the script throws me an error. We can see how it feels to the end-users when using switch and string. In the example below, we want a string for the $VMName, and protect and restore will be a switch type. We can start simply by defining the name of the parameters and defining their types. Our design is that besides the VM name, the user has to provide either backup or restore. This section must be at the top of your script. The first thing that we have to do is to define the Param() section. Getting started with your Param() section However, both scripts would share some standard functions, and it is much easier and practical to maintain a single script that performs both activities in the same code. We could create a script for every action (backup and restore).

Although we are not going to code anything within the script, let’s use a simple scenario that we have a script to protect Azure VMs, and we want the user to provide the virtual machine name and if he or she wants to protect or restore any given VM.

We are going to offer some simple scenarios that we can address at the parameter section of the script.
#INPUT PARAMETERS PYCHARM WINDOWS PROFESSIONAL#
PowerShell has several options to help the IT professional control the input and save tons of code from being validated during the execution time. When coding your PowerShell scripts, an essential part is the definition and validation of the parameters that are required to make it run properly.
