Powercli : Check and connect VMs network cards

Ce message est également disponible en : French

With this script you will be able to export a list of VMs with Network Cards disconnected. The script will generate a csv file. And at the end of the script you will be able to modify all the network cards that were in a disconnected states.

 


#########################################################################################
## Script check VM netword cards                                                                                                    #
##                                                                                                                                                       #
##                                                                                                                                                       #
## Julien VARELA – 02/10/2015    V1.0                                                                                                  #
#########################################################################################

## Check VMware Modules
$PSNAP= Get-PSSnapin VMware.VimAutomation.Core -erroraction ‘silentlycontinue’
if ($? -eq $false)
{
##Load VMware Modules
Write-Host “Loading”
asnp vmware.*
}
Else
{
Write-host “VMware module already present”
}
$vcenter = Read-Host “Nom du vCenter (FQDN)”
$login = $host.ui.PromptForCredential(“Identifiant de connexion au vCenter”, “Please enter your user name and password.”, “”, “NetBiosUserName”)
$output = Read-host “chemin complet de l’export csv  ex : f:tempoutput.csv”

#vCenter Connect
connect-viserver -server $vcenter -credential $login

#Get VM with Network Adapter with a connection state “Disconnected”
$VMs=Get-NetworkAdapter (Get-vm | Where {$_.Powerstate -eq “PoweredOn”} ) | where {$_.Connectionstate.connected -eq $false -and $_.Connectionstate.StartConnected -eq $true}
$list= @()
Foreach ($vm in $VMs) {
foreach ($cluster in @($vm.parent | get-cluster)) {
foreach ($esx in @(get-vm $vm.parent)){
$obj= “” | Select Name, Cluster, ESX, NetworkAdapter
$obj.Name = $vm.Parent
$obj.Cluster = $cluster.Name
$obj.ESX = $esx.VMhost.name
$obj.NetworkAdapter = $VMs.name
$list +=$obj
}
}
}
#Export list
$list | Export-Csv $output -noTypeInformation | Format-Table -AutoSize

#Display list
$list

#Choice if you want to reconnect the network cards
$choice= [System.Windows.Forms.MessageBox]::Show(“Voulez vous connecter les adaptateurs?” , “Continuer ?” , 4)

if ($choice -eq “YES”)
{
Foreach ($vm in $list)
{
$NetworkAdapter = Get-NetworkAdapter -VM $vm.Name
Set-NetworkAdapter -NetworkAdapter $NetworkAdapter -connected:$true -confirm:$false
}
else
{
Write-Host “No change”
}
}

 

You can download the script here  : check_connectednetwork

Julien Varela
Follow me

Julien Varela

Virtualisation & Cloud manager at Excellium Services
Consultant Virtualisation/Cloud au sein de Excellium Services et blogger @vThink. Je suis passionné par les nouvelles technologies. Et tout particulièrement, celles concernant la virtualisation. j'ai su apporter par mon autonomie, ma curiosité, ma pugnacité et ma technicité mon expertise dans ce domaine.
Julien Varela
Follow me

Leave a Reply