Scripts PowerCLI : Différence entre versions
(Page créée avec « {{ Introduction | Cette page regroupe un ensemble de scripts PowerCLI/Powershell }} Catégorie:PowerCLI ») |
|||
| Ligne 1 : | Ligne 1 : | ||
{{ Introduction | Cette page regroupe un ensemble de scripts PowerCLI/Powershell }} | {{ Introduction | Cette page regroupe un ensemble de scripts PowerCLI/Powershell }} | ||
| + | |||
| + | ==Snapshot== | ||
| + | |||
| + | * Script pour lister les VMs ayant un snapshot | ||
| + | <br> | ||
| + | <syntaxhighlight lang=powershell> | ||
| + | #Server VSphere | ||
| + | $server = "win2k12-Vcenter.admin.pass.be" | ||
| + | |||
| + | #Connexion au vsphere | ||
| + | Connect-VIServer -Server $server -WarningAction SilentlyContinue | ||
| + | |||
| + | #Liste des VM avec le nom du snapshot et la description | ||
| + | Get-VM | Get-Snapshot | Select VM,Name,Description | ||
| + | |||
| + | pause | ||
| + | |||
| + | Disconnect-VIServer -Server $server -Force | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | * Script pour réaliser un rapport des snapshot et l'envoyer par mai | ||
| + | <br> | ||
| + | <syntaxhighlight lang=powershell> | ||
| + | Param ( | ||
| + | [Alias("Host")] | ||
| + | [string]$VIServer = "win2k12-vcenter.admin.pass.be", | ||
| + | [string]$User = "Vsphere.local\Administrator", | ||
| + | [string]$Password="TOrtu€N1nj@", | ||
| + | [string]$PathToReport, | ||
| + | |||
| + | [string]$To = "devleeschauwer@pass.be", | ||
| + | [string]$From = "vmreports@pass.be", | ||
| + | [string]$SMTPServer = "mail.pass.be" | ||
| + | ) | ||
| + | |||
| + | $Header = @" | ||
| + | <style> | ||
| + | TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;} | ||
| + | TR:Hover TD {Background-Color: #C1D5F8;} | ||
| + | TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;} | ||
| + | TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;} | ||
| + | .odd { background-color:#ffffff; } | ||
| + | .even { background-color:#dddddd; } | ||
| + | </style> | ||
| + | <title> | ||
| + | Snapshot Report - $VIServer | ||
| + | </title> | ||
| + | "@ | ||
| + | |||
| + | Function Set-AlternatingRows { | ||
| + | [CmdletBinding()] | ||
| + | Param( | ||
| + | [Parameter(Mandatory=$True,ValueFromPipeline=$True)] | ||
| + | [object[]]$HTMLDocument, | ||
| + | |||
| + | [Parameter(Mandatory=$True)] | ||
| + | [string]$CSSEvenClass, | ||
| + | |||
| + | [Parameter(Mandatory=$True)] | ||
| + | [string]$CSSOddClass | ||
| + | ) | ||
| + | Begin { | ||
| + | $ClassName = $CSSEvenClass | ||
| + | } | ||
| + | Process { | ||
| + | [string]$Line = $HTMLDocument | ||
| + | $Line = $Line.Replace("<tr>","<tr class=""$ClassName"">") | ||
| + | If ($ClassName -eq $CSSEvenClass) | ||
| + | { $ClassName = $CSSOddClass | ||
| + | } | ||
| + | Else | ||
| + | { $ClassName = $CSSEvenClass | ||
| + | } | ||
| + | $Line = $Line.Replace("<table>","<table width=""50%"">") | ||
| + | Return $Line | ||
| + | } | ||
| + | } | ||
| + | |||
| + | #Desactiver la securité | ||
| + | Set-ExecutionPolicy RemoteSigned | ||
| + | |||
| + | #Chargement du module Vmware | ||
| + | If (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) | ||
| + | { Try { Add-PSSnapin VMware.VimAutomation.Core -ErrorAction Stop } | ||
| + | Catch { Throw "Problem loading VMware.VimAutomation.Core snapin because ""$($Error[1])""" } | ||
| + | } | ||
| + | |||
| + | #Connexion au vsphere | ||
| + | $connection = Connect-VIServer -Server $VIServer -User $User -Password $Password -WarningAction SilentlyContinue | ||
| + | |||
| + | $Report = Get-VM | Get-Snapshot | Select VM,Name,Description,@{Label="Size";Expression={"{0:N2} GB" -f ($_.SizeGB)}},Created | ||
| + | If (-not $Report) | ||
| + | { $Report = New-Object PSObject -Property @{ | ||
| + | VM = "No snapshots found on any VM's controlled by $VIServer" | ||
| + | Name = "" | ||
| + | Description = "" | ||
| + | Size = "" | ||
| + | Created = "" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | $Report = $Report | Select VM,Name,Description,Size,Created | ConvertTo-Html -Head $Header -PreContent "<p><h2>Snapshot Report - $VIServer</h2></p><br>" | Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd | ||
| + | |||
| + | #Si on veut enregistrer le rapport à un endroit | ||
| + | #$Report | Out-File $PathToReport\SnapShotReport.html | ||
| + | |||
| + | #Fonction d'envoi de mail | ||
| + | $MailSplat = @{ | ||
| + | To = $To | ||
| + | From = $From | ||
| + | Subject = "$VIServer Snapshot Report" | ||
| + | Body = ($Report | Out-String) | ||
| + | BodyAsHTML = $true | ||
| + | SMTPServer = $SMTPServer | ||
| + | } | ||
| + | |||
| + | Send-MailMessage @MailSplat | ||
| + | |||
| + | #Deconnexion du VIServer sans prompt | ||
| + | Disconnect-VIServer -Server $connection -Force -Confirm:$false | ||
| + | </syntaxhighlight> | ||
| + | |||
[[Catégorie:PowerCLI]] | [[Catégorie:PowerCLI]] | ||
Version du 16 février 2016 à 15:23
Cette page regroupe un ensemble de scripts PowerCLI/Powershell
Snapshot
- Script pour lister les VMs ayant un snapshot
#Server VSphere
$server = "win2k12-Vcenter.admin.pass.be"
#Connexion au vsphere
Connect-VIServer -Server $server -WarningAction SilentlyContinue
#Liste des VM avec le nom du snapshot et la description
Get-VM | Get-Snapshot | Select VM,Name,Description
pause
Disconnect-VIServer -Server $server -Force- Script pour réaliser un rapport des snapshot et l'envoyer par mai
Param (
[Alias("Host")]
[string]$VIServer = "win2k12-vcenter.admin.pass.be",
[string]$User = "Vsphere.local\Administrator",
[string]$Password="TOrtu€N1nj@",
[string]$PathToReport,
[string]$To = "devleeschauwer@pass.be",
[string]$From = "vmreports@pass.be",
[string]$SMTPServer = "mail.pass.be"
)
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TR:Hover TD {Background-Color: #C1D5F8;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
.odd { background-color:#ffffff; }
.even { background-color:#dddddd; }
</style>
<title>
Snapshot Report - $VIServer
</title>
"@
Function Set-AlternatingRows {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
[object[]]$HTMLDocument,
[Parameter(Mandatory=$True)]
[string]$CSSEvenClass,
[Parameter(Mandatory=$True)]
[string]$CSSOddClass
)
Begin {
$ClassName = $CSSEvenClass
}
Process {
[string]$Line = $HTMLDocument
$Line = $Line.Replace("<tr>","<tr class=""$ClassName"">")
If ($ClassName -eq $CSSEvenClass)
{ $ClassName = $CSSOddClass
}
Else
{ $ClassName = $CSSEvenClass
}
$Line = $Line.Replace("<table>","<table width=""50%"">")
Return $Line
}
}
#Desactiver la securité
Set-ExecutionPolicy RemoteSigned
#Chargement du module Vmware
If (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue))
{ Try { Add-PSSnapin VMware.VimAutomation.Core -ErrorAction Stop }
Catch { Throw "Problem loading VMware.VimAutomation.Core snapin because ""$($Error[1])""" }
}
#Connexion au vsphere
$connection = Connect-VIServer -Server $VIServer -User $User -Password $Password -WarningAction SilentlyContinue
$Report = Get-VM | Get-Snapshot | Select VM,Name,Description,@{Label="Size";Expression={"{0:N2} GB" -f ($_.SizeGB)}},Created
If (-not $Report)
{ $Report = New-Object PSObject -Property @{
VM = "No snapshots found on any VM's controlled by $VIServer"
Name = ""
Description = ""
Size = ""
Created = ""
}
}
$Report = $Report | Select VM,Name,Description,Size,Created | ConvertTo-Html -Head $Header -PreContent "<p><h2>Snapshot Report - $VIServer</h2></p><br>" | Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd
#Si on veut enregistrer le rapport à un endroit
#$Report | Out-File $PathToReport\SnapShotReport.html
#Fonction d'envoi de mail
$MailSplat = @{
To = $To
From = $From
Subject = "$VIServer Snapshot Report"
Body = ($Report | Out-String)
BodyAsHTML = $true
SMTPServer = $SMTPServer
}
Send-MailMessage @MailSplat
#Deconnexion du VIServer sans prompt
Disconnect-VIServer -Server $connection -Force -Confirm:$false