none
Foreach query RRS feed

  • Question

  • Hi

    Further to a previous post regarding creating a VM from a template VHD. I am having problems now inserting a Foreach loop to create each of the 3 VM's in the $VMName variable as below. I can get this to work if each new VM creates a new VHD but just cant seem to get it to work with each one copying the template VHD. Hoping someone can advise.

    Thanks in advance

    Regards

    $VHDXPath = "C:\HV-Machines"
    $VMName = "VM1","VM2","VM3"
    $Switch = "switch1"
    $CPU = "2"
    
    
    
    Copy-Item "$VHDXPath\TEMPLATE.vhdx" "$VHDXPath\$VMNAME.vhdx"
    
    New-VM -Name $VMName -MemoryStartupBytes 1GB -VHDPath "$VHDXPath\$VMName.vhdx" -Generation 1 -SwitchName $Switch
    
    Set-VM -ProcessorCount $CPU
    
    Start-VM -Name $VMName

    • Moved by Bill_Stewart Monday, March 12, 2018 7:20 PM This is not "fix/debug/rewrite my script for me" forum
    Friday, January 12, 2018 8:12 AM

All replies

  •  I am having problems now inserting a Foreach loop to create each of the 3 VM's in the $VMName variable as below. I

    What kind of problems do you have?

    Foreach-Object

    about_foreach


    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Friday, January 12, 2018 8:40 AM