Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewige committed Dec 17, 2024
1 parent 5456831 commit 93255f3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 43 deletions.
19 changes: 13 additions & 6 deletions 1es/Setup_orig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ param(

[Parameter(Mandatory=$False)][string]$BaseUnattendPath='.\unattend.xml',
[Parameter(Mandatory=$False)][string]$BaseVhdDirPath='.\',
[Parameter(Mandatory=$False)][string]$WorkingPath='.\working',
[Parameter(Mandatory=$False)][string]$OutVhdDirPath='.\exported_vhds',
# [Parameter(Mandatory=$False)][string]$WorkingPath='.\working',
[Parameter(Mandatory=$False)][string]$WorkingPath='C:\vms',
# [Parameter(Mandatory=$False)][string]$OutVhdDirPath='.\exported_vhds',

[Parameter(Mandatory=$False)][string]$VMCpuCount=4,
[Parameter(Mandatory=$False)][string]$VMMemory=4096MB
Expand All @@ -35,32 +36,38 @@ Create-VMSwitchIfNeeded -SwitchName 'VMExternalSwitch' -SwitchType 'External'
Create-DirectoryIfNotExists -Path $WorkingPath

# Unzip any VHDs
Log-Message "Processing VHDs in $BaseVhdDirPath"
$zipFiles = Get-ChildItem -Path $BaseVhdDirPath -Filter *.zip
foreach ($zipFile in $zipFiles) {
Log-Message "Extracting VHDs from $($zipFile.FullName)"
$outDir = Join-Path -Path $BaseVhdDirPath -ChildPath $zipFile.BaseName
if (-not (Test-Path -Path $outDir)) {
Expand-Archive -Path $zipFile.FullName -DestinationPath $outDir

# Move the VHDs to the base directory
$vhdFiles = Get-ChildItem -Path $outDir -Filter *.vhd -ErrorAction Ignore
$vhdFiles = @()
$vhdFiles += Get-ChildItem -Path $outDir -Filter *.vhd -ErrorAction Ignore
$vhdFiles += Get-ChildItem -Path $outDir -Filter *.vhdx -ErrorAction Ignore
foreach ($vhdFile in $vhdFiles) {
Move-Item -Path $vhdFile.FullName -Destination $BaseVhdDirPath
}
}
Log-Message "Successfully processed $($zipFile.FullName)"
}

# Read the input VHDs
$vhds = @((Get-ChildItem -Path $BaseVhdDirPath -Filter *.vhd))
$vhds += Get-ChildItem -Path $BaseVhdDirPath -Filter *.vhdx
$vhds = @()
$vhds += Get-ChildItem -Path $BaseVhdDirPath -Filter *.vhd -ErrorAction Ignore
$vhds += Get-ChildItem -Path $BaseVhdDirPath -Filter *.vhdx -ErrorAction Ignore
if ($vhds.Count -eq 0) {
throw "No VHDs found in $BaseVhdDirPath"
}
Log-Message "Successfully processed VHDs"

for ($i = 0; $i -lt $vhds.Count; $i++) {
try {
$vhd = $vhds[$i]
Log-Message -Message "Processing VHD: $($vhd.FullName)"
Log-Message -Message "Creating VM from VHD: $($vhd.FullName)"
$vmName = "runner_vm"
if ($i -gt 0) {
$vmName += "_$i"
Expand Down
76 changes: 39 additions & 37 deletions 1es/prepare_vm_helpers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -292,52 +292,54 @@ function Install-HyperVIfNeeded {

function Create-VMSwitchIfNeeded {
param (
[Parameter(Mandatory=$False)][string]$SwitchName='VMInternalSwitch',
[Parameter(Mandatory=$False)][string]$SwitchType='Internal'
[Parameter(Mandatory=$true)][string]$SwitchName,
[Parameter(Mandatory=$true)][string]$SwitchType
)
try {
if ($SwitchType -eq 'External') {
# Check to see if an external switch already exists
$ExternalSwitches = (Get-VMSwitch -SwitchType External -ErrorAction Ignore)
if ($ExternalSwitches -ne $null) {
Log-Message -Message "External switch already exists: $($ExternalSwitches[0].Name)"
return
}
if ($SwitchType -eq 'External') {
# Check to see if an external switch already exists
$ExternalSwitches = (Get-VMSwitch -SwitchType External -ErrorAction Ignore)
if ($ExternalSwitches -ne $null) {
Log-Message -Message "External switch already exists: $($ExternalSwitches[0].Name)"
return
}

# Try to create the external switch
$NetAdapterNames = (Get-NetAdapter -Name 'Ethernet*' | Where-Object { $_.Status -eq 'Up' }).Name
$index = 0
foreach ($NetAdapterName in $NetAdapterNames) {
try {
if ([string]::IsNullOrEmpty($NetAdapterName)) {
continue
}
$currSwitchName = $SwitchName + '-' + $index
Log-Message "Attempting to creating external switch: $currSwitchName with NetAdapter: $NetAdapterName"
New-VMSwitch -Name $currSwitchName -NetAdapterName $NetAdapterName -AllowManagementOS $true
$index += 1
# break
} catch {
Log-Message "Failed to create external switch for NetAdapter: $NetAdapterName with error: $_"
# Try to create the external switch
$NetAdapterNames = (Get-NetAdapter -Name 'Ethernet*' | Where-Object { $_.Status -eq 'Up' }).Name
$index = 0
foreach ($NetAdapterName in $NetAdapterNames) {
try {
if ([string]::IsNullOrEmpty($NetAdapterName)) {
continue
}
$currSwitchName = $SwitchName + '-' + $index
Log-Message "Attempting to creating external switch: $currSwitchName with NetAdapter: $NetAdapterName"
New-VMSwitch -Name $currSwitchName -NetAdapterName $NetAdapterName -AllowManagementOS $true
$index += 1
# break
} catch {
Log-Message "Failed to create external switch for NetAdapter: $NetAdapterName with error: $_"
}
} elseif ($SwitchType -eq 'Internal') {
# Check to see if an internal switch already exists
$InternalSwitches = (Get-VMSwitch -SwitchType Internal -ErrorAction Ignore)
if ($InternalSwitches -ne $null) {
Log-Message -Message "Internal switch already exists: $($InternalSwitches[0].Name)"
return
}
}
} elseif ($SwitchType -eq 'Internal') {
# Check to see if an internal switch already exists
$InternalSwitches = (Get-VMSwitch -SwitchType Internal -ErrorAction Ignore)
if ($InternalSwitches -ne $null) {
Log-Message -Message "Internal switch already exists: $($InternalSwitches[0].Name)"
return
}

# Try to create the internal switch
# Try to create the internal switch
try {
Log-Message "Creating internal switch"
New-VMSwitch -Name 'VMInternalSwitch' -SwitchType Internal
} else {
throw "Invalid switch type: $SwitchType"
} catch {
throw "Failed to create internal switch with error: $_"
}
} catch {
throw "Failed to create external switch with error: $_"
} else {
throw "Invalid switch type: $SwitchType"
}

Log-Message "Successfully created $SwitchType switch with name: $SwitchName" -ForegroundColor Green
}

function Create-VMStoredCredential {
Expand Down

0 comments on commit 93255f3

Please sign in to comment.