Script to capture the number of days since password was last changed for all users on ESX server. (Output saved in CSV file)

Leave a comment

 #Author: Anil Narayanan
 #Purpose: Shell Script to extract the number of days since the password was last changed on ESX servers. Output saved in CSV file.
 #Version: 1

 echo "========================================================================================"
 echo "Author: Anil Narayanan"
 echo "Purpose: Shell Script to extract the number of days since the password was last changed on ESX servers."
 echo "Version: 1"
 echo "========================================================================================"
 echo ""

header="Serial,User,Pasword Last Changed"
 echo $header > /tmp/`hostname -s`_LPwdChg.csv
 id=$(cut -f1 -d : /etc/passwd)
 inc=0
 for uid in $id
 do
 inc=`expr $inc + 1`
 lchgdate="$(/usr/sbin/lchage -l $uid | grep -i "Last Change" | cut -f2 -d : )"
 sdate=$(date -d $lchgdate +%s)
 edate=$(date +%s)
 diffdate=`expr $edate - $sdate`
 days="`expr $diffdate / 86400` Days"
 line="$inc,$uid,$days"
 echo $line
 echo $line >> /tmp/`hostname -s`_LPwdChg.csv
 done

Location of files that store Network Settings including MAC address in ESX 2.5

Leave a comment

# cd /proc/net/nicinfo
#ls
eth0.info vmnic0.info vmnic1.info vmnic2.info

Capture IBM DSA Hardware logs from ESXi hosts registered to vCenter

Leave a comment

clear
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
 $dsa_filedlg= new-object System.Windows.Forms.OpenFileDialog
 $app = new-object -com Shell.Application
 $cred_filedlg = New-Object system.Windows.Forms.OpenFileDialog

###TODO: Open File Dialog to select DSA Executable File

Read-Host "Press 'ENTER' to select DSA Exe File..."
 $dsa_filedlg.ShowHelp = $true
 $ret_val=$dsa_filedlg.ShowDialog()
 if($ret_val -eq "OK")
 {
 $dsa_filename = $dsa_filedlg.FileName
 }
 Write-Host "DSA Exe FilePath: " $dsa_filename -ForegroundColor Green

###TODO: Open Folder Dialog to select output folder for DSA logs

Read-Host "Press 'ENTER' to select output folder..."
 $dsa_flddlg = $app.BrowseForFolder(0, "Select Folder", 0, "C:\")
 if ($dsa_flddlg.Self.Path -ne "")
 {
 write-host "Output Folder Path " $dsa_flddlg.Self.Path -ForegroundColor Magenta
 }

###TODO: Select credentials file which contains login credentials for individual ESX servers
 ###INFO: The credentials file is generated with the "New-VICredentialStoreItem" command

Read-Host "Press 'ENTER' to select Credentials File...(XML File)"
 $cred_filedlg.ShowHelp = $true
 $ret_val=$cred_filedlg.ShowDialog()
 if($ret_val -eq "OK")
 {
 $cred_filename = $cred_filedlg.FileName
 }
 Write-Host "Credentials FilePath: " $cred_filename -ForegroundColor Yellow

#------------------------------------------------------------------------------------

Add-PSSnapin vmware.vimautomation.core
 connect-viserver <vCenter Server>    ###INFO: Enter ServerName/IP of the vCenter Server
 $hosts = Get-VMHost
 disconnect-viserver <vCenter Server> -WarningAction SilentlyContinue

foreach($esx_host in $hosts)
 {
 ###INFO: The below 5 lines are to get the Hostname from a Domain Name of ESX host
 $temp=$esx_host.name
 Write-Host $temp
 $index=$temp.IndexOf(".")
 $sub=$temp.Substring(0,$index)
 $sub

$pwd = Get-VICredentialStoreItem -Host $sub -File $cred_filename
 $exec_command = $dsa_filename + ' -v -c -d ' +  $dsa_flddlg.Self.Path + '\' + $sub + ' --vmware-esxi ' + $pwd.User + ':"' + $pwd.Password + '"@' + $sub
 [System.Windows.Forms.MessageBox]::Show($exec_command)
 Invoke-Expression -Command $exec_command
 }

Updated PowerCli Script to generate Inventory of Hosts/VM’s attached to vCenter (Under development)

Leave a comment

#Author: Anil Narayanan
#This script has been tested on Virtual Center 2.x and vSphere 4.x
#The old script has been re-written from scratch and this is more optimized and a cleaner looking script. There is some amount of redudant code which I plan to remove soon.
#The output of this script is written to a CSV file under C:\ with the name of the Virtual Center
#Import the CSV file and save it as an excel sheet  (Select "," and ";" as delimiter)
#Below is the data captured by the script:
#ESX: ESX Host Name, Version Build,    Manufacturer, Model, ProcessorType, Total Memory(GB), Uptime (Days), Critical Host Patches, Non-Critical Host Patches, Installed Patches
#VM: VM Name, Power State, H/W Version, CPU's, Memory, OS Name, Tools Status, IP Address, Hard Disks, Datastore Association, OS Disk Utilization(GB), Avg. CPU% Util. (Weekly), Avg. MEM% Util. (Weekly)

clear

add-pssnapin vmware.vimautomation.core
add-pssnapin vmware.vumautomation

$esx_header = "ESX Host Name" + ";" + "Version" + ";" + "Build" + ";" + "Manufacturer" + ";" + "Model" + ";" + "ProcessorType" + ";" + "Total Memory (GB)" + ";" + "Uptime (Days)" + ";" + "Critical Host Patches" + ";" + "Non-Critical Host Patches" + ";" + "Installed Patches (PatchID,Install Date,Desc.)"
$vm_header = "" + ";" + "VM Name" + ";" + "Power State" + ";" + "Version" + ";" + "CPU's" + ";" + "Memory" + ";" + "OS Name" + ";" + "Tools Status" + ";" + "IP Address" + ";" + "Hard Disks" + ";" + "Datastore Association" + ";" + "OS Disk Utilization (GB)" + ";" + "Avg. CPU% Util. (Weekly)" + ";" + "Avg. MEM% Util. (Weekly)"

#Variable Declaration/Initialization
$vcsession=""
$allhosts = @()
$allvms=@()
$vcname=Read-Host("Enter VC Server Name: ")    #Accepting Virtual Center Server Name
$vcname = $vcname.toUpper()
$outputfilename="c:\" + $vcname + ".csv"
$vmipaddress=""
$vmdatastores=""
$strvmdatastores=""
$disk_extensiondata=""
$hostpatchlist=""

$allvms = Get-VM | Sort-Object                #Get all VM's associated with the Virtual Center
$allhosts = Get-VMHost | Sort-Object        #Get all ESX Host's associated with the Virtual Center

#Connecting to Virtual Center Server
$vcsession=Connect-VIServer $vcname
write-output $vcsession
Start-Sleep -Seconds 2

#Send Virtual Center title to file
write-output ("Virtual Center" + ";" + $vcname) > $outputfilename

#Module to fetch VM information
for($j=0;$j -lt $allhosts.Length;$j++)
{
$allhosts[$j].Name
#Send Title header to file
write-output $esx_header >> $outputfilename

if(($vcsession.Version).Contains("4.1"))
{

#The below loop captures patches installed on an ESX server in the variable 'hostpatchlist'
for($i=0;$i -lt ($allhosts[$j] | get-vmhostpatch).Count;$i++)
{
$hostpatchlist=$hostpatchlist + (($allhosts[$j] | get-vmhostpatch)[$i].Id + ", " + ($allhosts[$j] | get-vmhostpatch)[$i].InstallDate + ", " + ($allhosts[$j] | get-vmhostpatch)[$i].Description + "`n" + ";;;;;;;;;;")
}

#Send captured ESX Header information to the CSV file
write-output ( $allhosts[$j].Name + ";" + $allhosts[$j].Version + ";" + $allhosts[$j].Build + ";" + $allhosts[$j].Manufacturer + ";" + $allhosts[$j].Model + ";" +  $allhosts[$j].ProcessorType + ";" + [Math]::Round($allhosts[$j].MemoryTotalMB/1024) + ";" + [Math]::Round(((get-date).Date - $allhosts[$j].ExtensionData.Summary.Runtime.Boottime).Totaldays) + ";" + ($allhosts[$j] | Get-Compliance -Baseline (Get-PatchBaseline 'Critical Host Patches')).Status + ";" + ($allhosts[$j] | Get-Compliance -Baseline (Get-PatchBaseline 'Non-Critical Host Patches')).Status + ";" + $hostpatchlist) >> $outputfilename
}
else
{
write-output ( $allhosts[$j].Name + ";" + $allhosts[$j].Version + ";" + $allhosts[$j].Build + ";" + $allhosts[$j].Manufacturer + ";" + $allhosts[$j].Model + ";" +  $allhosts[$j].ProcessorType + ";" + [Math]::Round($allhosts[$j].MemoryTotalMB/1024) + ";" + [Math]::Round(((get-date).Date - $allhosts[$j].ExtensionData.Summary.Runtime.Boottime).Totaldays) + ";" + "N/A" + ";" + "N/A" + ";" + "N/A") >> $outputfilename
}

write-output $vm_header >> $outputfilename

for($i=0;$i -lt $allvms.Length;$i++)
{
if($allvms[$i].Host.Name -eq $allhosts[$j].Name)        #Compare ESX Server Name as against one of the VM's parameter storing the ESX HostName
{
$allvms[$i].Name

#Populating string with IP Addresses for VM's with many IP Addresses.
for($k=0;$k -lt $allvms[$i].Guest.IPAddress.Count;$k++)
{
$vmipaddress = $vmipaddress + $allvms[$i].Guest.IPAddress[$k] + ", "
}
#Removing the last comma in the IP Address string variable
if(($vmipaddress.Trim()).Length -gt 1)
{
$vmipaddress = $vmipaddress.substring(0,$vmipaddress.Length-2)
}

#Populating string with Datastores for a VM
$vmdatastores = Get-Datastore -VM $allvms[$i].Name
if($vmdatastores.Length)
{
for($k=0;$k -lt $vmdatastores.Length;$k++)    #Multiple Disks
{
$strvmdatastores = $strvmdatastores + $vmdatastores[$k] + ", "
}
$strvmdatastores = $strvmdatastores.substring(0,$strvmdatastores.Length-2)
}
else    #Single Disk
{
$strvmdatastores = $vmdatastores.Name
}

#Populating string with VM's Disk Utilization
for($k=0;$k -lt (get-vmguest -VM $allvms[$i].Name).ExtensionData.Disk.Count;$k++)
{
$disk_content = "Disk Path: " +  (get-vmguest -VM $allvms[$i].Name).ExtensionData.Disk[$k].DiskPath + " Capacity: " + [Math]::Round((((get-vmguest -VM $allvms[$i].Name).ExtensionData.Disk[$k].Capacity)/1073741824),1) + " Free Space: " + [Math]::Round((((get-vmguest -VM $allvms[$i].Name).ExtensionData.Disk[$k].FreeSpace)/1073741824),1) + " / "
$disk_alldisks = $disk_alldisks + $disk_content
}
#Removing the last comma in the Disk Utilization string variable
if(($disk_alldisks).Length -gt 1)
{
$disk_alldisks = $disk_alldisks.substring(0,$disk_alldisks.Length-2)
}

#Sending output to File
write-output ( ";" + $allvms[$i].Name + ";" + $allvms[$i].PowerState + ";" + $allvms[$i].Version + ";" + $allvms[$i].NumCpu + ";" + $allvms[$i].MemoryMB + ";" + $allvms[$i].Guest.OSFullName + ";" + $allvms[$i].Guest.ExtensionData.ToolsStatus + ";" + $vmipaddress + ";" + $allvms[$i].HardDisks.Length + ";" + $strvmdatastores + ";" + $disk_alldisks + ";" + [Math]::Round((Get-Stat -Entity $allvms[$i].Name -Start (Get-Date).AddDays(-7) -Stat cpu.usage.average | Measure-Object Value -Average).Average, 2) + ";" + [Math]::Round((Get-Stat -Entity $allvms[$i].Name -Start (Get-Date).AddDays(-7) -Stat mem.usage.average | Measure-Object Value -Average).Average,2)) >> $outputfilename

#Reinitializing variable to empty string
$vmipaddress = ""
$strvmdatastores=""
$disk_alldisks = ""
$hostpatchlist = ""
}
}
}

write-output ("Total Hosts" + ";" + $allhosts.Count) >> $outputfilename
write-output ("Total VM's" + ";" + $allvms.Count) >> $outputfilename

Script to map Virtual Center Datastores to local computer powershell drives and download all the files from the datastore

Leave a comment

Connect-VIServer <vCenter Server Name>
 clear
 $ds=Get-Datastore -Name *
 foreach($i in $ds)
 {
 if($i.Name -match " "){                                              #Replace datastore names having "space" with "_" (underscore)
 $j=$i.Name -replace (" ","_")
 }
 elseif($i.Name -match ":"){                                  #Replace datastore names having ":" with "_" (underscore
 $j=$i.Name -replace (":","_")
 }
 $j
 New-PSDrive -Name psdrive_$j -PSProvider vimdatastore -Root "/" -Location $i
 }
 Copy-DatastoreItem -Item psdrive:\<directory name>\vmware* -Destination N:\test\
 Remove-PSDrive -Name psdrive

Script to browse Powershell Drives

Leave a comment

clear
 $j=@()
 $i=Get-PSDrive
 foreach($j in $i)
 {
 $j=$j.Name+(":")
 $j
 if($j -match "cert"){
 Set-Location $j
 Get-ChildItem -Recurse
 }
 }

Short Powercli Script to create VM’s

Leave a comment

1..10 | foreach {New-VM -VMHost 192.168.1.5 -Datastore datastore1 -DiskMB 1 -DiskStorageFormat Thin -Name test$_}

Delete VM’s using Powercli script with settings imported from CSV file

Leave a comment

$temp = Import-Csv C:\Users\Administrator\Desktop\book1.csv
 foreach($i in $temp)
 {
 Remove-VM -Server 192.168.1.3 -DeletePermanently -VM $i.Name -ErrorAction SilentlyContinue -Confirm:$false
 }

Add Virtual Machines using PowerCli script with settings imported from CSV file

Leave a comment

$hostip = "192.168.1.3"
 $vminventory = Import-Csv C:\Users\Administrator\Desktop\book1.csv
 Connect-VIServer $hostip
 ForEach($i in $vminventory)
 {
 New-VM -VMHost $hostip -Datastore datastore1 -Name $i.Name -MemoryMB $i.Memory -NumCpu $i.Cpu -DiskMB $i.Disk -NetworkName $i.Network -ErrorAction Inquire
 }

vSphere 4 performance chart clarity

Leave a comment

Under performance charts of a VM, under Network performance, what are numbers 4000, 4001 and under CPU performance, what are numbers 0,1,2..8.

Under CPU Performance chart, numbers 0,1,2..8 are the number of processors allocated to the VM starting from 0.

The <VM-Name> is the aggregate for all vCPUs of a VM.

Under Network Performance chart, for e.g. if  we have added 2 NIC’s to the VM, under performance charts VMNIC0 is represented with 4000 and VMNIC1 represented with 4001. But, I don’t see more than two virtual NIC’s when added under the performance chart of a VM. Dunno why?

But I hope this post offers some clarity.

Older Entries

Follow

Get every new post delivered to your Inbox.