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

Follow me

Julien Varela

Sr . Systems Engineer at Nutanix
and blogger @vThink.
Follow me

Leave a Reply