Showing posts with label Powershell Basics. Show all posts
Showing posts with label Powershell Basics. Show all posts

Saturday, May 9, 2015

#68 : Understanding Try-Catch-Finally Blocks in Powershell

Try-Catch-Finally is language construct introduced in Powershell v2.0. It provides option to error handling in an elegant manner. While a faulty code might stop flow of your code, Try-Catch can be used to handle some of the obvious errors. For example, if you are writing to a file and suddenly disk got full, your script will fail miserably and error could be trapped further. The script will proceed further without having finishing pervious operation correctly. In a simple language, we can say that Try-Catch gives option to handle errors during runtime without impacting the desired flow of script.

(There are so many big articles written on this subject, this article is just a jump start. My intention is not re-invent the wheel.)


Below is a simple example using this language construct :
(It will run a divide-by-error operation and cause failure. The error will be forwarded on screen)



Enjoy developing Powershell scripts!




 

Monday, May 4, 2015

#65 : Error-Handling with Trap

In v1.0 of Windows Powershell, there was no Try-Catch-Finally feature. We had trap construct for the same purpose. I think, Trap was a new concept as it was not there in any of the existing .NET-based languages. But Powershell v1.0 somehow had this unusual feature. In most of the old machines, there will be Powershell v1.0 and we cannot use Try-Catch blocks for those servers. So if you are writing script for all versions of Windows, there is no option left except using Trap.

Let us see the Trap example below :



The above example will call two divide statement which will end up with divide-by-zero. There are two blocks where trap is declared :
1. At script level
2. At function level

You will notice that output will call Function level when it will fail inside function block and it will call script level trap when called outside function.

Hope this simple example will make you learn this correctly. Please send me your questions and suggestion if you have any. Learning from small example is always better if you want to understand something correctly.

That's it for today! Thanks for reading today and keep reading this blog which is updated everyday.


Sunday, May 3, 2015

Tool #4 : Powershell ISE with Powershell v4.0

Powershell ISE comes bundled with Powershell. The editor was useful in previous versions also, but version 4.0 comes with collapsing and expanding of functions and statement blocks. This is very important to have big functions collapsed as it avoids so much scrolling back and forth. Also, it makes script more readable if you are just reviewing someone's code. Syntax highlighting and Intellisense have already been there.

So, those who have not used it for some reasons. I would request them to give a try on this tool. If you do not see it in your newly installed Windows 2008 or 2012, you can try to add it from features.


Remarkable Features:

1. Intellisense feature
2. East of Debugging Code using Breakpoints
3. Collapsing and Expanding of blocks
4. Code and Output windows switching
5. Familiar shortcuts used in all Microsoft products such as F5 etc.

Below are some items which are missing in this version, hope Microsoft will add in future releases :
1. Script parameter passing is not possible.
2. There is no scope of writing GUI based tools.

If you find something interesting about this tool, please let me know.


 

Wednesday, September 10, 2014

#26 : How to send SMTP Mail with Powershell ?

Many of your scripts require to send mail. There are several ways to send mail, but I am introducing you with SMTP mail sending script here. The code will accept below values which either you can hard-code inside the script or you can make a config file or ini file to get the settings. Below is simple piece of code for the same:



How to send mail to multiple recipients?

This can be done by modifying NOTIFY_ID variable with below -

$NOTIFY_ID=som@foo.com,john@foo.com


Make sure you are using a comma(,) to separate two Mail-Ids.

How to Send HTML Formatted mail using Powershell ?

This is another good option which can be done easily with Powershell. You need to just add one more line inside the code:



Easy and Simple. Enjoy Scripting!!

Thursday, April 24, 2014

#7 : Jump Start to Powershell

Powershell provides a rich scripting environment in Windows based operating systems. It provided better scripting options than Windows batch programming and VBScript which were previously used. It is high level language in itself as the script is not interpreted, but compiled to MSIL and then executed. Nearly all Windows based servers are fully supported by Powershell scripting. Any task or action done with the help of GUI can now be automated using Powershell modules.

Powershell is based on .NET Framework. It has nearly all the features available which are available in any .NET Framework related programming languages such as C#, VB.NET, VC++ and others. It also allows running programs written in .NET based languages.
Powershell was initially named as Monad, but it was later named as Powershell.
The programming style of Powershell is similar to Perl and Korn shell and similar to C# or VB.NET. 
Powershell can be used in Windows Core installation for administrative purposes. 
How to install Powershell?
There is no need to install Powershell on Windows 2008 and above version of servers. You just have to enable the feature.
To check if Powershell is installed-
1. Goto Start > Run.
2. Type cmd to open command prompt.
3. Type below command -
where powershell 

If Powershell is not installed, you will get below output-

INFO: Could not find files for the given pattern(s).
You may install the version 1 or version 2 of Powershell based on the machine type and operating system. Please search on the internet and find the link, I am skipping providing link as it might change in future. 

Powershell command line
Powershell command line is nearly the same as Command prompt. However, it looks bit different in background color which is blue by default.



Hello world!
This is simple to just write Hello world! Just type in below command -
echo Hello world!

But, I will write a script for the same which will have an argument. 

Writing your first script in Powershell:

1. Powershell script must be a script with *.ps1 extension. 
2. Your execution policy must be set correctly. I will discuss about execution-policy later, but for now, set to unrestricted to run your first script using below command.

set-executionpolicy Unrestricted;

3. Write the script as mentioned below -





function HelloWorld([string] $useInput)  
{       
 echo $userInput;  
}    

HelloWorld $args[0];  




4. Save the script. I assume your named helloworld.ps1. 
5. Run the script

From Powershell command-line:
.\helloworld.ps1

Use .\ when the script is located on the same directory (present working directory)

You may also write the complete path. 

From Command prompt:
Powershell D:\helloworld.ps1

Points to remember :

1. The file extension must be .ps1
2. Powershell execution policy must be set appropriately to run the script on a given machine.
3. You cannot double click the powershell script similar to batch programs. 
4. A Powershell script run under .NET Framework. It can include all the libraries which C# or VB.NET can utilize. 

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...