@@ -1133,6 +1133,7 @@ $CommonFunc = {
11331133 #
11341134 function GetMappedCSV
11351135 {
1136+ [CmdletBinding ()]
11361137 param (
11371138 [Parameter ()]
11381139 [string ]
@@ -2237,12 +2238,13 @@ function New-Fleet
22372238 }
22382239
22392240 # Create the fleet vmswitches with a fixed IP at the base of the APIPA range
2240- Invoke-Command $nodes {
2241+ Invoke-CommonCommand $nodes - InitBlock $CommonFunc - ScriptBlock {
22412242
22422243 if (-not (Get-VMSwitch - Name $using :vmSwitchName - ErrorAction SilentlyContinue)) {
22432244
22442245 $null = New-VMSwitch - name $using :vmSwitchName - SwitchType Internal
22452246 $null = Get-NetAdapter | ? DriverDescription -eq ' Hyper-V Virtual Ethernet Adapter' | ? Name -eq " vEthernet ($ ( $using :vmSwitchName ) )" | New-NetIPAddress - PrefixLength 16 - IPAddress $using :vmSwitchIP
2247+ LogOutput " CREATE internal vmswitch $ ( $using :vmSwitchName ) @ $ ( $env: COMPUTERNAME ) "
22462248 }
22472249 }
22482250
@@ -2254,7 +2256,16 @@ function New-Fleet
22542256 # create $vms vms per each csv named as <nodename><group prefix>
22552257 # vm name is vm-<group prefix><$group>-<hostname>-<number>
22562258
2257- Invoke-CommonCommand $nodes - InitBlock $CommonFunc - ScriptBlock {
2259+ # note that this would pass as a shallow copy of the object; this is why they are unpacked
2260+ # into a (flat) custom object
2261+ $csvs = GetMappedCSV @clusterParam | % { [PSCustomObject ]@ {
2262+ VDName = $_.VDName
2263+ FriendlyVolumeName = $_.SharedVolumeInfo.FriendlyVolumeName
2264+ }}
2265+
2266+ Invoke-CommonCommand $nodes - InitBlock $CommonFunc - ArgumentList @ (, $csvs ) - ScriptBlock {
2267+
2268+ param ($csvs )
22582269
22592270 function ApplySpecialization ( $path , $vmspec )
22602271 {
@@ -2371,7 +2382,7 @@ function New-Fleet
23712382 return $ok
23722383 }
23732384
2374- foreach ($csv in GetMappedCSV ) {
2385+ foreach ($csv in $csvs ) {
23752386
23762387 if ($ ($using :groups ).Length -eq 0 ) {
23772388 $groups = @ ( ' base' )
@@ -2381,124 +2392,125 @@ function New-Fleet
23812392
23822393 # identify the CSvs for which this node should create its VMs
23832394 # the trailing characters (if any) are the group prefix
2384- if ($csv.VDName -match " ^$env: COMPUTERNAME (?:-.+){0,1}" ) {
2395+ if ($csv.VDName -notmatch " ^$env: COMPUTERNAME (?:-.+){0,1}" ) {
2396+ continue
2397+ }
23852398
2386- foreach ($group in $groups ) {
2399+ foreach ($group in $groups ) {
23872400
2388- if ($csv.VDName -match " ^$env: COMPUTERNAME -([^-]+)$" ) {
2389- $g = $group + $matches [1 ]
2390- } else {
2391- $g = $group
2392- }
2401+ if ($csv.VDName -match " ^$env: COMPUTERNAME -([^-]+)$" ) {
2402+ $g = $group + $matches [1 ]
2403+ } else {
2404+ $g = $group
2405+ }
23932406
2394- foreach ($vm in 1 .. $using :vms ) {
2407+ foreach ($vm in 1 .. $using :vms ) {
23952408
2396- $stop = $false
2409+ $stop = $false
23972410
2398- $newvm = $false
2399- $name = (" vm-$g -$env: COMPUTERNAME -{0:000}" -f $vm )
2411+ $newvm = $false
2412+ $name = (" vm-$g -$env: COMPUTERNAME -{0:000}" -f $vm )
24002413
2401- $placePath = $csv.SharedVolumeInfo .FriendlyVolumeName
2402- $vmPath = Join-Path $placePath $name
2403- $vhdPath = Join-Path $vmPath " $name .vhdx"
2414+ $placePath = $csv.FriendlyVolumeName
2415+ $vmPath = Join-Path $placePath $name
2416+ $vhdPath = Join-Path $vmPath " $name .vhdx"
24042417
2405- # if the vm cluster group exists, we are already deployed
2406- if (-not (Get-ClusterGroup - Name $name - ErrorAction SilentlyContinue)) {
2418+ # if the vm cluster group exists, we are already deployed
2419+ if (-not (Get-ClusterGroup - Name $name - ErrorAction SilentlyContinue)) {
24072420
2408- if (-not $stop ) {
2409- $stop = Stop-After " AssertComplete"
2410- }
2421+ if (-not $stop ) {
2422+ $stop = Stop-After " AssertComplete"
2423+ }
24112424
2412- if ($stop ) {
2425+ if ($stop ) {
24132426
2414- Write-Host - ForegroundColor Red " vm $name not deployed"
2427+ LogOutput - ForegroundColor Red " vm $name not deployed"
24152428
2416- } else {
2429+ } else {
24172430
2418- Write-Host - ForegroundColor Yellow " create vm $name @ path $vmPath with vhd $vhdPath "
2431+ LogOutput " CREATE vm $name @ path $vmPath "
24192432
2420- # always specialize new vms
2421- $newvm = $true
2433+ # always specialize new vms
2434+ $newvm = $true
24222435
2423- # We're on the canonical node to create the vm; if the vm exists, tear it down and refresh
2424- $o = Get-VM - Name $name - ErrorAction SilentlyContinue
2436+ # We're on the canonical node to create the vm; if the vm exists, tear it down and refresh
2437+ $o = Get-VM - Name $name - ErrorAction SilentlyContinue
24252438
2426- if ($null -ne $o ) {
2439+ if ($null -ne $o ) {
24272440
2428- # interrupted between vm creation and role creation; redo it
2429- Write-Host " REMOVING vm $name for re-creation"
2441+ # interrupted between vm creation and role creation; redo it
2442+ LogOutput " REMOVE vm $name for re-creation"
24302443
2431- if ($o.State -ne ' Off' ) {
2432- Stop-VM - Name $name - TurnOff - Force - Confirm:$false
2433- }
2434- Remove-VM - Name $name - Force - Confirm:$false
2444+ if ($o.State -ne ' Off' ) {
2445+ Stop-VM - Name $name - TurnOff - Force - Confirm:$false
24352446 }
2447+ Remove-VM - Name $name - Force - Confirm:$false
2448+ }
24362449
2437- # scrub and re-create the vm metadata path and vhd
2438- if (Test-Path $vmPath )
2439- {
2440- Remove-Item - Recurse $vmPath
2441- }
2450+ # scrub and re-create the vm metadata path and vhd
2451+ if (Test-Path $vmPath )
2452+ {
2453+ Remove-Item - Recurse $vmPath
2454+ }
24422455
2443- $null = New-Item - ItemType Directory $vmPath
2444- Copy-Item $using :BaseVHD $vhdPath
2456+ $null = New-Item - ItemType Directory $vmPath
2457+ Copy-Item $using :BaseVHD $vhdPath
24452458
2446- # ### STOPAFTER
2447- if (-not $stop ) {
2448- $stop = Stop-After " CopyVHD" $using :stopafter
2449- }
2459+ # ### STOPAFTER
2460+ if (-not $stop ) {
2461+ $stop = Stop-After " CopyVHD" $using :stopafter
2462+ }
24502463
2451- if (-not $stop ) {
2464+ if (-not $stop ) {
24522465
2453- # Create A1 VM. use set-vmfleet to alter fleet sizing post-creation.
2454- # Do not monitor the internal switch connection; this allows live migration
2455- # despite the internal switch.
2456- $o = New-VM - VHDPath $vhdPath - Generation 2 - SwitchName $using :vmSwitchName - Path $placePath - Name $name
2457- if ($null -ne $o )
2458- {
2459- $o | Set-VM - ProcessorCount 1 - MemoryStartupBytes 1.75 GB - StaticMemory
2460- $o | Get-VMNetworkAdapter | Set-VMNetworkAdapter - NotMonitoredInCluster $true
2461- }
2466+ # Create A1 VM. use set-vmfleet to alter fleet sizing post-creation.
2467+ # Do not monitor the internal switch connection; this allows live migration
2468+ # despite the internal switch.
2469+ $o = New-VM - VHDPath $vhdPath - Generation 2 - SwitchName $using :vmSwitchName - Path $placePath - Name $name
2470+ if ($null -ne $o )
2471+ {
2472+ $o | Set-VM - ProcessorCount 1 - MemoryStartupBytes 1.75 GB - StaticMemory
2473+ $o | Get-VMNetworkAdapter | Set-VMNetworkAdapter - NotMonitoredInCluster $true
24622474 }
2475+ }
24632476
2464- # ### STOPAFTER
2465- if (-not $stop ) {
2466- $stop = Stop-After " CreateVM" $using :stopafter
2467- }
2477+ # ### STOPAFTER
2478+ if (-not $stop ) {
2479+ $stop = Stop-After " CreateVM" $using :stopafter
2480+ }
24682481
2469- if (-not $stop ) {
2482+ if (-not $stop ) {
24702483
2471- # Create clustered vm role (don't emit) and assign default owner node.
2472- # Swallow the (expected) warning that will be referring to the internal
2473- # vmswitch as being a local-only resource. Since we replicate the vswitch
2474- # with the same IP on each cluster node, it does work.
2475- #
2476- # If an error occurs, it will emit per normal.
2477- $null = $o | Add-ClusterVirtualMachineRole - WarningAction SilentlyContinue
2478- Set-ClusterOwnerNode - Group $o.VMName - Owners $env: COMPUTERNAME
2479- }
2484+ # Create clustered vm role (don't emit) and assign default owner node.
2485+ # Swallow the (expected) warning that will be referring to the internal
2486+ # vmswitch as being a local-only resource. Since we replicate the vswitch
2487+ # with the same IP on each cluster node, it does work.
2488+ #
2489+ # If an error occurs, it will emit per normal.
2490+ $null = $o | Add-ClusterVirtualMachineRole - WarningAction SilentlyContinue
2491+ Set-ClusterOwnerNode - Group $o.VMName - Owners $env: COMPUTERNAME
24802492 }
2481-
2482- } else {
2483- Write-Host - ForegroundColor Green " vm $name already deployed"
24842493 }
24852494
2486- # ### STOPAFTER
2487- if (-not $stop ) {
2488- $stop = Stop-After " CreateVMGroup" $using :stopafter
2489- }
2495+ } else {
2496+ LogOutput - ForegroundColor Green " vm $name already deployed"
2497+ }
24902498
2491- if (-not $stop -or ($using :specialize -eq ' Force' )) {
2492- # specialize as needed
2493- # auto only specializes new vms; force always; none skips it
2494- if (($using :specialize -eq ' Auto' -and $newvm ) -or ($using :specialize -eq ' Force' )) {
2495- Write-Host - fore yellow specialize $vhdPath
2496- if (-not (SpecializeVhd $vhdPath )) {
2497- Write-Host - fore red " Failed specialize of $vhdPath , halting."
2498- }
2499- } else {
2500- Write-Host - fore green skip specialize $vhdPath
2499+ # ### STOPAFTER
2500+ if (-not $stop ) {
2501+ $stop = Stop-After " CreateVMGroup" $using :stopafter
2502+ }
2503+
2504+ if (-not $stop -or ($using :specialize -eq ' Force' )) {
2505+ # specialize as needed
2506+ # auto only specializes new vms; force always; none skips it
2507+ if (($using :specialize -eq ' Auto' -and $newvm ) -or ($using :specialize -eq ' Force' )) {
2508+ LogOutput " SPECIALIZE vm $name "
2509+ if (-not (SpecializeVhd $vhdPath )) {
2510+ LogOutput - ForegroundColor red " Failed specialize of vm $name @ $vhdPath , halting."
25012511 }
2512+ } else {
2513+ LogOutput - ForegroundColor green skip specialize $vhdPath
25022514 }
25032515 }
25042516 }
0 commit comments