我正在尝试创建一个带有参数的函数来创建VM,这就是我所处的位置。
我没有任何错误,但没有任何执行。
Function Create-VM {
param (
[string] $SRV1 = "Windows7",
[string] $SRAM = "512",
[string] $SRV1VHD = "41943040000",
[string] $VMLOC = "C:\users\Public\documents\hyper-v\virtual hard disks",
[string] $Network1 = "Local Area Connection – Virtual Switch")
# Create Virtual Machines
MD $VMLoc –erroraction silentlycontinue
new-vm $SRV1 -path $VMLoc
new-vhd -vhdpaths $VMLoc\$SRV1 -size $SRV1VHD
add-vmdisk -vm $SRV1 -controllerid 0 -lun 0 -path $VMLoc\$SRV1
get-vm $SRV1 | add-vmdrive -controllerid 1 -lun 0 -dvd
get-vm $SRV1 | set-vmmemory -memoryinbytes $SRAM
get-vm $SRV1 | add-vmnic -virtualswitch $Network1
}
答案 0 :(得分:0)
我好像您正在使用PowerShell 2.0。在2.0中,您需要导入模块才能使用这些命令。如果更新到3.0或4.0(推荐),只要安装了模块,就会自动执行此操作。这看起来像HyperV module from CodePlex,所以试试这个:
Function Create-VM {
param (
[string] $SRV1 = "Windows7",
[string] $SRAM = "512",
[string] $SRV1VHD = "41943040000",
[string] $VMLOC = "C:\users\Public\documents\hyper-v\virtual hard disks",
[string] $Network1 = "Local Area Connection – Virtual Switch")
#Import HyperV module
Import-Module HyperV
# Create Virtual Machines
MD $VMLoc –erroraction silentlycontinue
new-vm $SRV1 -path $VMLoc
new-vhd -vhdpaths $VMLoc\$SRV1 -size $SRV1VHD
add-vmdisk -vm $SRV1 -controllerid 0 -lun 0 -path $VMLoc\$SRV1
get-vm $SRV1 | add-vmdrive -controllerid 1 -lun 0 -dvd
get-vm $SRV1 | set-vmmemory -memoryinbytes $SRAM
get-vm $SRV1 | add-vmnic -virtualswitch $Network1
}
Create-VM