所以,我最近开始自学有关PowerShell脚本的问题,因为我所做的阅读以及我所看到的视频表明它几乎更容易做到,如果没有,PowerShell中的所有内容都与GUI相比。这适用于Windows 10客户端和服务器。
我在家里使用Windows 10 Professional,因为我真的很喜欢Hyper-V。
这是我目前为我的剧本所做的:
Start-Job
{
# Create new VMs based on the names given in the "VMName.csv" file.
$NewVM = Import-Csv "D:\Hyper-V\Hyper_V_Scripts\Test_Gen2_Virtual_Machine_Scratch.csv"
# Specifies VMName and Generation Number without attaching VHDX files.
$NewVM | ForEach-Object {New-VM -Name $_.VMName -Generation $_.Generation -NoVHD}
# Sets Startup, Mininum, and Maximum Memory fields.
$NewVM | ForEach-Object {Set-VMMemory -VMName $_.VMName -StartupBytes 1GB -MinimumBytes 512MB -MaximumBytes $_.MaxMemory}
# Creates VHDX file based on the VM name.
$NewVM | ForEach-Object {New-VHD -Path ("F:\Hyper-v_Storage\"+$_.VMName+".vhdx") -SizeBytes $_.VHDXSize}
# Adds VHDX to VMs.
$NewVM | ForEach-Object {Add-VMHardDiskDrive -VMName $_.VMName -Path ("F:\Hyper-V_Storage\"+$_.VMName+".vhdx") -ControllerNumber 0 -ControllerLocation 0}
# Add VMDVDDrive with ISO to VMs.
$NewVM | ForEach-Object {Add-VMDvdDrive -VMName $_.VMName -Path ("D:\iso\"+$_.ISO+".iso") -ControllerNumber 0 -ControllerLocation 1}
# Change firmware's boot order to CD/DVD, Hard Disk Drive, and Network Adapter
$NewVM | ForEach-Object {Set-VMFirmware -VMName $_.VMName -BootOrder (Get-VM -Name $_.VMName | Get-VMDvdDrive),(Get-VM -Name $_.VMName | Get-VMHardDiskDrive),(Get-VM -Name $_.VMName | Get-VMNetworkAdapter)}
# Add RemoteFX3DAdapter to certain VMs
$NewVM | Where-Object {$_.VMName -like "Chief_Architect - Windows 10"} | ForEach-Object {Add-VMRemoteFx3dVideoAdapter -VMName $_.VMName}
# Creates External Network Switch
# New-VMSwitch "External Realtek NIC" -NetAdapterName "Realtek_NIC" -AllowManagementOS $False
# Adds External Realtek_NIC to all VMs
$NewVM | ForEach-Object {Add-VMNetworkAdapter -VMName $_.VMName -SwitchName "External Realtek NIC" -Name "External Realtek"}
}
我想知道,在"#创建外部交换机"下进行组件是明智的,考虑到我只需要这样做一次,或者我应该单独运行它它会一次性运行吗?
此外,每次我运行最后一行时,在## ;;将外部Realtek_NIC添加到所有虚拟机"下,我都会收到以下错误:
Add-VMNetworkAdapter : Cannot validate argument on parameter 'VMName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:1 char:55
+ $NewVM | ForEach-Object {Add-VMNetworkAdapter -VMName $_.VMName}
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-VMNetworkAdapter], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.HyperV.PowerShell.Commands.AddVMNetworkAdapter
我不明白为什么当我创建的CSV文件在其他所有地方都运行正常时,我会收到错误。
CSV文件的内容:
VMName,MaxMemory,ISO,VHDXSize,Generation
ADDS-DHCP-DNS-WDS - Windows Server 2016 TP 5,2147483648,TP_5\Windows_Server_2016\14300.1000.160324-1723.RS1_RELEASE_SVC_SERVER_OEMRET_X64FRE_EN-US,107374182400,2
Chief_Architect - Windows 10,6442450944,Win10_1607_English_x64,37580963840,2
Development - Windows 10,4294967296,Win10_1607_English_x64,161061273600,2
PFSense,1073741824,pfSense-CE-2.3.1-RELEASE-amd64,2147483648,2
Steam - Windows 10,6442450944,Win10_1607_English_x64,322122547200,2
Storage Pool - Windows Server 2016 TP 5,2147483648,TP_5\Windows_Server_2016\14300.1000.160324-1723.RS1_RELEASE_SVC_SERVER_OEMRET_X64FRE_EN-US,42949672960,2
Winixhub - Windows 10,4294967296,Win10_1607_English_x64,53687091200,2
Winixhub Web Server - Windows Server 2016 TP 5,4294967296,TP_5\Windows_Server_2016\14300.1000.160324-1723.RS1_RELEASE_SVC_SERVER_OEMRET_X64FRE_EN-US,85899345920,2
先谢谢你提供的帮助。对此,我真的非常感激。 :)
答案 0 :(得分:1)
好的,我保留了你的评论,但是通过=TEXT(A1,"$#,##0.00")
将所有各种循环合并到一个循环中(并在该循环之前移动了开关创建,仍然被注释掉)。我还将GFX命令更改为$NewVM
语句,因此它只会将该命令应用于指定的VM。最后,我修复了你的If
命令。那个因为你的参数设置无效而无法正常工作。如果您运行Add-VMNetworkAdapter
,您将看到参数集是:
Get-Help Add-VMNetworkAdapter
您尝试使用第一组中的Add-VMNetworkAdapter [-VMName] <string[]> [-CimSession <CimSession[]>] [-ComputerName <string[]>] [-Credential <pscredential[]>] [-SwitchName <string>] [-IsLegacy <bool>]
[-Name <string>] [-DynamicMacAddress] [-StaticMacAddress <string>] [-Passthru] [-ResourcePoolName <string>] [-DeviceNaming {On | Off}] [-WhatIf] [-Confirm] [<CommonParameters>]
Add-VMNetworkAdapter [-CimSession <CimSession[]>] [-ComputerName <string[]>] [-Credential <pscredential[]>] [-ManagementOS] [-SwitchName <string>] [-Name <string>]
[-DynamicMacAddress] [-StaticMacAddress <string>] [-Passthru] [-DeviceNaming {On | Off}] [-WhatIf] [-Confirm] [<CommonParameters>]
Add-VMNetworkAdapter [-VM] <VirtualMachine[]> [-SwitchName <string>] [-IsLegacy <bool>] [-Name <string>] [-DynamicMacAddress] [-StaticMacAddress <string>] [-Passthru]
[-ResourcePoolName <string>] [-DeviceNaming {On | Off}] [-WhatIf] [-Confirm] [<CommonParameters>]
和最后一组中的-VMName
。我刚刚更改它以捕获在变量中创建的VM,然后将其用于-SwitchName
参数。我还将该命令移动到刚刚创建VM之后,以便变量中的数据是原始的。这就是我最终得到的结果:
-VM