I recently started teaching myself about PowerShell scripting. I'm using Windows 10 Professional at home.
This is what I have so far for my script:
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"}
}
Is it wise to do the component under # Creates External Switch
, considering that I only need to do that once, or should I just run that separately since it'll be a one time run?
Also, every time I run the last line, under the # Adds External Realtek_NIC to all VMs
, I get the following error:
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
Why do I get that error, when the CSV file I created works just fine in all other places?
Contents of the CSV File:
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