9 Replies Latest reply on Sep 10, 2019 2:46 AM by LucD

    Modify VM Deploy Script - Specify CPU, RAM and additional disks and size

    sejohlun Novice

      Just starting to use a deploy script and I want to add some new features......

       

      After hostname and "IP" input I would like 3 input options

       

      1. Number of vCPUs

      2. Size of RAM

      3. Add additional disk yes/no

      -----if yes "size" (They should always be "eager" and on a Paravirtual.

      Then the same question again.....

      ....If no....
      The choose Template
      Then...customization spec

       

      As a side note the "Add IP" doesn't stick.....I get a Invoke-VMScript Failed to authenticate with the guest operating system using the supplied credential. m

       

      Is this a local user on the vm that needs to be set in the script?

        • 1. Re: Modify VM Deploy Script - Specify CPU, RAM and additional disks and size
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          I added options 1, 2 and 3 to the attached script.

           

          If the guest OS of the VM doesn't "know" the account under which you run the deployment script, you should use the GuestCRrdential, or GuestUser/GuestPassword, parameters to pass an account that is allowed to make those network changes inside the guest OS.
          This can, for example, be a local administrator.

          But watch out, if UAC is enabled in your template, this might still failt due to the UAC prompt.

          • 2. Re: Modify VM Deploy Script - Specify CPU, RAM and additional disks and size
            sejohlun Novice

            Just tried and I get:

             

             

            Get-NetworkAdapter : Cannot validate argument on parameter 'VM'. The argument is null. Provide a valid value for the argument, and then try running the command again.

            At C:\SCRIPTS\DEPLOY VM\Deploy-VM.ps1:149 char:24

            + Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $Network ...

            +                        ~~~

                + CategoryInfo          : InvalidData: (:) [Get-NetworkAdapter], ParameterBindingValidationException

                + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.GetNetworkAdapter

             

            I don´t really see why it fails because $vm is set to New-VM

             

             

            Write-Host "Deploying Virtual Machine with Name: [$Hostname] using Template: [$SourceVMTemplate] and Customization Specification: [$SourceCustomSpec] on cluster: [$cluster]"

            $vm = New-VM -Name $Hostname -Template $SourceVMTemplate -ResourcePool $cluster -OSCustomizationSpec $SourceCustomSpec -Location $Location -Datastore $Datastore -DiskStorageFormat $DiskStorageFormat | Out-Null

            Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $NetworkName -confirm:$false | Out-Null

            Set-VM -VM $vm -NumCpu $CPU -MemoryGB $Memory -Confirm:$false | Out-Null

            Get-VM $Hostname | Get-HardDisk | Where-Object {$_.Name -eq "Hard Disk 1"} | Set-HardDisk -CapacityGB $DiskCapacity -Confirm:$false | Out-Null

            if($diskSizes.Count -gt 0){

                New-HardDisk -VM $vm -CapacityGB $diskSizes -StorageFormat EagerZeroedThick | New-ScsiController -Type ParaVirtual -Confirm:$false | Out-Null

            }

            • 3. Re: Modify VM Deploy Script - Specify CPU, RAM and additional disks and size
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

              I hadn't noticed the Out-Null at the end of the previous line.

              Try changing that line to

               

              $vm = New-VM -Name $Hostname -Template $SourceVMTemplate -ResourcePool $cluster -OSCustomizationSpec $SourceCustomSpec -Location $Location -Datastore $Datastore -DiskStorageFormat $DiskStorageFormat

               

               

              • 4. Re: Modify VM Deploy Script - Specify CPU, RAM and additional disks and size
                sejohlun Novice

                Almost works now...

                 

                I creates cpu and ram correctly. But the additional disk fails

                 

                 

                New-HardDisk : Cannot convert 'System.Object[]' to the type 'System.Decimal' required by parameter 'CapacityGB'.

                • 5. Re: Modify VM Deploy Script - Specify CPU, RAM and additional disks and size
                  LucD Guru
                  User ModeratorsCommunity WarriorsvExpert

                  Ok, try replacing that part with

                   

                  if($diskSizes.Count -gt 0){

                    $diskSizes | %{

                      New-HardDisk -VM $vm -CapacityGB $_ -StorageFormat EagerZeroedThick | New-ScsiController -Type ParaVirtual -Confirm:$false | Out-Null

                    }

                  }

                  • 6. Re: Modify VM Deploy Script - Specify CPU, RAM and additional disks and size
                    sejohlun Novice

                    Perfect, you are a star!

                     

                    No error messages now when I run the script.....looks very good.

                     

                    The one thing I noticed on a new deployed VM is that the static IP that I´m setting will not "stick".....

                     

                    InterfaceIndex 2 is maybe wrong here.....Maybe I should use something like Get-NetAdapter

                     

                     

                    if ($IP) {

                    Add-Script "New-NetIPAddress -InterfaceIndex 2 -IPAddress %1 -PrefixLength %2 -DefaultGateway %3" @($IP, $SubnetLength, $GW)}

                    • 7. Re: Modify VM Deploy Script - Specify CPU, RAM and additional disks and size
                      LucD Guru
                      Community WarriorsUser ModeratorsvExpert

                      You are now ending the result of the scripts to Out-Null.
                      I would remove that, so you can see if there are any errors being returned from the script.

                       

                      Also, I'm not sure what is tested with $script[1].

                      I would try changing that last part to

                       

                      foreach ($script in $scripts)

                      {

                          Invoke-VMScript -ScriptText $script[0] -VM $Hostname -GuestCredential $VMLocalCredential

                      }

                      Restart-VM $Hostname

                      • 8. Re: Modify VM Deploy Script - Specify CPU, RAM and additional disks and size
                        sejohlun Novice

                        ExitCode     : 0

                        ScriptOutput : New-NetIPAddress : Cannot process argument transformation on parameter 'PrefixLength'. Cannot convert value "/16" to ty

                                       pe "System.Byte". Error: "Input string was not in a correct format."

                                       At line:1 char:79

                                       + ... nterfaceIndex 2 -IPAddress "172.16.49.29" -PrefixLength "/16" -Defaul ...

                                       +                                                             ~~~~~

                                           + CategoryInfo          : InvalidData: (:) [New-NetIPAddress], ParameterBindingArgumentTransformationException

                                           + FullyQualifiedErrorId : ParameterArgumentTransformationError,New-NetIPAddress

                        • 9. Re: Modify VM Deploy Script - Specify CPU, RAM and additional disks and size
                          LucD Guru
                          vExpertCommunity WarriorsUser Moderators

                          Try defining the prefix length as

                           

                          $SubnetLength = "16"                      #Subnetlength IP address to use for VM