L'objectif de ce script est de faire une vérification des alarmes dans vSphere
<#
. AUTEUR : Kyle Ruddy
. MODIFICATIONS : Antoine JOVELIN
. FONCTION : Verification des alarmes
. UTILISATION :
- Lancer le script
.SYNOPSIS
This function lists the triggered alarms for the specified entity in vCenter
.DESCRIPTION
List the triggered alarms for the given object
.NOTES
Author: Kyle Ruddy, @kmruddy, kmruddy.com
.PARAMETER VM
Specifies the name of the VM
.PARAMETER VMHost
Specifies the name of the VMHost
.PARAMETER Datacenter
Specifies the name of the Datacenter
.PARAMETER Datastore
Specifies the name of the Datastore
.EXAMPLE
Get-TriggeredAlarm -VM VMname
Entity Alarm AlarmStatus AlarmMoRef EntityMoRef
---- ---- ---- ---- ----
VMname Name Yellow Alarm-MoRef Entity-MoRef
#>
$StartTime = Get-Date
cls
# Declaration des fonctions
$herestring = @"
***** L'INSTALLATION A ECHOUEE *****
POUR INSTALLER POWERCLI :
1- RECUPERER LE MODULE DEPUIS CE LIEN : "https://developer.vmware.com/web/tool/vmware-powercli"
2- COPIER LE CONTENU DE L'ARCHIVE DANS "C:\Users\NOM_DU_USER\Documents\WindowsPowerShell\Modules"
3- RELANCER LE SCRIPT
"@
function verifPowerLI {
if($MyModules.name -match "VMware.PowerCLI"){
Write-Host "Le module PowerCLI est installé. Le script va continuer" -ForegroundColor Green
}else{
Write-Host "Le module PowerCLI n'est PAS installé. Le script ne pourra pas continuer" -ForegroundColor Yellow
do{
$reponse = Read-Host " Installer le module VMware.PowerCLI ? [oui/non] "
Switch ($reponse){
oui{
Install-Module -name vmware.powercli -Scope CurrentUser -Confirm:$false -Force
if($MyModules -notcontains "VMware.PowerCLI"){
Write-Host $herestring -ForegroundColor red
sleep 3
Write-Host " Fin du script dans quelques secondes" -ForegroundColor yellow
1..5 | Sort-Object -Descending | %{Start-Sleep -Seconds 1;Write-Host " $_ " -ForegroundColor yellow}
echo " "
exit
}
}
non{
Write-Host " Fin du script dans quelques secondes" -ForegroundColor yellow
1..5 | Sort-Object -Descending | %{Start-Sleep -Seconds 1;Write-Host " $_ " -ForegroundColor yellow}
echo " "
exit
}
}
}while(($reponse -ne "oui") -and ($reponse -ne "non"))
}
}
function vcenter-connect{
$VC = Read-Host -Prompt " Entrer le nom de vCenter "
$cred = Get-Credential
Write-Host;
Write-Host -ForegroundColor cyan "Connexion à vCenter. Merci de patienter..."
Connect-VIServer $VC -Credential $cred
Write-Host;
}
function vcenter-disconnect{
Write-Host;
Write-Host -ForegroundColor cyan "Déconnexion de vCenter..."
Disconnect-viserver -Server $Global:DefaultVIServer -Confirm:$false -Force
Write-Host;
}
function Get-TriggeredAlarm {
[CmdletBinding()]
param(
[string]$VM,
[string]$VMHost,
[string]$Datacenter,
[string]$Datastore
)
BEGIN {
switch ($PSBoundParameters.Keys) {
'VM' {$entity = Get-VM -Name $vm -ErrorAction SilentlyContinue}
'VMHost' {$entity = Get-VMHost -Name $VMHost -ErrorAction SilentlyContinue}
'Datacenter' {$entity = Get-Datacenter -Name $Datacenter -ErrorAction SilentlyContinue}
'Datastore' {$entity = Get-Datastore -Name $Datastore -ErrorAction SilentlyContinue}
default {$entity = $null}
}
if ($null -eq $entity) {
Write-Warning "No vSphere object found."
break
}
}
PROCESS {
if ($entity.ExtensionData.TriggeredAlarmState -ne "") {
$alarmOutput = @()
foreach ($alarm in $entity.ExtensionData.TriggeredAlarmState) {
$tempObj = "" | Select-Object -Property Entity, Alarm, AlarmStatus, AlarmMoRef, EntityMoRef
$tempObj.Entity = Get-View $alarm.Entity | Select-Object -ExpandProperty Name
$tempObj.Alarm = Get-View $alarm.Alarm | Select-Object -ExpandProperty Info | Select-Object -ExpandProperty Name
$tempObj.AlarmStatus = $alarm.OverallStatus
$tempObj.AlarmMoRef = $alarm.Alarm
$tempObj.EntityMoRef = $alarm.Entity
$alarmOutput += $tempObj
}
$alarmOutput | Format-Table -AutoSize
}
}
}
# Debut du script
vcenter-connect
Write-Host;
Write-Host " Vérification des modules nécessaires " -ForegroundColor Cyan
1..3 | Sort-Object -Descending | %{Start-Sleep -Seconds 1}
$MyModules = Get-Module -ListAvailable | select name
Write-Host;
verifPowerLI
Write-Host;
Write-Host -fore cyan " ---- Verification des alarmes ---- "
Write-Host;
do{
$reponse = Read-Host " Choisir entre VM, VMHost, Datacenter et Datastore : "
switch ($reponse)
{
#Appel de la fonction networking
VM
{
$VM = Read-Host -Prompt " Entrer le nom complet d'une VM : "
Get-TriggeredAlarm -VM $VM
}
#Appel de la fonction RenameNetwork
VMHost
{
$VMHost = Read-Host -Prompt " Entrer le nom complet d'un ESXi : "
Get-TriggeredAlarm -VM $VMHost
}
#Appel de la fonction NewForest
Datacenter
{
$Datacenter = Read-Host -Prompt " Entrer le nom complet d'un Datacenter : "
Get-TriggeredAlarm -VM $Datacenter
}
#Appel de la fonction ChildDomain
Datastore
{
$Datastore = Read-Host -Prompt " Entrer le nom complet d'un Datastore : "
Get-TriggeredAlarm -VM $Datastore
}
}
}while ( ($reponse -ne "VM") -and ($reponse -ne "VMHost") -and ($reponse -ne "Datacenter") -and ($reponse -ne "Datastore"))
# Fin du script et deconnexion
Write-Host;
vcenter-disconnect
Write-Host -fore cyan " ---- Durée du Scipt ---- "
Write-Host;
$EndTime = Get-Date
$duration = [math]::Round((New-TimeSpan -Start $StartTime -End $EndTime).TotalMinutes,2)
Write-Host -fore green @"
Fin du Script
StartTime: $StartTime
EndTime: $EndTime
Duree : $duration minutes
"@
Write-Host -fore cyan " ---------------------- "