VMware Cloud Community
bernworx
Enthusiast
Enthusiast
Jump to solution

Connect VM network adapters

Hi

We had a power outaged yesterday on one of our DC. Unfortunately, all VMs network adapters appeared to be off/unchecked its network device status, and we done checking it one by one just to be sure.

Am asking if there will be one powercli script that will do the things:

1. Get the cluster name

2. Get VMs with unchecked network adapter {Set-NetworkAdapter -Connected:$false}

3. Set the VMs network adapter connected

4. Extract a report to csv.

this will be a big help.

here are some one liner cli.

-------------------------------------------------------------------------------

Disconnect Network Adapter on Virtual Machine

Get-VM -Name $VM_Name | Get-NetworkAdapter | Set-NetworkAdapter -Connected:$false

Connect Network Adapter on Virtual Machine

Get-VM -Name $VM_Name | Get-NetworkAdapter | Set-NetworkAdapter -Connected:$true

Check Network Adapters Connection State on VMs

Get-VM | Get-NetworkAdapter | Select-Object Parent,ConnectionState
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

To limit this to a specific cluster you could do

$clusterName = Read-Host -Prompt "Cluster name" 
$cluster = Get-Cluster -Name $clusterName
$nics
= Get-VM -Location $cluster | Get-NetworkAdapter | where {!$_.ConnectionState.Connected} $nics | Set-NetworkAdapter -Connected:$true -Confirm:$false
$nics | Select @{N="VM";E={$_.Parent.Name}},Name |
Export-Csv c:\report.csv -NoTypeInformation -UseCulture

And for a specific folder you could do

$folderName = Read-Host -Prompt "Folder name" 
$folder
= Get-Folder -Name $folderName $nics = Get-VM -Location $folder | Get-NetworkAdapter | where {!$_.ConnectionState.Connected} $nics | Set-NetworkAdapter -Connected:$true -Confirm:$false $nics | Select @{N="VM";E={$_.Parent.Name}},Name |
Export-Csv
c:\report.csv -NoTypeInformation -UseCulture

The concept si each time the same, you limit the Get-VM by using the Location parameter


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

I think you already have most of the code.

The following will connect the unconnected NICs and produce a report to a CSV file.

$nics = Get-VM | Get-NetworkAdapter | where {!$_.ConnectionState.Connected}

$nics | Set-NetworkAdapter -Connected:$false -Confirm:$false
$nics | Select @{N="VM";E={$_.Parent.Name}},Name | 
Export-Csv c:\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
bernworx
Enthusiast
Enthusiast
Jump to solution

Should that work in a specific cluster or folder?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

To limit this to a specific cluster you could do

$clusterName = Read-Host -Prompt "Cluster name" 
$cluster = Get-Cluster -Name $clusterName
$nics
= Get-VM -Location $cluster | Get-NetworkAdapter | where {!$_.ConnectionState.Connected} $nics | Set-NetworkAdapter -Connected:$true -Confirm:$false
$nics | Select @{N="VM";E={$_.Parent.Name}},Name |
Export-Csv c:\report.csv -NoTypeInformation -UseCulture

And for a specific folder you could do

$folderName = Read-Host -Prompt "Folder name" 
$folder
= Get-Folder -Name $folderName $nics = Get-VM -Location $folder | Get-NetworkAdapter | where {!$_.ConnectionState.Connected} $nics | Set-NetworkAdapter -Connected:$true -Confirm:$false $nics | Select @{N="VM";E={$_.Parent.Name}},Name |
Export-Csv
c:\report.csv -NoTypeInformation -UseCulture

The concept si each time the same, you limit the Get-VM by using the Location parameter


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
markdjones82
Expert
Expert
Jump to solution

Luc,

  On this command shouldn't it be set to $true?

$nics | Set-NetworkAdapter -Connected:$false -Confirm:$false

$nics | Set-NetworkAdapter -Connected:$true -Confirm:$false

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
LucD
Leadership
Leadership
Jump to solution

Indeed, it should. I corrected it


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
bernworx
Enthusiast
Enthusiast
Jump to solution

Yeah... great you two.

It's working..... thanks.

0 Kudos