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. 

Wednesday, April 16, 2014

#5 : Learn Windows PowerShell in a Month of Lunches

The name is promising and close to practical approach of something. Hope the name could help you plan to study Powershell in spare time like lunch provided you don't take heavy diet with too much fat which will divert all blood circulation to your stomach. (Just kidding!)



The book is good and it has good content for Adminstrators. The book is not meant for developers although. it gives good concept for Powershell v2.0.

Don Jones has good style of covering any topic and I enjoyed it a lot with this book.

Tuesday, April 8, 2014

#3 : Professional Powershell Programming

Powershell or any scripting gives you freedom to write simple to complex programs. You must follow standards while programming to make your life easier and your organization as well.

Common Standards followed in Scripts
1. Add header and footer of the code. The header might be organization specific. But, mostly below lines are common -

#--------------------------------------#
# Script Name :
# Author Name :
# Purpose     :
#
#--------------------------------------#
# Revision History
#--------------------------------------#
# 05/05/2011 - Som DT - Initial release
# 08/12/2011 - Jack   - Added one mo...
#--------------------------------------#  

2. Add comment for each complex logic. You may skip those lines where logic is simple and understood. But, when logic is complex, you are supposed to add at least one line of comment.

3. Declare all Global variables as capital case and Local variables as small case.

(Short of time.. leaving to office.. i will be back..Happy scripting.. )
- Som DT

Thursday, April 3, 2014

#2 : ASE Admin Script Editor Review

Form Designing is rarely required in Powershell Programming. But, this is very effective to design form in Powershell and it enhances the usability of the script.
To design form, you can simply code yourself few of the lines.

Is there any tool from Micrsoft?
No. There is no tool provided from Microsoft which is not a new thing with Powershell. Powershell might be a first product which is mistrated like this by MS. I don't know why?
If they release some tool, it become difficult to use - Powershell ISE is one of such example. It has few things and many things missing. Especially I want Function or block collapsing tool. But this is missing in Powershell ISE. The editor is slow and display is not sharp. Who is the person who likes this blur font while developing something.

So, the option is Third-party tool. Third party tool are effective but expensive. But, we can take a demo to convince our manager.

ASE Admin Script Editor

Company: iTripoli
Evaluation: 45 days
Description:
This is a good Powershell editor with a Form Designer. It can be used for general Powershell programming and also for WMI, VBScript and other scripting language.
The form designer comes along with it. It can be found at - Tools > Script Form Designer. The window appears as Visaual Studio and everything is yours.
Once you are done with Form Designing- click File > Save to ASE or press Ctrl+E. I like the tool.
Cost:
Standard Edition $99.00 / Each
Professional Edition $199.00 / Each
Enterprise Edition $299.00 / Each

Powershell-Tips Rating: 3/5

Wednesday, April 2, 2014

#1 : Powershell Basics : Pyramid program

I remember those old days, when I was learning programming. Making pyramid, paliendrome, fibonacci series, factorial and so many. Basically, these all were little difficult in those days and now they look like building block for making a programmer. Aaah! what a philosophy. 

Recalling those days, I am writing the same pyramid program in powershell. 
I have written one-liner:

for ($x =0; $x -lt 21; $x++) { "*" * $x }



Yes, this is sufficient! 
If you are learning programming with Powershell, you make seek my help. I can give you roadmap. 

#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...