Tuesday, April 29, 2014

Creating vSphere VM Snapshot for a given list of servers



#Taking the input from user
$File_Name = Read-Host "Provide the filename that contains the list of Servers "
$VM_list = Get-Content "$File_Name"

Write-Host "VMName`t`t`t      SnapShot Name`t`t`t`t`t    Size of Snapshot"
# Checking each Server one by one
foreach($CHK_VM in $VM_List) {
$Cap_VM_Info = Get-VM $CHK_VM | Get-Snapshot | Select VM, Name, SizeGB
$Name = $Cap_VM_Info.VM
              $SnapshotName = $Cap_VM_Info.Name
              $SizeOfSnapshot = $Cap_VM_Info.SizeGB

Write-Host "$Name`t`t$SnapshotName`t`t`t$SizeOfSnapshot"
}



Get-VM | Sort Name | Get-Snapshot | Where { $_.Name.Length -gt 0 } | Select VM,Name,Created,SizeMB

Tuesday, April 15, 2014

Script to scan for new disks added to a Linux server

#!/bin/bash

for channel in `ls /sys/class/scsi_host`
do
    echo "rescanning $channel"
    echo "- - -" > /sys/class/scsi_host/$channel/rescan
done

Friday, April 4, 2014

appending string after end of each line

I have a File that contains list of servers and I just want to append a domain-name (say .xyz.com ) at the end of line.

# head -5 serverlist.txt
server1
server2
server3
server4
server5
# sed  -ie 's/$/.xyz.com/' serverlist.txt
# head -5 serverlist.txt
server1.xyz.com
server2.xyz.com
server3.xyz.com
server4.xyz.com
server5.xyz.com
#

Another method using Awk:
awk '{ print $0 ".xyz.com" }' < serverlist.txt > resultfile.txt

Wednesday, February 19, 2014

Writing infinite loop in Perl

for (;;) {
  # perform some tasks
}

while (1) {
   # perform some tasks
}

Tuesday, November 5, 2013

Monday, October 14, 2013

Useful SED Examples

Append string on each line in a given file:
# sed 's/$/<string>/'  file.txt


Wednesday, October 2, 2013

A sample PowerCLI script for iterating multiple vCenters

Add-PsSnapin VMware.vimAutomation.core
$vcs=@()
$vcs +="vCenter1"
$vcs +="vCenter2"
$vcs +="vCenter3"
foreach ($vc in $vcs)
{
"Connecting to $vc"
Connect-VIServer -server $vc -user xxx -password xxx
"Connected to $vc"
}
...
Disconnect-VIServer *

Wednesday, January 23, 2013

Some SED tips for my records

To delete trailing whitespace from end of each line:
# sed 's/[ \t]*$//' filename > filename_notrailingspace

To remove all blank lines:
# sed '/^$/d' filename > filename_noblankspace

To remove all leading and trailing whitespace from end of each line:
$ cat filename | sed 's/^[ \t]*//;s/[ \t]*$//' > filename_nospace

Sunday, September 2, 2012

Perl script to check Kernel parameter differences between 2 Linux machines

This Perl script can be used for checking the Kernel Parameters difference between 2 Linux machines.
All you got to do is, take the ‘sysctl –a’ output into a file from each Linux machine that you wish to compare and give it as an input for this script like shown below:
Upon execution, you need to choose option 1 or 2.  ‘1’ is for listing out all the kernel parameter values of both servers in tabular form and ‘2’ for listing out only the parameters that are either differing or non-exist on other server.
 # perl  <scriptname>.pl  <first_server_kernel.txt>  <second_server_kernel.txt>



#!/usr/bin/perl
# Description   :       This script can be used to list out of the differences in the Kernel Settings between 2 Linux Servers.        
#                       It takes 2 files (consists of sysctl -a output) as input, upon execution it asks for User's choice either      
#                       to List out all the Kernel parameter of 2 Servers or to list out only the Differences.                        
# Version       :       1.1                                                                                                            
# Execution method :    Manual (by any normal user)                                                                                    

if( @ARGV != 2 )
{
   print "\nUsage: $0 <First-file> <Second-file>\n\n";
   print "First-file should contain 'sysctl -a' output taken from first Linux Machine\n\n";
   print "Second-file should contain 'sysctl -a' output taken from Second Linux Machine\n\n";
   exit 1;
}

open(File1, "$ARGV[0]" );
open(File2, "$ARGV[1]" );

my @first_file = <File1>;
my @second_file = <File2>;
my %output1, %output2, %kerparm;
my ($cnt1,$cnt2) = (0,0);

print "\nEnter '1' for Listing all Kernel parameters or '2' to list only the Differences : ";
my $choice=<STDIN>;
chomp($choice);

system $^O eq 'MSWin32' ? 'cls' : 'clear';
printf("%-50s %-25s %-50s\n\n", "KERNEL PARAMETER", "FIRST SERVER", "SECOND SERVER");

foreach $i(@first_file) {
        $i =~ s/\s+$//;
        @array1 = split(" = ",$i);
        $output1{$array1[0]} = $array1[1];
        $cnt1++;
        $kerparm{$array1[0]} = $cnt1;
}

foreach $j(@second_file) {
        $j =~ s/\s+$//;
        @array2 = split(" = ",$j);
        $output2{$array2[0]} = $array2[1];
        $cnt2++;
        $kerparm{$array2[0]} = $cnt2;
}

foreach $key (sort keys %kerparm)
{
  $output1{$key} = "" unless defined $output1{$key};
  $output2{$key} = "" unless defined $output2{$key};
  if($choice eq '1') {
          printf("%-50s %-25s %-50s\n", $key,$output1{$key},$output2{$key}); }
  elsif($choice eq '2') {
          printf("%-50s %-25s %-50s\n", $key,$output1{$key},$output2{$key}) if ($output1{$key} ne $output2{$key});
  }
}

Friday, August 24, 2012

PowerCLI script to get the status of CPU/Memory Hot-Plug in vSphere5

Get-VM | Get-View | Select Name, `
@{N="CpuHotAddEnabled";E={$_.Config.CpuHotAddEnabled}}, `
@{N="CpuHotRemoveEnabled";E={$_.Config.CpuHotRemoveEnabled}}, `
@{N="MemoryHotAddEnabled";E={$_.Config.MemoryHotAddEnabled}} | Export-Csv C:\<path>\file.csv

Popular Posts

About Me

My Photo
I have started this blog to share my work experience and spread some smart solutions on Linux to Internet community. I'm hoping more people will get benefited from this blog. Brief about me: I have 10+ years experience working as System Admin and currently work with VMware.