#!/bin/bash
# Linux/UNIX box with ssh key based login
SERVERS="Server1 server2 server3"
# SSH User name
USR="Admin"
# Email
SUBJECT="Server user login report"
EMAIL="sravan03ie29@hotmail.com"
EMAILMESSAGE="/tmp/emailmessage.txt"
# create new file
>$EMAILMESSAGE
# connect each host and pull up user listing
for host in $SERVERS
do
echo "--------------------------------" >>$EMAILMESSAGE
echo "* HOST: $host " >>$EMAILMESSAGE
echo "--------------------------------" >>$EMAILMESSAGE
sudo ssh $USR@$host " hostname; df -h" >> $EMAILMESSAGE
done
# send an email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
Myself (Sravan Kumar E) Datacenter and Cloud Computing Professional with over 14+ years of experience, including service leadership and management of mid to large size organization, corporate development, Product Implementation, Cloud Scoping and Implementation,business,operations and strategy.
Advertisement Header
Wednesday, 28 July 2010
Tuesday, 6 July 2010
Loop Processing in Powershell
For Loop:
for ($i=1; $i -le 5; $i++)
{Write-Host $i}
Output:
1
2
3
4
5
------------------------------
$i=1
for (; $i -le 5; $i++)
{Write-Host $i}
------------------------------
$ints = @( 1, 2, 3, 4, 5)
for ($i=0; $i -le $ints.Length – 1; $i++)
{Write-Host $ints[$i]}
------------------------------
Foreach:
$ints = @(1, 2, 3, 4, 5)
foreach ($i in $ints)
{Write-Host $i}
Output:
1
2
3
4
5
-----------------------------
for($i=1; $i -le 10; $i++)
{
Write-Host $i
break
}
-----------------------------
$i=0
$varB = (10,20,30,40)
foreach ($var in $varB)
{
$i++
if ($var -eq 30)
{
break
}
}
Write-Host “30 was found in array position $i”
------------------------------
for ($i=1; $i -le 5; $i++)
{Write-Host $i}
Output:
1
2
3
4
5
------------------------------
$i=1
for (; $i -le 5; $i++)
{Write-Host $i}
------------------------------
$ints = @( 1, 2, 3, 4, 5)
for ($i=0; $i -le $ints.Length – 1; $i++)
{Write-Host $ints[$i]}
------------------------------
Foreach:
$ints = @(1, 2, 3, 4, 5)
foreach ($i in $ints)
{Write-Host $i}
Output:
1
2
3
4
5
-----------------------------
for($i=1; $i -le 10; $i++)
{
Write-Host $i
break
}
-----------------------------
$i=0
$varB = (10,20,30,40)
foreach ($var in $varB)
{
$i++
if ($var -eq 30)
{
break
}
}
Write-Host “30 was found in array position $i”
------------------------------
Tuesday, 29 June 2010
Running a PowerCLI Scheduled task
There are two ways of doing this really:
1. Add a line to a start of each of your scripts to make sure it loads the PowerCLI snapin, without which PowerShell will not recognise any of the PowerCLI cmdlets.
Add the following line to the top of each script you will be running as a scheduled task:
add-pssnapin VMware.VimAutomation.Core
Then in the run box on your scheduled task you can call your script with the following command:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe “& ‘C:\Scripts\MyScript.ps1′”
But remember when you are troubleshooting this script or amending it you will most likely get an error as your script editor will automatically add the VMware snapin.
2. This method is the one I use as its easier than remembering to put the line at the top of each file and then commenting it out when you want to edit it etc, this method runs the PowerCLI Console file before it runs your script.
A PowerShell Console file is a xml like file containing information on what snapins to load when PowerShell is started.
This will enable the snapin for you and there is nothing to ad to the scripts.
The run command then looks like this:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& 'C:\Host-Inventory-Status.ps1'"
You can test both of these methods by running them from a cmd prompt before using it to ensure they work properly.
1. Add a line to a start of each of your scripts to make sure it loads the PowerCLI snapin, without which PowerShell will not recognise any of the PowerCLI cmdlets.
Add the following line to the top of each script you will be running as a scheduled task:
add-pssnapin VMware.VimAutomation.Core
Then in the run box on your scheduled task you can call your script with the following command:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe “& ‘C:\Scripts\MyScript.ps1′”
But remember when you are troubleshooting this script or amending it you will most likely get an error as your script editor will automatically add the VMware snapin.
2. This method is the one I use as its easier than remembering to put the line at the top of each file and then commenting it out when you want to edit it etc, this method runs the PowerCLI Console file before it runs your script.
A PowerShell Console file is a xml like file containing information on what snapins to load when PowerShell is started.
This will enable the snapin for you and there is nothing to ad to the scripts.
The run command then looks like this:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& 'C:\Host-Inventory-Status.ps1'"
You can test both of these methods by running them from a cmd prompt before using it to ensure they work properly.
Subscribe to:
Posts (Atom)