VMware Cloud Community
sejohlun
Contributor
Contributor
Jump to solution

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

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?

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try defining the prefix length as

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

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

View solution in original post

Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

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.


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

Reply
0 Kudos
sejohlun
Contributor
Contributor
Jump to solution

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

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
sejohlun
Contributor
Contributor
Jump to solution

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'.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

  }

}


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

Reply
0 Kudos
sejohlun
Contributor
Contributor
Jump to solution

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)}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
sejohlun
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try defining the prefix length as

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

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

Reply
0 Kudos