Friday, August 29, 2014

#24 : How to play music from Powershell Script?


While doing something, I thought to add some alerting music. Just to notify that script has completed execution. Just for fun and in effort to do everything in Powershell, I wrote the below :

$sound.SoundLocation="c:\WINDOWS\Media\notify.wav"
$sound.Play()
$sound.Stop()

You may use this script when you have to check something related with Sound-Card or it may be useful to give some interactive help. You may run any of the WAV file with this.

Hope you liked this article.

Friday, August 22, 2014

#23 : Find the List of Services Running or Stopped

While programming a script, I wanted to know if specific service is running or not. To accomplish it, I used Get-Service cmdlet. Get-Service gives you details about all the Windows Services.

1. Get the name of services, status and description about all services -
Get-Service

2. Get the details of services running -
Get-Service | where { $_.Status -eq "Running" }

3. Get the details of services stopped -
Get-Service | where { $_.Status -eq "Stopped" } 

4. Find if specific service is running or not and do some operation -
$SPOOL_SRV_STATUS=Get-Service | Where { $_.name -eq "Spooler" } | foreach {$_.Status}


if ( $SPOOL_SRV_STATUS -eq "Running" ) 


    echo "Service is running ... " 
    #--Do any operation --#   

else
{
    
    echo "Service is not running ... " 
    #--Do any operation --# 


}

I play a lot with Powershell and submit them for you.. Please send your comments and mails to me...

Happy scripting!!

Friday, August 15, 2014

#22 : Get History of Command in Powershell

Get-History is the command which can be used to find the commands executed in the current session.

PS C:\Users\admin> Get-History

Id CommandLine
-- -----------
1 $arr=@()
2 $arr += "ABC"
3 $arr += "XYZ"
4 $arr += "PQR"
5 $arr[0]
6 $arr[1]
7 $arr[2]
8 $arr[3]
9 $arr.count
10 for ($x=0; $x -lt $arr.count; $x++) { echo ${arr}[$x] }
11 dir
12 Get-Counter
13 Get-EventLog
14 get-help

Friday, August 8, 2014

#21 : Creating an Array in Powershell

I have gone through several article to understand this, but below is the one which looks most simple to me, the reason is - below one is dynamic and can be used easily in your scripts -

$arr=@()
$arr += "ABC"
$arr += "XYZ"
$arr += "PQR"


$arr[0]
ABC
$arr[1]
XYZ
$arr[2]
PQR

$arr.count
3

for ($x=0; $x -lt $arr.count; $x++) { echo ${arr}[$x] }
ABC
XYZ
PQR
Simple and effective! This is something which we want in all our scripts.
This is dynamic and you can assign so many values in different functions. I will take in detail later.... Keep sending your comments / mails.



Friday, August 1, 2014

#20 : Moving Cursor to Specific Location

Powershell is full of all possibilities which you might think. I believe there is nothing which can be done by other tools and can not be done in Powershell. This is great about Powershell. So, if you assume that something can not be done - think twice there might be some alternative.

Long back when I was learning Shell Programming, I used tput cup command to move cursor and print something in x,y co-ordinates of screen. This option was not in Batch Programming which I always wanted and it was to much effort consuming to make a Console-Based interactive screen.

Below code can be used to accomplish it:

cls 

function move-the-cursor([int]$x, [int] $y) 
{
 $Host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates $x , $y 
  $Host.UI.Write('Hello') 
} 



move-the-cursor 8 8 
echo "Tested by Som : OK" 

When you run the above it moves the cursor to a different location and then prints Hello.
The above sample is enough for you to understand the concept.

Thanks for reading this article!

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