Showing posts with label Programming-Basics. Show all posts
Showing posts with label Programming-Basics. Show all posts

Saturday, April 18, 2015

#53 : Error Handling with $Error

$Error is an interesting array which records all error messages from current session. It can be cleared also and looked up for presence of any error message in last command. In most of the cases, you may want to set it clear and then look for $error[0].

Make sure when you check the $error.count right after you run the statement which you think as problematic statement.

Code:

$error.clear()

copy-item “c:\temp\abc.txt” “d:\abc.txt”

if ( $error.count -ne 0 )

{

echo “An error occurred in copy operation. Please review the message below:”

echo $error[0]

}

When you run the above in console and abc.txt does not exist, you will get an error message in red color and then from echo part as well. You might be thinking why we need this if error message is there in console. The reason behind using is that if you run the script from a scheduler and want output to be there in log file, this approach can help you a lot.

Other better methods of catching exceptions are:

Try.. Catch

Trap

We will discuss about them in next article. Test this approach and let know your responses.

Enjoy!

Friday, March 27, 2015

#49 : Generating Random Numbers with Powershell


I never got a requirement where I need to generate a random number in Powershell. The reason is that I never develop something like game or script which runs on chance.  But there is one script which might need random number. The script will be all about generating complex passwords randomly. The script will generate a number between 1 and 26 to get a number and then a letter is generate from that number. The loop goes for as many characters you want in your random password. We will talk about the program in next post. For now, let's play with get-random cmdlet.

The cmdlet to be used is :Get-Random. Please review the syntax below. If syntax looks horrible, kindly take look in to examples, I bet you will not close this tab in your browser.

Syntax:

Get-Random [-InputObject] Object[] [-Count int] [-SetSeed int] [CommonParameters]



Example:

[1] Generate a number between 0 and 100.

Get-random -Minimum 0 -Maximum 100

- Run it multiple time and you will see a new number everytime.

[2] Generate a number within the list provided

get-random -input 8, 4, 12, 20, 24, 108 -count 1

Conclusion:

Get-Random cmdlet can be used for several purpose. At least, I know one purpose which is all about generating a random complex password. I will write the script and post very soon. The viewership of this blog has increased alot and people like and send their mails to me. I think, the time has come when I should write some well qualified scripts and publish in this blog, hope you all will like them.

Thanks!


Get-Random [-InputObject] Object[] [-Count int] [-SetSeed int] [CommonParameters]






Wednesday, February 18, 2015

#45 : Display top n lines or last n lines of a file

Often we reach to a situation where we need to get top 10 or 100 lines of a file.
These commands are fairly simple in Unix shell programming and most of you must have used Head and Tail commands. Those commands work really fast and accurate.

Keeping these points in mind, I dig something and want to share will all of you.

How to use Head and Tail Commands in Powershell?

To get the top 10 lines -
Get-Content ".\file_test.txt" | select -First 10

To get the top 10 lines -

Get-Content ".\file_test.txt" | select -Last 10

Easy! This is power of Powershell !

Some more Examples:

Problem: Write a command to get 3rd line of a file.

Solution:
Get-Content ".\file_test.txt" | select -First 3 | select -Last 1

Problem: Write a command to skip 10 lines from top and display rest all lines of file.

Solution:
Get-Content ".\file_test.txt" | select -Skip 10

But this approach has one drawback, when file is so big, it takes a little more time to produce results.

To be able to read a big file, you can adjust Get-Content to read only few lines. As you saw, we were reading the complete file before that caused more memory consumption.



Enjoy Scripting!!

Thursday, December 25, 2014

#37 : Download Files from Internet with Powershell

Powershell can be used to download a file from internet. This might help you matching some item from internet or you can create a download manager kind of software in Powershell.
Actually, you can do nearly everything which you can do in C# or any .NET based programming language because, .NET framework is common to C#, VB.NET and Powershell as well. Both Powershell or C# compile the code into MSIL (Microsoft Intermediate Language). But the only difference is Powershell is in form of script and C# will be a program. 
Powershell scripts can be compiled using C#, but again the compiled executable will require Powershell be installed on the machine. This cannot be called as standalone executable, but again I think "Can you run a C# code when .NET framework is not installed?". Anyways, lets not waste more time in thinking, below is the piece of code- 

(new-object System.Net.WebClient).Downloadfile("http://www.freeware995.com/bin/pdfedit.exe", "d:\pdfedit.exe")

Above is smallest inner line of a possible big code which will handle download of files from Internet. 

For sake of simplicity, I always prefer to give a one-liner. The reason is, when you want to know how to do something, your intention is basically to get something too small which can be further enhanced in your program. When I search, I look for a small code and I hope everyone does. There is no point is providing a complex code which you could not understand or incorporate in your code!

Enjoy scripting!!

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