Sunday, 5 October 2014

Android Application of this blog!

Presenting an android application based on this blog. The application can be downloaded here.
It has good interface and it includes most famous articles of this blog and you can read them offline.
Please download and have a look. This application will be upgraded time to time, so please upgrade accordingly.

Good part of this application is that it can be run offline, so if you do not have internet, still you can study and learn things related with Powershell.

Hope you would like this application.


Saturday, 31 May 2014

Speaking Script in Powershell

A script can be written which can talk to you and speak out - "Sir! Your copy job has completed." or something like "Sir! Please press any key to continue!". These all are not fictions with Powershell and you don't have to write much more lines of code to accomplish. In fact, this is just a one liner!

(new-object -com SAPI.SpVoice).speak("One more Powershell tips")

So, I decided to write a completely talking script which will copy all file from one location to another.

#--Script will demonstrate File-copy with speaking voice. --# 


$source_dir="E:\folder1"
$target_dir="E:\folder2"

#-- List number of files --#

$count=0

ls ${source_dir} | select Name | foreach { $count++}

(new-object -com SAPI.SpVoice).speak("Sir! Total ${count} files to be copied. Please wait for sometime, Thank you!!")

Copy-Item "$source_dir\*.*" "$target_dir" -recurse


if ($? -ne $True )
{
(new-object -com SAPI.SpVoice).speak("Sorry! Your file-copy operation failed, please troubleshoot!!")
}
else

{
(new-object -com SAPI.SpVoice).speak("Sir! Your file-copy operation completed successfully, Thank you!!")
}



Once you run it, you will have more ideas about writing any interactive script which could give messages verbally. I am now thinking to write something which will take commands from microphone!!


Monday, 25 November 2013

Developing Powershell scripts with Notepad++

Not sure how many are using Notepad++ for developing Powershell, but I am using Notepad++ every time I develop. Being Powershell developer, it helps alot due to following reasons: 

1. Syntax highlighting : This is really easy and you can also edit the color code you want. You can download syntax highlighter or you can make your definition. These things are so easy. Just go to style configurator and set the color you want. 

2. Block Collapsing : The + sign to the left of function block or if statement makes code more and more manageable. ALT+1 and ALT+2 can be used for different levels collapse you want. 

3. Freeware : This tool is free and you don't have to convince your manager. Just request to download. You can also find portable version of Notepad++ also.

Please see the download location at - http://notepad-plus-plus.org/

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. 

Find the disk volumes in host

WMI gives a lot of such important information which makes windows administration more manageable. Scripting becomes more and more robust than ever before. 

Powershell can be used to call WMI objects and we can get so much information out of it. 

CODE : 
get-wmiobject win32_logicaldisk | select DeviceId,Size, FreeSpace



You might get a result in proper tabular format. There are lot many scripts possible with this. 
1. Script to report low disk space
2. Script to display disk inventory in multiple hosts 

If you have trouble writing scripts mentioned above, just add your request in comment,  I will pick from there.

Wednesday, 20 November 2013

Jump Start to Powershell

Powershell is one of command shell alternative for handling command line based operations and scripting.

Powershell is based on .NET Framework. It has nearly all the features available in other .NET Framework related programming 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?

Installation of Powershell is not required for Windows 2008 and Windows 7.. 

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

Tuesday, 19 November 2013

Creating modules with Powershell

Powershell modules are extension to inbuilt cmdlets. You may download third-party modules or you can write them using C#. Along with some installation, modules are installed in Powershell.

List the modules available: 

Use get-module to list the modules.

Import an existing module: 

import-module <module_name>

Writing your own module with Powershell

You can write your own module in Powershell using below steps:
1. Create a file with below content: 

function do-nothing()
{
echo "You are in nothing"
}

function do-something( $i, $j)
{
echo "Result : $($i + $j)"
}

2. Save the file with .psm1 extension. I use test.psm1 as filename.

3. Import the module 

import-module c:\tst\test.psm1

4. Check if module is loaded

PS D:\Org\Powershell> Get-Module

ModuleType Name ExportedCommands
---------- ---- ----------------
Script test {do-something, do-nothing}


5. Run the command used 

PS D:\Org\Powershell> do-something 5 6
Result : 11


CONCLUSION

Overall, we have below advantages using this approach:
1. Increases code manageability
Any changes made in one module is copied over all the modules.

2. Increases code reusability :
You can use common tasks like sending mails or logging in one module and share in all the scripts.

3. Team work
If a team of people working, working in modules reduces burden on single person and everyone owns his own part.