Friday 2 October 2015

Simple Powershell Scripting


               Powershell always rocking with its possibilities. As it mark's it one of the advantage is scripting over powershell .Powershell scripting is nothing like simply batch scripting but with bit advanced and powerful.

               Powershell scripts are nothing but simple text files with .ps1 extension. You can create powershell script in any if things like textpad , notepad or notepad++ etc.,. simply by typing whatever content we need and save it with .ps1 extension.

             Let's start with the simple sample script which is giving addition of two numbers.
The main thing in powershell is declaration and usage of variables. But it's pretty much simple than you thought because there is no data types or limitation of range here.We can store anything in a variable with powershell scripting.

Variable can be defined by using dollar($) symbol before your user defined variable variable name.

Eg:$a = 5 Here $a is the variable which holds the value 5. As I told earlier we can store any type of data in powershell variable . If you want to store your credential in a single variable that's also possible with powershell script.The following is the simple script which will give the addition of 2 numbers.

$a = 10
$b = 34
$c = $a + $b
Write-host "Result is :: "  + $c



Here you can se some command called Write-Host which is nothing but print statement in any of the language like c or c++ or Java

Here this is simple static program. What to do if you want to decide the value at run time ?
Simply it's possible by getting input from the user.

This can be achieved by means of 2 ways either passing as arguments or getting input from user and show it with resultant value.
At first I will explain how to make a script to accept arguments. The script will be

$a = $args[0]
$b = $args[1]
$c = $a + $b
Write-host "Result is :: " $c

pause






Here pause is optional which will be useful when run it using Run command.

Now scripts are ready. Then how to execute the script ?
It's simple with powershell. You can simply drag and drop the script in powershell window or navigate to path where script got located ant type the script name followed by .\ and click Enter to get the result.

If you want to run script without opening console type Powershell.exe and then followed by file location in Windows Run command.

Is it done ? defnintely not. If you execute script in your powershell for the first time it will show error like execution of script is disabled in this system.



Because by default script execution is in restricted state in Windows powershell in any of the system.
In order to enable this in you machine you can use any one of the following execution mode.

You have yo use the command Set-ExecutionPolicy -ExecutionPolicy followed by any one of the following execution mode.

 Unrestricted - No requirements script can be simply executed .
 RemoteSigned - Execute all local script and remote but remote script should be of signed one.
 Allsigned    - Both local and remote script need to be signed.

 For my betterment i will make it as Unrestricteed in my machine by using the command
Set-ExecutionPolicy -ExecutionPolicy Unrestricted



If you execute this command it will whether to allow it or not type 'Y' and fire enter.
If I am enabling in my system fir the first time i got some error likr this. So to solve that again launch powershell as administrator.





That's it with the basic scripting behind powershell since it is such a simple initiative to your advanced scripting.


No comments:

Post a Comment