7 Replies Latest reply on Oct 27, 2016 10:00 PM by LucD

    Script for disconnecting the network adapter

    BowDaddy Lurker

      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

       

       

      }

        • 1. Re: Script for disconnecting the network adapter
          mattboren Master
          vExpert

          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?

          • 2. Re: Script for disconnecting the network adapter
            LucD Guru
            User ModeratorsvExpertCommunity Warriors

            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

            }

             

            • 3. Re: Script for disconnecting the network adapter
              BowDaddy Lurker

              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

              • 4. Re: Script for disconnecting the network adapter
                LucD Guru
                User ModeratorsvExpertCommunity Warriors

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

                • 5. Re: Script for disconnecting the network adapter
                  BowDaddy Lurker

                  Thank you LucD

                  • 6. Re: Script for disconnecting the network adapter
                    BowDaddy Lurker

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

                    • 7. Re: Script for disconnecting the network adapter
                      LucD Guru
                      User ModeratorsCommunity WarriorsvExpert

                      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"

                          }

                      }