VMware Global Community
Halukk
Contributor
Contributor

PowerShell ile Snapshotlarin Bulunmasi

Herkeze selam,

Ben gruba yeni katildim ve elimde olan bazi PowerShell scriptleri sizlerle paylasmak istedim. Eger sizinde gunluk vCenter yonetiminde kullandiginiz scriptler varsa lutfen paylasin. Ben otomasyona cok onem veriyorum. Asagidaki scripti PowerCLi in kurulu oldugu sunucudan calistirmaniz gerekiyor. Ben vCenter sunucusunda bu scriptin calismasi icin gunluk bir program hazirladim her sabah saat 7:00 de email gonderiyor. Script oncelikle vSphere e baglaniyo, snapshot i olan sanal makineleri buluyor, HTML liste hazirliyor ve istediniz email adresine email atiyor. Ornek email de konuyo ekliyorum.

#############################################################################

# Oncelikle PowerShell e VMware eklentisini yukluyoruz

Add-PSSnapin VMware.VimAutomation.Core

# Burada gondericegimiz HTML formatindaki email i sekillendiriyoruz

$head = "<style>"

$head = $head + "BODY{background-color:white;}"

$head = $head + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"

$head = $head + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:Salmon}"

$head = $head + "TD{border-width: 1px;padding: 2px 20px 0px 2px;border-style: solid;border-color: black;background-color:Wheat}"

$head = $head + "</style>"

# SMTP (Email sunucusu) bilgileri

$smtpServer = "10.0.103.140"

$strFrom = "VMSnapshots@sirket.com"

$strTo = "admin@sirket.com"

$strSubject = “Snapshot list - ” + (get-date -DisplayHint date)

$strBody = "Snapshot biligileri ekte"

$strMail = "<H2><u>" + $strSubject + "</u></H2>"

# vCenter sunucularinin listesi eger birden cok ise "," virgul ile ayiriniz

#Kullanici adi ver sifre bolumunde kullanacaginiz kullnici adi vcenter a kayitli ve gerekli yetkileri olan bir kullici olmasi gerekiyor. Ben bunun icin vSphere de bir servis hesabi yarattim.

$Servers="sunucu1","Sunucu2"

foreach ($Server in $Servers){

  Connect-VIServer $Server -Protocol https -User 'kullnici adi' -Password 'sifre'

  $date=Get-Date -uFormat "%Y%m%d%H%M%S"

  $strOutFile = "c:\snapshot_list$date.htm"

  $strSubject = “Snapshot Listesi $Server - ” + (get-date -DisplayHint date)

  # Sanal makinelerin listesi

  $vms = Get-VM

  $myCol = @()

  ForEach ($vm in $vms){

  $snapshots = Get-SnapShot -VM $vm

  if ($snapshots.Name.Length -ige 1 -or $snapshots.length){

  ForEach ($snapshot in $snapshots){

  $myObj = "" | Select-Object VM, Snapshot, Created, Description

  $myObj.VM = $vm.name

  $myObj.Snapshot = $snapshot.name

  $myObj.Created = $snapshot.created

  $myObj.Description = $snapshot.description

  $myCol += $myObj

  }

  }

}

  # HTML formatindaki emailin verileri

  if ($myCol -ne $null){

  $myCol | Sort-Object VM | ConvertTo-HTML -Head $head -Body $strMail | Out-File $strOutFile

  # Mail the output file

  $msg = new-object Net.Mail.MailMessage

  $att = new-object Net.Mail.Attachment($strOutFile)

  $smtp = new-object Net.Mail.SmtpClient($smtpServer)

  $msg.From = $strFrom

  $msg.To.Add($strTo)

  $msg.Subject = $strSubject

  $msg.IsBodyHtml = 1

  $msg.Body = Get-Content $strOutFile

  $msg.Attachments.Add($att)

  $msg.Headers.Add("message-id", "<3BD50098E401463AA228377848493927-1>") # Adding a Bell Icon for Outlook users

  $smtp.Send($msg)}

  DisConnect-VIServer -Server * -Force -Confirm:$false

}

Ornek email

Capture.PNG

Eger script i indermek isterseniz onuda .ps1 (powershell script) formatinda ekliyorum.

Saygilar,

Haluk Kocaman

0 Kudos
1 Reply
ErcanSahin
Enthusiast
Enthusiast

Emeğinize sağlık.

Ercan SAHIN http://forum.netcom.com.tr
0 Kudos