Learn PowerShell (my way)!! Part 2


Lesson 2 : Variables, Parameter/Arguments

“Thumb rule” for my scripts, I do use variables as much possible I can, so that script will not be tough for someone to decode it themselves! Use variable for reading, displaying, passing values within my scripts. All variables preceded with a $ sign

There are automatic and constructed variable will be used in powershell. Constructed variables used for storing, passing, values with in PowerShell scripts, name variables after your values (char, int or strings etc) and requirement.

For ex: for storing username and computer I usually use $username and $computer

$number = 1               —–(saves value 1 to $number)

$Computer = $env:COMPUTERNAME               —–(Local computer name saves to $computer)

$version = Get-WmiObject win32_operatingSystem -ComputerName $Computer | select -ExpandProperty Version               —–(Get version of computer using $computer variable created earlier and save it to $version)

Other one is system variable which are default static available within PowerShell called automatic variables

$$     –    Display the last command in the last line received by the session.

$?     –    Display the execution status of the last operation. It is either TRUE or FALSE.

$ARGS     –    Contains an array of the undeclared parameters or values that are passed to a function or script block

$ERROR     –    Displays an array of error objects that represent the most recent errors. The most recent error is the first error object in the array, retrieve using $Error[0]

$FALSE     –    Contains FALSE. You can use this variable to represent FALSE in commands and scripts instead of using the string “false”

$TRUE     –    Contains TRUE. You can use this variable to represent TRUE in commands and scripts.

$FOREACH     –    Contains the enumerator (not the resulting values) of a ForEach loop

$HOME     –    Contains the full path of the user’s home directory

$MATCHES     –    The $Matches variable works with the -match and -notmatch operators.

$NULL     –    $null is an automatic variable that contains a NULL or empty value.

$PSHOME : Contains the full path of the installation directory for Windows PowerShell

Parameter/Arguments

Another important piece on a powershell script we need to care about is Parameter/ Argument, for a script both do the same function but former is something which is defined in a script, scriptblock or a function. Argument is some value which is passing to a parameter to a script, scriptblock or to a function itself.

Below script better define the parameter/argument and how to execute them. Save all below script block in to a powershell script file, I saved it as “My_Script.PS1“.
Here we used “param” statement to define arguments by name, to pass the values to scripts using these parameters.

Edit : Some terms and method used below might not explained already.
this is only to show how parameter and arguments works

#———————- Script Start ———————
param (
[string]$SomeValue = 100,
[string]$ComputerName = $env:computername,
[Parameter(Mandatory=$true)][string]$username,
[string]$password = $(Read-Host -asSecureString “Please enter you passowrd”),
[switch]$TrueFalse = $false
)

write-output “First argument is $SomeValue”
write-output “Second argument is $ComputerName”
write-output “The True/False switch argument is $TrueFalse”
#——————— Script End ————————-

We can run this script like “.\My_Test.ps1 -ComputerName vipinpc -SomeValue 30 -username vipinuser -TrueFalse:$true”  (pic 1) Or  .\My_Test.ps1(Pic 2)

Here “$SomeValue, $ComputerName, $username, $password $TrueFalse” are the arguments defined, which is used to pass the values through Parameter defined.

Where $Somevalue, $Computername, $TrueFalse has default value, if no value provided to the script default value will be assigned.
$username is mandatory parameter which should be passed through or will be prompted.
$password is not defined mandatory but will prompt using powershell read-host cmdlet that interactively prompt for input some information (here password save the string as a securestring )

Result as below
Pic 1 arg_result_01Pic 2
arg_result_02

Below shows another simple way of using arguments in powershell scripts, without using the “param” statement to define the argument by name.

Copy script in to a powershell script, we will execute with values as argument list separated by ‘space’ . for example, .\My_Script.ps1 TEST_PC1 TEST_PC2

# —— Script Start —————
[array]$arglist = $Args #this will pass all values in to array called $arglist variable

foreach ($argments in $arglist) {
Write-Host “Here is the argument ‘$argments’ passed to Foreach loop”
}

Write-Host “My first argument is $($Args[0])”
Write-Host “My Second argument is $($Args[1])”
# —— Script End —————

Result will be as shown below.arg_ps01

View Part 1

Advertisements

4 thoughts on “Learn PowerShell (my way)!! Part 2

    • Vipin Vasudevan says:

      Thanks for your response.. I will definitely work on this page to make them easy !

      Both variable and parameter do the same function with in a script, variable is one whihc defined and assigned value with in a script or function. But parameter are defined with in a script but value must be passed to script (for ex: .\powershelltest.ps1 -computername PC1 -UserName user01, where computername and UserName are defined in script) OR to a function (I will explain them in later session)

      Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s