Vérifier la configuration des cartes réseau de vos VM (PowerCLI)

Je vous propose ce script qui vous permet de lister toutes vos VMs. Vous y trouver les informations (Nom de la VM, Hostname, IP, Gateway, Mac Addresse, Nom de la carte réseau) que vous pouvez filtrer a l’aide de la commande “| where”.

Exemple : Monscript.ps1 | Where {$_.VM -eq "Test"}
#Script By Olivier Gosselin - France
#http://www.purplescreen.eu
#Tools PowerCLI 6.5 Release 1

$vCenterFQDN = "FQDN"
#$UservCenter = "Login"
#$PasswordvCenter ="Password"
$CredentialvCenter = Get-Credential -Message "Identifiants pour le vCenter"

Connect-VIServer $vCenterFQDN -User $CredentialvCenter.UserName -Password $CredentialvCenter.Password

$CurentPath = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Definition)
$Date = date | select *

$AllVM = get-vm
#$OneVm = get-vm -Name $option
$ALL = $AllVM | Get-VMGuest
#$PowerVM = get-vm -Name $VMName | select Name,PowerState

    foreach ($VMs in $ALL) {
$VMToolsState = $VMs.ExtensionData.ToolsStatus
$GuestState = $VMs.ExtensionData.GuestState
$Hostname = $VMs.HostName
$ALLIPs = $VMs.nics

#On verifie l'etat des VMwaretools
if ($VMToolsState -eq "toolsOk") {
    foreach ($NetIP in $ALLIPs) {
$VMName = $VMs.VmName
$StateCard = $NetIP.Connected
$Device = $NetIP.Device
$IP = $NetIP.IPAddress | select -Unique
$MAC = $NetIP.MacAddress

$WhereisMAC = $ALLIPs.MacAddress
$WhereisGateway = [array]::indexof($WhereisMAC,"$MAC")

$Gateway = $VMs.ExtensionData.IpStack.IpRouteConfig.IpRoute.Gateway | where {$_.Device -eq "$WhereisGateway"} | where {$_.IpAddress -ne $null}
$GatewayIP = $Gateway.IpAddress | select -Unique

if (!$IP) {
$IP = "None"
}
else {
$IP = (@($IP) -join ',')
}

if (!$Device) {
$Device = "Unknown"
}

if (!$GatewayIP) {
$GatewayIP = "None"
}
#Pour l'affichage et mise en variable Gateway & Cie
$Output = New-Object psobject
$Output | Add-Member -MemberType NoteProperty -Name VM -Value $VMName
$Output | Add-Member -MemberType NoteProperty -Name Hostname -Value $Hostname
$Output | Add-Member -MemberType NoteProperty -Name StateCard -Value $StateCard
$Output | Add-Member -MemberType NoteProperty -Name Device -Value $Device
$Output | Add-Member -MemberType NoteProperty -Name IP -Value $IP
$Output | Add-Member -MemberType NoteProperty -Name MAC -Value $MAC
$Output | Add-Member -MemberType NoteProperty -Name Gateway -Value $GatewayIP

#Pour l'affichage et mise en variable Route
$VMName = $VMs.VmName
$NetworkRoute = $VMs.ExtensionData.IpStack.IpRouteConfig.IpRoute.Network
$NetworkRoute = (@($NetworkRoute) -join '|')
$PrefixLength = $VMs.ExtensionData.IpStack.IpRouteConfig.IpRoute.PrefixLength
$PrefixLength = (@($PrefixLength) -join '|')
$IpAddressRoute = $VMs.ExtensionData.IpStack.IpRouteConfig.IpRoute.Gateway.IpAddress
$IpAddressRoute = (@($IpAddressRoute) -join '|')

$RouteOutput = New-Object psobject
#$RouteOutput | Add-Member -MemberType NoteProperty -Name VM -Value $VMName
$RouteOutput | Add-Member -MemberType NoteProperty -Name NetworkRoute -Value $NetworkRoute
$RouteOutput | Add-Member -MemberType NoteProperty -Name PrefixLength -Value $PrefixLength
$RouteOutput | Add-Member -MemberType NoteProperty -Name IpAddressRoute -Value $IpAddressRoute

#On affiche le resultat
if ($Hostname) {
Write-Host "=============  "$VMs.VmName" ============="
$Output
Write-Host "-----------> Route"
$RouteOutput
}
}
}
else {
$VMName = $VMs.VmName
$PowerVM = get-vm -Name $VMName | select Name,PowerState

if ($PowerVM.PowerState -eq "PoweredOff") {
$ErreurVM = "Erreur : La VM ($VMName) est eteinte"
}
elseif ($GuestState -eq "notRunning") {
$ErreurVM = "Erreur : Les VMWare Tool ($VMName) ne fonctionne pas"
}
elseif ($VMToolsState -eq "toolsNotInstalled") {
$ErreurVM = "Erreur : Les VMWare tools pour cette VM ($VMName) ne sont pas installer"
}
#On ecrit les erreurs dans un fichier TXT
$Day = $Date.Day
$Month = $Date.Month
$Hour = $Date.Hour
$Minute = $Date.Minute
Add-Content -Path $CurentPath"\Erreur.log" -Value "[$Day/$Month-$Hour.$Minute]$ErreurVM"
}
}