Thursday, December 13, 2012

As i mentioned earlier that i am very busy in the month on October and November that’s why i was not able to write any article on Powershell, but i am little free now and will try to publish articles whenever i get free time.

Today one of my fried call me and he was having some problem understanding DO WHILE loop, I explained the working of the loop over the phone to him and i thought it is worth writing a post on it.

the working of do while loop is simple.

do { code block }while ( condition )

for example.

do { Dancing }while ( music is playing )

You got an idea ? isnt ?

That means do something till while condition is true. Like in above not-IT example do the Dancing till the Music is Playing

In my previous post i have write a little article on how to view to see the contents of files.

As you know that i like Text files and my most of the day spent on PowerShell console so i love to do different different examples with cmdlets and command.

On my laptop i have a simple test file name as “Seen Movies.txt”, i this i mainly put the name of all movies which i have seen on my laptop and whose video files i do have stored on my NAS drive. SO when i need to get any movie i check this file to know that if i already have that movie or not. In case i have a movies entry in the file then i don’t get or purchase that Movies and if i don’t have entry of that movie then i can think of buying it if i am interested to watch that movie.

So what’s special ? “we can do the same by open the file in notepad and then we can search for that movie”.

Yes agreed but remember there are lots of way to do one thing,

To check that i have that movie or not i just run a simple command.

Get-Content FILE_NAME | Select-String “Movie Name”

Using Get-content we are getting the content of the files and the we are piping the output to Select-String Cmdlet and then selecting to show the string “Avenger”

The above command showed me that i have “The Avengers” movies in my database.

ok but what is the real use?

The real use is that you can search for specific words in log files.

Get-Content C:\Windows\Logs\CBS\CBS.log | Select-String "error"

In above command i am getting the content of C:\Windows\Logs\CBS\CBS.log and piped them to the Select-String cmdlet and asking it match everything which contain the word “Error”

If you want to do a search on case sensitive words then you can use -CaseSensitive parameter with Select-String cmdlet

Thursday, October 4, 2012

In my office we have multiple internet connections. The Alerting and monitoring already set when any of connection went down.

But sometime i need to know which Internet connection is currently working. to check which internet connection is currently working we can go to some IPCHECK sites and check the Static/Public IP Address of the Internet Connection but i want to write a simple script which can get an Static/Pubic IP Address if the internet for me.

The use of this script, you can use this in any GUI so show the current static address in any in-house WPF Powershell based custom tool.

You can incorporate it in to a any user end client script and much more.

Wednesday, October 3, 2012

Perfmon is one the favourites tools of all IT admins. Every IT admin love this tool and we love Powershell too.

As a Powershell users most of my time is spent on powershell tool and sometime there is a need arrive when you want to check few performance counters. and as you know i am quite lazy is doing stuffs and I think that laziness is a path to automation.

I don’t want to to pent RunBox and then type PerfMon and then click on OK and then add the performance counter which i needed.

In performance counters i don't care about Graphs , i do care about values.

Powershell give us a very nice and clean cmdlet to get the values of Performance counters. As i mentioned above I don’t care about the graph i do care about values and in powershell console every thing is Text and we love that texts.

To get the values of performance counter we need to use the cmdlet Get-Counter .

Lets Start.

To list all the performance counters sets use the command.

Get-Counter -ListSet *

This will show you the list and path of all Counter sets.

If you want to know what kind of Counter sets you do have in your system just piped the above command to the Select CounterSetName cmdlet

Get-Counter -ListSet * | select CounterSetName

we can also use wildcards in -ListSet parameter. I am looking for all disk related counter sets, to search all conter sets those have disk word in it i can use *Disk* argument in -ListSet parameter

Get-Counter -ListSet *disk*

I am interested in counter set Logical Disk. and i can piped the above command to show me the LogicalDisk counter set only using where-Object cmdlet.

In above command we are using counter "\LogicalDisk(C:)\Avg. Disk Queue Length" to monitor the disk queue length of our C drive and using -SampleInterval parameter we are telling to get-Counter cmdlet to collect the data for 10 seconds.

if you want to see or monitor the counters you can use the -Continuous parameter.