Wednesday, September 10, 2014

#26 : How to send SMTP Mail with Powershell ?

Many of your scripts require to send mail. There are several ways to send mail, but I am introducing you with SMTP mail sending script here. The code will accept below values which either you can hard-code inside the script or you can make a config file or ini file to get the settings. Below is simple piece of code for the same:



How to send mail to multiple recipients?

This can be done by modifying NOTIFY_ID variable with below -

$NOTIFY_ID=som@foo.com,john@foo.com


Make sure you are using a comma(,) to separate two Mail-Ids.

How to Send HTML Formatted mail using Powershell ?

This is another good option which can be done easily with Powershell. You need to just add one more line inside the code:



Easy and Simple. Enjoy Scripting!!

Thursday, September 4, 2014

#25 : Create a ShortCut with Powershell

Creating a Shortcut might be a difficult task in any scripting language in Windows. Not sure how difficult or simple is there in Unix.

Let's take a look on the simple few lines which accomplish this task -

clear
$wsc = New-Object -ComObject ("Wscript.Shell")$sc=Join-Path -Path "d:\test" -childpath "Notepad.lnk"
$slink=$wsc.CreateShortcut($sc)$slink.TargetPath = "C:\windows\notepad.exe"
$slink.WindowStyle = 0
$slink.Hotkey = "CTRL+SHIFT+F"
$slink.IconLocation = "notepad.exe, 0"
$slink.WorkingDirectory = "d:\Test"
$slink.Save() 


Since this uses Wscript.Shell which is VBScript shell, there are some Windows version where you do not see desired output. Please let me know..

Enjoy!

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