VMware Cloud Community
BowDaddy
Contributor
Contributor
Jump to solution

Script for disconnecting the network adapter

I was able to figure out the command to disconnect the network adapter for "connected" and "connect at power on" for a single vm using :

get-vm xyz | get-networkadpater | set-networkadapter -connected:$false -startconnected:$false

But i need to run this in a script from a csv for about 200 VMs. I'm only concerned with disconnecting at this point and time.

I tried modifying a previous script that I had but I'm still new to building scripts. I have attached the script I'm trying to reuse. If anyone could help, I would greatly appreciate it. When running the script it throws an error saying Get-NetworkAdapter is not a proper cmdlet etc..

# Import VMs from csv

$VMS = Import-Csv $csvfile

Out-Log "VMs to be created: $totalVMs" "Yellow"

# Check to ensure csv is populated

If ($totalVMs -lt 1) {

    Out-Log "`nError: No entries found in VMs.csv" "Red"

    Out-Log "Exiting...`n" "Red"

    Exit

}

# Show input and ask for confirmation, unless -auto was used

If (!$auto) {

    $newVMs | Out-GridView -Title "VMs to be Updated"

    $continue = Read-Host "`nContinue (y/n)?"

    If ($continue -notmatch "y") {

        Out-Log "Exiting..." "Red"

        Exit

    }

}

# Connect to vCenter server

If ($vcenter -eq "") {$vcenter = Read-Host "`nEnter vCenter server FQDN or IP"}

Try {

    Out-Log "`nConnecting to vCenter - $vcenter`n`n" "Yellow"

    Connect-VIServer $vcenter -EA Stop | Out-Null

} Catch {

    Out-Log "`r`n`r`nUnable to connect to $vcenter" "Red"

    Out-Log "Exiting...`r`n`r`n" "Red"

    Exit

}

# VMs to be updated

foreach ($vm in $VMS){

      #$VM = $vm.vm

  Get-VM $vm.vm | Get-NetworkAdpater | Set-NetworkAdpater -connected:$false startconnected:$false

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There seems to a typo in the cmdlet, it should be Get-NetworkAdapter, not Get-NetworkAdpater

You can do something like this

Import-Csv vms.csv -UseCulture | %{

    Get-VM -Name $_.Name | Get-NetworkAdapter | where{$_.ConnectionState.Connected} | Set-NetworkAdapter -Connected:$false -Confirm:$false

}


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

View solution in original post

Reply
0 Kudos
9 Replies
mattboren
Expert
Expert
Jump to solution

Hello, BowDaddy-

I think that might just be a couple of typos in the following line:

Get-VM $vm.vm | Get-NetworkAdpater | Set-NetworkAdpater -connected:$false startconnected:$false

..the word "Adapter" is misspelled as "Adpater" a couple of times.  Also, the "StartConnected" parameter is missing the leading dash.  Try that line like:

Get-VM $vm.vm | Get-NetworkAdapter | Set-NetworkAdapter -connected:$false -startconnected:$false

That work better?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There seems to a typo in the cmdlet, it should be Get-NetworkAdapter, not Get-NetworkAdpater

You can do something like this

Import-Csv vms.csv -UseCulture | %{

    Get-VM -Name $_.Name | Get-NetworkAdapter | where{$_.ConnectionState.Connected} | Set-NetworkAdapter -Connected:$false -Confirm:$false

}


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

Reply
0 Kudos
BowDaddy
Contributor
Contributor
Jump to solution

Thank you. I'm still learning this stuff. If I'm pulling from a csv, is -Name in the script referring to the header title of the vm names in the csv? I'm confused on the -Name $_.Name part

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is correct, the CSV has one column with the header "Name".


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

Reply
0 Kudos
BowDaddy
Contributor
Contributor
Jump to solution

Thank you LucD

Reply
0 Kudos
BowDaddy
Contributor
Contributor
Jump to solution

Is this also possible by implementing this by an IP address instead of vm name?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

On the Get-VM you will need to give the Displayname of the VM.

If you can resolve the IP address into this Display name, that would be easiest.

If the DNS entry corresponds with the Display name you could do

Import-Csv vms.csv -UseCulture | %{

    Try{

        $name = [system.net.dns]::GetHostByAddress("$($_.ip)")

        Get-VM -Name $name.HostName.Split('.')[0] | Get-NetworkAdapter -VM $ipTab[$_.ip] | where{$_.ConnectionState.Connected} | Set-NetworkAdapter -Connected:$false -Confirm:$false

    }

    Catch{

        Write-Host "IP $($_.ip) does not resolve"

    }

}

Another option is to get all the VMs and then check which one has the IP address you are looking for.

This requires that the VMware Tools be installed on all the VMs, and that they are all powered on.

Something like this for example:

$ipTab = @{}

Get-VM | %{

    $ip = $_.ExtensionData.Guest.IPAddress

    if(!$ipTab.ContainsKey($ip)){

        $ipTab.Add($ip,$_)

    }

}

Import-Csv vms.csv -UseCulture | %{

    if($ipTab.ContainsKey($_.ip){

        Get-NetworkAdapter -VM $ipTab[$_.ip] | where{$_.ConnectionState.Connected} | Set-NetworkAdapter -Connected:$false -Confirm:$false

    }

    else{

        Write-Host "No VM with IP $($_.ip) found"

    }

}


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

Reply
0 Kudos
syedquadri719
Contributor
Contributor
Jump to solution

@LucD - Is there any way to disconnect network adapters of poweroff VMs. I tried the cmdlet but it fails stating that the VM powerstate must be poweredON. Any workaround for that? 
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

When the VM is powered off you should use the StartConnected switch.


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

Reply
0 Kudos