Skip to content
Menu
  • CloudThesis-Home
  • Cloud Management
    • VMware Cloud
    • VCF (VMware Cloud Foundation)
    • vRO
    • vRA
    • vRealize Log Insight
    • vCloud Director
    • Hybrid Cloud Extension (HCX)
    • vCloud
    • Cloud on AWS
  • VMware
    • vCenter
    • vSphere
      • Generic vSphere Commands
    • ESXi
    • VMware vRealize Suite Lifecycle Manager –8 Deployment Guide
    • VMware Site Recovery Manager (SRM)
    • vRealize Network Insight
    • VMware Horizon
  • Power CLI
    • PowerCLI Snippets
  • NSX
  • vSAN
    • vSAN 7
  • Nutanix
  • VOIP
  • Microsoft
    • Azure Cloud
    • Microsoft Windows Server
    • Hypervisor
    • Create Azure VMs (Portal)
    • Create Azure VMs – PowerShell
  • Author
  • Contact

Snapshot details of multiple domain with PowerCLI scripts .

Posted on June 18, 2019

What is VMware snapshot ?

A VMware snapshot is a copy of the virtual machine’s disk file (VMDK) at a given point in time. Snapshots provide a change log for the virtual disk and are used to restore a VM to a particular point in time when a failure or system error occurs.

 

Snapshot details powerCLI script :

PowerCLI-10.0.0-Logo

Open the PowerCLI icon (Run as Administrator) and you can execute commands manually, run scripts manually or automate your scripts with Task Scheduler (see below).

Function Test-Cred{
Param ($cred)

$username = $cred.username
$password = $cred.GetNetworkCredential().password

# Get current domain using logged-on user's credentials
$CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)

if ($domain.name)
{
return "TRUE"
}
else
{
return "FALSE"
}
}


Clear-Host
del *.csv

Set-ExecutionPolicy RemoteSigned
Set-ExecutionPolicy Unrestricted

$outfile=""
$vc = "SvrList.txt"

$i=0
$j=0
$k=0
$l=0

$prodflag=0
$drflag=0
$uatflag=0


$prodCreds="NULL"
$DrCreds="NULL"
$uatCreds="NULL"

$vcnt = (Get-Content $vc).Count

Get-Content $vc | ForEach-Object {

# Write-Host $_
If ( $_ | Select-String -Pattern "prod.test.com")
{
# Write-Host " Server : $_ belongs To prod Domain"
$prodflag=1
}

if ($_ | Select-String -Pattern "uat.test.com")
{
# Write-Host " Server : $_ belongs To uat Domain"
$drflag=1

}

if ($_ | Select-String -Pattern "dr.test.com")
{
# Write-Host " Server : $_ belongs To dr Domain"
$uatflag=1
}

}

if($prodflag -eq 1)
{
Write-Host " You need to provide Creds for prod Domain"
$prodCreds=$host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "NetBioUserName")

$flag=Test-Cred($prodCreds)

if ($flag -eq "TRUE")
{
Write-Host " Authentication Successful For prod Domain" -f green
}
else
{
Write-Host " Authentication Failed For prod Domain .... exiting" -f green
exit
}
}

if($uatflag -eq 1)
{
Write-Host " You need to provide Creds for uat Domain"
$uatCreds=$host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "NetBioUserName")


$flag=Test-Cred($uatCreds)

if ($flag -eq "TRUE")
{
Write-Host " Authentication Successful For uat Domain" -f green
}
else {
Write-Host " Authentication Failed For uat Domain .... exiting" -f green
exit
}
}

if($drflag -eq 1)
{
Write-Host " You need to provide Creds for DR Domain"
$drCreds=$host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "NetBioUserName")

$username = $drCreds.username
$password = $drCreds.GetNetworkCredential().password

# Get current domain using logged-on user's credentials
$CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)

if($domain)
{

Write-Host " Authentication Successful For dr Domain" -f green
}
else {
Write-Host " Authentication Failed For dr Domain .... exiting" -f green
exit
}
}


Get-Content $vc | ForEach-Object {

$vcenter=$_

$outfile = ".\$($vcenter).csv"

try
{
$bestping = (Test-Connection -ComputerName $vcenter -Count 10 -ErrorAction STOP | Sort ResponseTime)[0].ResponseTime
}
catch
{
Write-Warning $_.Exception.Message
$bestping = "Unable to connect"
continue
}


if ($bestping -eq "Unable to connect")
{
Write-Host "Unable to connect to $vcenter ..... Skipping" -f cyan
exit

}


try{
Write-Host "Connecting to vCenter : $vcenter, please wait.." -f blue

if ($vcenter | Select-String -Pattern "dr.test.com")
{
Connect-ViServer -server $vcenter -Credential $drCreds| Out-Null
}
elseif ($vcenter | Select-String -Pattern "uat.test.com")
{
Connect-ViServer -server $vcenter -Credential $uatCreds| Out-Null
}
elseif ($vcenter | Select-String -Pattern "prod.test.com")
{
Connect-ViServer -server $vcenter -Credential $prodCreds| Out-Null
}


}
catch{
#Write-Warning $_.Exception.Message
$exception = $_.Exception
# Write-debug "Could not connect to vCenter"
Write-Host "Exiting ....." -f red
exit
}

Write-Host " Connected. " -f green
Write-Host "Processing $vcenter"

$i++
$j=0
$k=0
$l=0

try{
Write-Progress -Activity "Scanning vCenter .... " -Status ("vCenter : {0}" -f $vcenter) -PercentComplete ($i/$vcnt*100) -Id 0
}
catch [System.DivideByZeroException]{

}

$outfile=$vcenter+".csv"

$dcs=Get-Datacenter

$data=foreach( $dc in $dcs)
{

$j++ 
$k=0
$l=0

$clusters = $dc | Get-Cluster

try{
Write-Progress -Activity "Scanning DataCenter .... " -Status ("Datacenter : {0}" -f $dc.Name) -PercentComplete ($j/$dcs.count*100) -Id 1
}
catch [System.DivideByZeroException]{

}

foreach($cluster in $clusters)
{
$k++
$l=0

try{
Write-Progress -Activity " Scanning Clusters .... " -Status ("Cluster : {0}" -f $cluster.Name) -PercentComplete ($k/$clusters.count*100) -Id 2
}
catch [System.DivideByZeroException]{

}


$vms = $cluster | Get-VM

$result = $vms | Get-Snapshot

if (!$result)
{ 
# Write-Host "No Snapshots in Cluster : $cluster.Name"
continue
}


foreach ($snap in $result)
{
$l++

<#
try{
Write-Progress -Activity " Processing SnapShot info .... " -Status ("Cluster : {0}" -f $snap) -PercentComplete ($l/$clusters.count*100) -Id 2
}
catch [System.DivideByZeroException]{}

#>

$snapevent = Get-VIEvent -Entity $snap.VM -Types Info -Finish $snap.Created -MaxSamples 1 | Where-Object {$_.FullFormattedMessage -imatch 'Task: Create virtual machine snapshot'}

if ($snapevent -ne $null)
{
Write-Host ( "VM: "+ $snap.VM + ". Snapshot '" + $snap + "' created on " + $snap.Created.DateTime + " by " + $snapevent.UserName +".") 

} 
else {
Write-Host ("VM: "+ $snap.VM + ". Snapshot '" + $snap + "' created on " + $snap.Created.DateTime + ". This event is not in vCenter events database")
$snapevent.UserName = "This event is not in vCenter events database"
}

New-Object PSObject -Property @{
VCenter = $vcenter
DataCenter = $dc 
Cluster = $cluster.name
EsxHost = $snap.VM.Host
VMName = $snap.VM 
PowerState = $snap.PowerState
SnapshotName = $snap
SizeGB = $snap.SizeGB
Description = $snap.Description 
# Filename
DateCreated = $snap.Created.DateTime
CreatedBy = $snapevent.UserName
Quiesced = $snap.Quiesced
} | Select VCenter,Datacenter,Cluster,EsxHost,VMName,PowerState,SnapshotName,SizeGB,Quiesced,Description,DateCreated,CreatedBy 
}
}
}
# }



$data | Export-Csv $outfile -NoTypeInformation

Write-Host "Disconnecting from vCenter : $vcenter"

Disconnect-VIServer $vcenter -confirm:$false | Wait-Task | out-null

# Wait-Task -Task $task

Write-Host "Processing Done Dumping : $outfile " -f green

}

<#
Write-Host "Finalizing ......" -f cyan

$swp = @()
ls *.csv | %{
$tmp = Import-CSV $_
$swp += $tmp
}
$swp | Export-CSV .\All-vCenter-SnapInfo.csv -NoTypeInformation

Write-Host "Completed....Please Check the Generated : All-vCenter-Info.csv" -f cyan
#>

 

Thanks hope you like it.

Rajiv Pandey.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search Topic

Categories

  • Azure Cloud
  • Cloud Management
  • Cloud on AWS
  • Create Azure VMs – PowerShell
  • Create Azure VMs (Portal)
  • ESXi
  • Generic vSphere Commands
  • Hybrid Cloud Extension (HCX)
  • Hypervisor
  • Microsoft
  • Microsoft Azure Cloud and Services
  • Microsoft Windows Server
  • NSX
  • Nutanix
  • Power CLI
  • PowerCLI Snippets
  • vCenter
  • VCF (VMware Cloud Foundation)
  • vCloud
  • vCloud Director
  • VMware
  • VMware Cloud
  • VMware Horizon
  • VMware Site Recovery Manager (SRM)
  • VMware vRealize Suite Lifecycle Manager – 8.0
  • vRA
  • vRealize Log Insight
  • vRealize Network Insight
  • vRO
  • vSAN
  • vSAN 7
  • vSphere
  • Windows Servers
©2025 | WordPress Theme by Superbthemes.com