【问题标题】:Jenkins with Powershell - Creating VMJenkins 与 Powershell - 创建虚拟机
【发布时间】:2020-12-23 14:56:20
【问题描述】:

今天开始学习Jenkins,想做一个小项目。

尝试创建将“询问”虚拟机名称和用户想要多少 RAM 的作业并部署虚拟机。

我正在使用数字 4、8 的“选择参数”

powershell中的脚本是

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

连接-VMNetworkAdapter -VMName $env:Vm_Name -SwitchName $env:NIC

我也尝试过这样做

$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

但还是报错

New-VM : Cannot bind parameter 'MemoryStartupBytes'. Cannot convert value "4gb" to type "System.Int64". Error: "Input 

或错误:

Cannot convert value "4096mb" to type "System.Int32". Error: "Input string was not in a correct format."

在 C:\Users\Administrator\AppData\Local\Temp\jenkins9080442734241450545.ps1:3 char:2

  • $integer = [int]$ramgb

或:

    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)

只想让用户选择分配多少内存给虚拟机

有人可以帮忙吗?

【问题讨论】:

    标签: powershell jenkins


    【解决方案1】:

    The reference 表示参数-MemoryStartupBytes

    指定分配给虚拟的内存量(以字节为单位) 机器。

    类型:Int64

    像“4gb”或“4096mb”这样的字符串不能转换为Int64。您必须自己完成,当我们假设用户只能选择 GB 值时,这很容易。

    1 GB = 1024 MB = 1024 * 1024 KB = 1024 * 1024 * 1024 字节

    $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
    

    我们将字符串变量$env:ram 转换为Int64,方法是将类型放在变量前面的方括号中,称为cast operator。 Cast 运算符的precedence 高于算术运算符,因此 PS 将首先转换字符串,然后再继续进行乘法运算。

    【讨论】:

      猜你喜欢
      • 2017-01-02
      • 2019-08-30
      • 2011-11-03
      • 1970-01-01
      • 2014-08-23
      • 2019-05-10
      • 2018-09-06
      • 2012-10-20
      • 2012-01-29
      相关资源
      最近更新 更多