L'objectif de ce script est de faire une vérification de la communication réseau par un Ping sur une VM ou un hôte
<#
. AUTEUR : Antoine JOVELIN
. FONCTION : L'objectif de ce script est de vérifier la communication réseau par un Ping sur une VM ou un hôte
. UTILISATION :
- Lancer le script
#>
$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-PingStatus {
param(
[Parameter(ValueFromPipeline=$true)]
[string]$device,
[validateSet("Online","Offline","ObjectTable")]
[String]$getObject
)
begin{
$hash = @()
}
process{
$device| foreach {
if (Test-Connection $_ -Count 1 -Quiet) {
if(-not($GetObject)){write-host -ForegroundColor green "Online: $_ "}
$Hash = $Hash += @{Online="$_"}
}else{
if(-not($GetObject)){write-host -ForegroundColor Red "Offline: $_ "}
$Hash = $Hash += @{Offline="$_"}
}
}
}
end {
if($GetObject) {
$Global:Objects = $Hash | foreach { [PSCustomObject]@{
DeviceName = $_.Values| foreach { "$_" }
Online = $_.Keys| where {$_ -eq "Online"}
offline = $_.Keys| where {$_ -eq "Offline"}
}
}
Switch -Exact ($GetObject)
{
'Online' { $Global:Objects| where 'online'| select -ExpandProperty DeviceName }
'Offline' { $Global:Objects| where 'offline'| select -ExpandProperty DeviceName }
'ObjectTable' { return $Global:Objects }
}
}
}
}
# 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 " ---- Ping une VM ou un hôte ---- "
Write-Host;
$device = Read-Host -Prompt " Entrer le nom complet du device a ping "
Write-Host;
Get-PingStatus -device $device
Write-Host;
# 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 " ---------------------- "