Issue
Started to learn about Jenkins today and I want to do a little project.
Trying to create Job that will "Ask" for VM name and How much RAM user want and deploy the VM.
I'm Using "Choice Parameter" with number 4 , 8
href="https://i.stack.imgur.com/6vjif.png" rel="nofollow noreferrer">
and the script in the powershell is
New-VM -Name $env:Vm_Name -MemoryStartupBytes $env:ram"GB" -BootDevice VHD -NewVHDPath "C:\Users\Itay\Desktop\vm-test\$env:Vm_Name.vhdx" -Path C:\Users\Itay\Desktop\vm-test -NewVHDSizeBytes 30GB -Generation 2
Connect-VMNetworkAdapter -VMName $env:Vm_Name -SwitchName $env:NIC
I Also tried to do something like this
$number = $env:ram
$integer = [int]$number
New-VM -Name $Vm_Name -MemoryStartupBytes $integer"gb" -BootDevice VHD -NewVHDPath "C:\Users\Itay\Desktop\vm-test\$Vm_Name.vhdx" -Path C:\Users\Itay\Desktop\vm-test -NewVHDSizeBytes 30GB -Generation 2
but still getting error
New-VM : Cannot bind parameter 'MemoryStartupBytes'. Cannot convert value "4gb" to type "System.Int64". Error: "Input
or error :
Cannot convert value "4096mb" to type "System.Int32". Error: "Input string was not in a correct format."
At C:\Users\Administrator\AppData\Local\Temp\jenkins9080442734241450545.ps1:3 char:2
- $integer = [int]$ramgb
or :
New-VM : Failed to modify device 'Memory'.
Invalid startup memory amount assigned for '007'.
'007' failed to modify device 'Memory'. (Virtual machine ID 5EB708B9-49D9-4BD3-AC5A-4678B771AA35)
Invalid startup memory amount assigned for '007'. The minimum amount of memory you can assign to this virtual machine
is '32' MB. (Virtual machine ID 5EB708B9-49D9-4BD3-AC5A-4678B771AA35)
just want to do a choice for user to choose how much ram to allocate to the VM
Can someone please help ?
Solution
The reference says for parameter -MemoryStartupBytes
:
Specifies the amount of memory, in bytes, to assign to the virtual machine.
Type: Int64
Strings like "4gb" or "4096mb" are not convertible to Int64
. You have do it on your own, which is quite easy when we assume that user can choose only GB values.
1 GB = 1024 MB = 1024 * 1024 KB = 1024 * 1024 * 1024 Byte
$bytes = [Int64] $env:ram * 1024 * 1024 * 1024
New-VM -Name $Vm_Name -MemoryStartupBytes $bytes -BootDevice VHD -NewVHDPath "C:\Users\Itay\Desktop\vm-test\$Vm_Name.vhdx" -Path C:\Users\Itay\Desktop\vm-test -NewVHDSizeBytes 30GB -Generation 2
We convert the string variable $env:ram
to Int64
, by placing the type in square brackets in front of the variable, which is called the cast operator. Cast operator has higher precedence than arithmetic operators, so PS will first convert the string before it continues with the multiplications.
Answered By - zett42
Answer Checked By - Robin (JavaFixing Admin)