9 Replies Latest reply on Aug 26, 2019 8:53 AM by LucD

    Create ova using details as on csv file

    bansne Enthusiast

      Hi,

       

      I am stuck on script to pull out information from csv and deploy ova using script to vc. All I am trying to do is pull details of ova in csv , delete the ova's and create new using new ova file and deploy in VC.

       

      Following is the code I am using to deploy ova.

       

      #vSphere login
      $Username = 'xxxxxxxxx'
      $Password = Read-Host -assecurestring "Please enter your password"
      $Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
      $vcenter = 'vctest01'
      $srv = Connect-VIServer $vcenter -User $Username -Password $Password

      Connect-VIServer $srv -Session $srv.SessionId

      $vmlist = Import-CSV "c:\script\Proxy.csv"
      $OVAPath = "c:\script\avamarproxy.ova"

      foreach ($item in $vmlist) {

          $DNS = $item.DNS
          $Gateway = $item.gateway_id
          $NTP = $item.NTP
          $IP0 = $item.ip_address
          $Netmask = $item.subnet_mask
          $VMname = $item.dns_name
          $Cluster = $item.Cluster
          $VMHost = Get-Cluster $Cluster | Get-VMHost | Get-Random
          $Datastore = $item.datastore
          $Portgroup = $item.vlan

      $OVAConfig = Get-OvfConfiguration -Ovf $OVAPath
      $ovfconfig.ToHashTable() | FT -AutoSize

      $OVAConfig.vami.Avamar_Virtual_Machine_Combined_Proxy.DNS.Value = $DNS
      $OVAConfig.vami.Avamar_Virtual_Machine_Combined_Proxy.gateway.Value = $Gateway

      $OVAConfig.vami.Avamar_Virtual_Machine_Combined_Proxy.ntp.Value = $NTP

      $OVAConfig.vami.Avamar_Virtual_Machine_Combined_Proxy.ip0.Value = $IP0
      $OVAConfig.vami.Avamar_Virtual_Machine_Combined_Proxy.netmask0.Value = $Netmask
      $OVAConfig.NetworkMapping.Isolated_Network.Value = $Portgroup

      Import-VApp -Source $OVAPath -OvfConfiguration $OVAConfig -Name $VMname -VMHost $VMHost -Datastore $Datastore -DiskStorageFormat "Thin" ---> also I think if it is thick should not add this entry for thin at all because I do not want changes in ova settings.

       

      #Move-VM -VM $VMname -Destination "Storage & Backup"\"Avamar"

      }

       

      Following is the code for csv > Attachment vminventory1,2.

       

      Currently using info from these two scripts for pulling out vm inventory. All I need VM name, dns details , cluster , datastore name , ntp server details , ip, network mask, subnet , gateway Ip and vlan details.

       

      However , I am unable to have ova been deployed to same folder as older one and with details as of old ova in vcenter server.

       

      Quick help on same would be much appreciated.

       

      Regards

      Neha Bansal

        • 1. Re: Create ova using details as on csv file
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          Are you getting any error messages?

          • 2. Re: Create ova using details as on csv file
            bansne Enthusiast

            no , but it is not working properly.

            I am little lost how can I get all excel data /script to export data as needed for ova deployment.

            • 3. Re: Create ova using details as on csv file
              bansne Enthusiast

              Hi LucD, LucD

               

              Apologies for directly looping you in. I still need small help.

              I Managed to get script to work. It is pulling up details from csv and deploying ova to vc.

               

              However can you help me how to have deployed vm moved to folder with spaces. Its working if I remove spaces.

               

              #Part 1: Gather VM info for existing Avamar proxies
              #Export folder details
              filter Get-FolderPath {
                  $_ | Get-View | % {
                      $row = "" | select Name, Path
                      $row.Name = $_.Name

                      $current = Get-View $_.Parent
              #        $path = $_.Name # Uncomment out this line if you do want the VM Name to appear at the end of the path
                      $path = ""
                      do {
                          $parent = $current
                          if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
                          $current = Get-View $current.Parent
                      } while ($current.Parent -ne $null)
                      $row.Path = $path
                      $row
                  }
              }

              #vSphere login
              $Username = 'admin'
              $Password = Read-Host -assecurestring "Please enter your password"
              $Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
              $vcenter = 'vc0001'
              $srv = Connect-VIServer $vcenter -User $Username -Password $Password
              $VMFolder = "HISAvamarProxyAppliances"

              $Report = @()
              $VMs = Get-Folder $VMFolder | Get-VM


              Connect-VIServer $srv -Session $srv.SessionId

              #Gather info for Proxies
              $result=&{foreach($vm in (Get-VM virtebs-abp*)) {

                  $vm.ExtensionData.Guest.Net | select -Property @{N='dns_name';E={$vm.Name}},

                  @{N='Host';E={$vm.VMHost.Name}},

                  @{N='OS';E={$vm.Guest.OSFullName}},

                  @{N='Tools';E={$vm.ExtensionData.Guest.ToolsRunningStatus}},

                  @{N='NicType';E={[string]::Join(',',(Get-NetworkAdapter -Vm $vm | Select-Object -ExpandProperty Type))}},

                  @{N='vlan';E={[string]::Join(',',(Get-NetworkAdapter -Vm $vm | Select-Object -ExpandProperty NetworkName))}},

                  @{N='ip_address';E={[string]::Join(',',($vm.Guest.IPAddress | Where {($_.Split(".")).length -eq 4}))}},
                 
                  @{N='Folder';E={[string]::Join(',',($vm | Get-Folderpath).Path )}},
                 

                  @{N='gateway_id';E={[string]::Join(',',($vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute | %{if($_.Gateway.IpAddress){$_.Gateway.IpAddress}}))}},

                  @{N='subnet_mask';E={

                              $dec = [Convert]::ToUInt32($(("1" * $_.IpConfig.IpAddress[0].PrefixLength).PadRight(32, "0")), 2)
                      $DottedIP = $( For ($i = 3; $i -gt -1; $i--) {
                        $Remainder = $dec % [Math]::Pow(256, $i)
                        ($dec - $Remainder) / [Math]::Pow(256, $i)
                        $dec = $Remainder

                                  } )

                              [String]::Join('.', $DottedIP)

                          }},

                       
                  @{N="DNS";E={[string]::Join(',',($vm.ExtensionData.Guest.IpStack.DnsConfig.IpAddress))}},

                  @{N='MAC';E={[string]::Join(',',$_.MacAddress)}},

                  @{N='Cluster';E={[string]::Join(',',(Get-Cluster -Vm $vm))}},

                  @{N='Datastore';E={[string]::Join(',',(Get-DatastoreCluster -Vm $vm))}}

                }
              }
              $result | Export-Csv C:\scripts\DeployAvamarProxy\getinfo2.csv -NoTypeInformation

              #Part 2: Deploy OVAs

              ##Deploy VMs
              $vmlist = Import-CSV "C:\scripts\DeployProxy\getinfo2.csv"
              $OVAPath = "C:\scripts\DeployProxy\AvamarCombinedProxy18.2.100-134.ova"

              foreach ($item in $vmlist) {

                  $DNS = $item.DNS
                  $Gateway = $item.gateway_id
                  $NTP = "10.0.0.x,11.x.x.x.x"
                  $IP0 = $item.ip_address
                  $Netmask = $item.subnet_mask
                  $VMname = $item.dns_name
                  $Cluster = $item.Cluster
                  $VMHost = Get-Cluster $Cluster | Get-VMHost | Get-Random
                  $Datastore = $item.datastore
                  $Portgroup = $item.vlan
                  $VMFolder = $item.Folder
                

              $OVAConfig = Get-OvfConfiguration -Ovf $OVAPath

              $OVAConfig.vami.Avamar_Virtual_Machine_Combined_Proxy.DNS.Value = $DNS
              $OVAConfig.vami.Avamar_Virtual_Machine_Combined_Proxy.gateway.Value = $Gateway
              $OVAConfig.vami.Avamar_Virtual_Machine_Combined_Proxy.ntp.Value = $NTP
              $OVAConfig.vami.Avamar_Virtual_Machine_Combined_Proxy.ip0.Value = $IP0
              $OVAConfig.vami.Avamar_Virtual_Machine_Combined_Proxy.netmask0.Value = $Netmask
              $OVAConfig.NetworkMapping.VM_Network.Value = $Portgroup


              Import-VApp -Source $OVAPath -OvfConfiguration $OVAConfig -Name $VMname -VMHost $VMHost -Datastore $Datastore -DiskStorageFormat "Thin"

              $VMFolder = 'ABCAppliances'  #this part I am confused how to have it move vm to vDC\Storage & Backup\Proxy Appliances , want it to pick the full path because there are two folders with same name in vc but dc name is different.
              Move-VM -VM $VMname -Destination $VMFolder -Confirm

              }

              • 4. Re: Create ova using details as on csv file
                LucD Guru
                User ModeratorsvExpertCommunity Warriors

                You can use my Get-FolderByPath function, see Folder By Path

                Note that you will need to give a Destination and InventoryLocation.

                Something like this

                 

                function Get-FolderByPath{

                <#

                .SYNOPSIS Retrieve folders by giving a path

                .DESCRIPTION The function will retrieve a folder by it's path.

                  The path can contain any type of leave (folder or datacenter).

                .NOTES

                  Author: Luc Dekens .PARAMETER Path The path to the folder. This is a required parameter.

                .PARAMETER

                  Path The path to the folder. This is a required parameter.

                .PARAMETER

                  Separator The character that is used to separate the leaves in the path. The default is '/'

                .EXAMPLE

                  PS> Get-FolderByPath -Path "Folder1/Datacenter/Folder2"

                .EXAMPLE

                  PS> Get-FolderByPath -Path "Folder1>Folder2" -Separator '>'

                #>

                  param(

                  [CmdletBinding()]

                  [parameter(Mandatory = $true)]

                  [System.String[]]${Path},

                  [char]${Separator} = '/'

                  )

                  process{

                   if((Get-PowerCLIConfiguration).DefaultVIServerMode -eq "Multiple"){

                   $vcs = $global:defaultVIServers

                   }

                   else{

                   $vcs = $global:defaultVIServers[0]

                   }

                   foreach($vc in $vcs){

                   $si = Get-View ServiceInstance -Server $vc

                   $rootName = (Get-View -Id $si.Content.RootFolder -Property Name).Name

                   foreach($strPath in $Path){

                   $root = Get-Folder -Name $rootName -Server $vc -ErrorAction SilentlyContinue

                   $strPath.Split($Separator) | %{

                   $root = Get-Inventory -Name $_ -Location $root -Server $vc -ErrorAction SilentlyContinue

                   if((Get-Inventory -Location $root -NoRecursion | Select -ExpandProperty Name) -contains "vm"){

                   $root = Get-Inventory -Name "vm" -Location $root -Server $vc -NoRecursion

                   }

                   }

                   $root | where {$_ -is [VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl]}|%{

                   Get-Folder -Name $_.Name -Location $root.Parent -NoRecursion -Server $vc

                   }

                   }

                   }

                  }

                }


                $folderPath = 'DC/Test Folder'

                $vmName = 'MyVM'


                $folder = Get-FolderByPath -Path $folderPath

                $cluster = Get-Cluster -VM $vmName

                Move-VM -VM $vmName -InventoryLocation $folder -Destination $cluster

                • 5. Re: Create ova using details as on csv file
                  bansne Enthusiast

                  Let me give it a try and get back to you.

                   

                  Cheers!!!

                  • 6. Re: Create ova using details as on csv file
                    bansne Enthusiast

                    Hi,

                     

                    No it does not work. It did say Move into resource pool(Target is name of my cluster) but not to folder in vc datacenter.

                     

                    I tried after renaming folder name and it works using old script.

                     

                     

                    Is it anyway possible if i can poweroff and then delete existing ova and create new on the above script too?

                     

                     

                    Regards

                    Neha Bansal

                    • 7. Re: Create ova using details as on csv file
                      LucD Guru
                      User ModeratorsvExpertCommunity Warriors

                      What kind of folder was that?
                      Was it a folder of type VM?

                       

                       

                      You should be able to stop and remove a VM with

                       

                      Get-VM -Name MyVM | Stop-VM -Kill -Confirm:$false |

                      Remove-VM -Confirm:$false

                      • 8. Re: Create ova using details as on csv file
                        bansne Enthusiast

                        Hi,

                         

                        Yes "VM and Template" type folder.

                        • 9. Re: Create ova using details as on csv file
                          LucD Guru
                          vExpertUser ModeratorsCommunity Warriors

                          Strange, can you attach a screenshot of the error(s) you get?