6 Replies Latest reply on Jun 5, 2019 10:35 PM by ganapa2000

    Unable to add SCSI Controllers

    ganapa2000 Enthusiast

      Hi,

       

      I am having trouble adding multiple SCSI controllers to VM, using the below script, it adds just one controller and exits. But in .csv file I have mentioned multiple SCSI controllers that needs to added.

       

      Please help

       

      CSV File Contents

      "LUN_ID","SCSI_Control","SCSI_ID","Capacity_GB"

      "2","SCSI controller 1","0","1"

      "0","SCSI controller 2","0","100"

      "152","SCSI controller 3","0","500"

       

      #Script

      $csvpath = '.\controllers.csv'

       

      foreach ($vmdk in (Import-CSV -Path $csvpath -UseCulture))

      {

      $vmName = Read-Host -Prompt "Name of the VM"

      $vm = Get-VM -Name $vmName

       

      # Does the SCSI Controller exist?

      $controller = Get-ScsiController -Name $vmdk.SCSI_Control -VM $vm -ErrorAction SilentlyContinue

         while (-not $controller)

         {

      $newBusNumber = ($vm.ExtensionData.Config.Hardware.Device |

          where{$_ -is [VMware.Vim.VirtualSCSIController]} |

          Select -ExpandProperty BusNumber |

          Measure-Object -Maximum |

          Select -ExpandProperty Maximum) + 1

      $spec = New-Object VMware.Vim.VirtualMachineConfigSpec

      $device = New-Object VMware.Vim.VirtualDeviceConfigSpec

      $device.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add

      $device.Device = New-Object VMware.Vim.VirtualLsiLogicSASController

      $device.Device.BusNumber = $newBusNumber

      $device.Device.SharedBus = [VMware.Vim.VirtualSCSISharing]::PhysicalSharing

      $spec.DeviceChange += $device

      $vm.ExtensionData.ReconfigVM($spec)

      $controller = Get-ScsiController -VM $vm -Name $vmdk.SCSI_Control

      exit

         }

      }

        • 1. Re: Unable to add SCSI Controllers
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          Leave out the exit statement.

          Also not sure why you are using a While construct, this should work with a simple If.

           

          #Script

          $csvpath = '.\controllers.csv'

          foreach ($vmdk in (Import-CSV -Path $csvpath -UseCulture))

          {

             $vmName = Read-Host -Prompt "Name of the VM"

             $vm = Get-VM -Name $vmName

             # Does the SCSI Controller exist?

             $controller = Get-ScsiController -Name $vmdk.SCSI_Control -VM $vm -ErrorAction SilentlyContinue

             if (-not $controller)

             {

             $newBusNumber = ($vm.ExtensionData.Config.Hardware.Device |

             where { $_ -is [VMware.Vim.VirtualSCSIController] } |

            Select -ExpandProperty BusNumber |

             Measure-Object -Maximum |

            Select -ExpandProperty Maximum) + 1

             $spec = New-Object VMware.Vim.VirtualMachineConfigSpec

             $device = New-Object VMware.Vim.VirtualDeviceConfigSpec

             $device.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add

             $device.Device = New-Object VMware.Vim.VirtualLsiLogicSASController

             $device.Device.BusNumber = $newBusNumber

             $device.Device.SharedBus = [VMware.Vim.VirtualSCSISharing]::PhysicalSharing

             $spec.DeviceChange += $device

             $vm.ExtensionData.ReconfigVM($spec)

             $controller = Get-ScsiController -VM $vm -Name $vmdk.SCSI_Control

             }

          }

          • 2. Re: Unable to add SCSI Controllers
            ganapa2000 Enthusiast

            LucD,

             

            This Script adds just one next available SCSI Controller and loops again asking for VM name, but it is not checking from the input.csv file which are SCSI controllers that needs to be added.

             

            For Example, from the below csv file, I have 3 different controllers needs to be validated and to be added to same VM, like, SCSI Controller 1, 3 and 5

             

            CSV File Contents

            "LUN_ID","SCSI_Controller","SCSI_ID","Capacity_GB"

            "2","SCSI controller 1","0","1"

            "0","SCSI controller 3","0","100"

            "152","SCSI controller 5","0","500"

            • 3. Re: Unable to add SCSI Controllers
              LucD Guru
              User ModeratorsCommunity WarriorsvExpert

              Try like this

               

              #Script

              $csvpath = '.\controllers.csv'


              $vmName = Read-Host -Prompt "Name of the VM"

              $vm = Get-VM -Name $vmName

              foreach ($vmdk in (Import-CSV -Path $csvpath -UseCulture))

              {

                 # Does the SCSI Controller exist?

                 $controller = Get-ScsiController -Name $vmdk.SCSI_Control -VM $vm -ErrorAction SilentlyContinue

                 if (-not $controller)

                 {

                 $newBusNumber = ($vm.ExtensionData.Config.Hardware.Device |

                 where { $_ -is [VMware.Vim.VirtualSCSIController] } |

                Select -ExpandProperty BusNumber |

                 Measure-Object -Maximum |

                Select -ExpandProperty Maximum) + 1

                 $spec = New-Object VMware.Vim.VirtualMachineConfigSpec

                 $device = New-Object VMware.Vim.VirtualDeviceConfigSpec

                 $device.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add

                 $device.Device = New-Object VMware.Vim.VirtualLsiLogicSASController

                 $device.Device.BusNumber = $newBusNumber

                 $device.Device.SharedBus = [VMware.Vim.VirtualSCSISharing]::PhysicalSharing

                 $spec.DeviceChange += $device

                 Write-Host "Adding SCSI $($vmdk.SCSI_Control) Bus $($newBusNumber)"

                 $vm.ExtensionData.ReconfigVM($spec)

                 $vm.ExtensionData.UpdateViewData()

                 }

              }

              • 4. Re: Unable to add SCSI Controllers
                ganapa2000 Enthusiast

                LucD,

                 

                From the csv file, I have provided SCSI Controller 1, 2 and 5

                 

                From CLI, I see as below

                 

                 

                From GUI, I see SCSI Controllers added as 1, 2 and 3, instead of 5

                 

                • 5. Re: Unable to add SCSI Controllers
                  LucD Guru
                  Community WarriorsUser ModeratorsvExpert

                  That is how vSphere works I'm afraid.
                  It assigns the numbers in the Label sequentially, you can't leave gaps.

                  • 6. Re: Unable to add SCSI Controllers
                    ganapa2000 Enthusiast

                    Thanks for the update and help LucD.

                     

                    I think, I am good now, let me go with sequential then...