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]






Thursday, March 19, 2015

#48 : 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 a fiction 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.






One you run it, you would see Merlin talking ...




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!!

Friday, March 6, 2015

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

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