Skip to content

Commit 19f6c0c

Browse files
Add post-network script execution and optional module auto-update suppression
Enhanced Initialize-OSDCloudStartNet.ps1 to support execution custom PowerShell scripts after establishing a network connection. Added support for suppressing automatic updates of the lastest OSD module.
1 parent 3935f7b commit 19f6c0c

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

Public/OSDCloudSetup/OSDCloudTemplate.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,8 @@ Windows Registry Editor Version 5.00
10141014
'Config\Scripts\SetupComplete',
10151015
'Config\Scripts\Shutdown',
10161016
'Config\Scripts\Startup',
1017-
'Config\Scripts\StartNet'
1017+
'Config\Scripts\StartNet',
1018+
'Config\Scripts\StartNet2'
10181019
)
10191020

10201021
if ($Name -match 'public') {

Public/OSDCloudTS/Initialize-OSDCloudStartnet.ps1

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,53 @@ function Initialize-OSDCloudStartnet {
142142
}
143143
}
144144

145-
# Check if the OSD Module in the PowerShell Gallery is newer than the installed version
146-
$TimeSpan = New-TimeSpan -Start $Global:StartnetStart -End (Get-Date)
147-
Write-Host -ForegroundColor DarkGray "$($TimeSpan.ToString("mm':'ss")) Updating OSD PowerShell Module"
148-
$PSModuleName = 'OSD'
149-
$InstalledModule = Get-Module -Name $PSModuleName -ListAvailable -ErrorAction Ignore | Sort-Object Version -Descending | Select-Object -First 1
150-
$GalleryPSModule = Find-Module -Name $PSModuleName -ErrorAction Ignore -WarningAction Ignore
151-
152-
# Install the OSD module if it is not installed or if the version is older than the gallery version
153-
if ($GalleryPSModule) {
154-
if (($GalleryPSModule.Version -as [version]) -gt ($InstalledModule.Version -as [version])) {
155-
Write-Host -ForegroundColor DarkGray "$PSModuleName $($GalleryPSModule.Version) [AllUsers]"
156-
Install-Module $PSModuleName -Scope AllUsers -Force -SkipPublisherCheck
157-
Import-Module $PSModuleName -Force
145+
Write-Host -ForegroundColor Cyan '[i] Config Post StartNet Scripts'
146+
$Global:ScriptStartNet2 = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Name -ne 'C' } | ForEach-Object {
147+
Write-Host -ForegroundColor DarkGray "$($_.Root)OSDCloud\Config\Scripts\StartNet2\*.ps1"
148+
Get-ChildItem "$($_.Root)OSDCloud\Config\Scripts\StartNet2\" -Include "*.ps1" -File -Recurse -Force -ErrorAction Ignore
149+
}
150+
if ($Global:ScriptStartNet2) {
151+
$Global:ScriptStartNet2 = $Global:ScriptStartNet2 | Sort-Object -Property FullName
152+
foreach ($Item in $Global:ScriptStartNet2) {
153+
Write-Host -ForegroundColor Gray "Execute $($Item.FullName)"
154+
& "$($Item.FullName)"
155+
}
156+
$TimeSpan = New-TimeSpan -Start $Global:StartnetStart -End (Get-Date)
157+
Write-Host -ForegroundColor DarkGray "$($TimeSpan.ToString("mm':'ss")) Tried to execute Post StartNet Scripts"
158+
}
159+
160+
Write-Host -ForegroundColor Cyan '[i] OSD module update'
161+
$Global:OSDModuleUpdate = $true # Default is trying to newer OSD module
162+
$Global:OSDCloudStartnetJson = Get-PSDrive -PSProvider FileSystem | Where-Object {$_.Name -ne 'C'} | ForEach-Object {
163+
Get-ChildItem "$($_.Root)OSDCloud\Config" -Include "Initialize-OSDCloudStartnet.json" -File -Force -Recurse -ErrorAction Ignore
164+
}
165+
if ($Global:OSDCloudStartnetJson ) {
166+
foreach ($Item in $Global:OSDCloudStartnetJson) {
167+
Write-Host -ForegroundColor DarkGray "$($Item.FullName)"
168+
$Global:OSDModuleUpdate = (Get-Content -Path "$($Item.FullName)" | ConvertFrom-Json -ErrorAction "Stop").OSDAutoUpdate
169+
Write-Host -ForegroundColor DarkGray "- OSDAutoUpdate: $($Global:OSDModuleUpdate)"
158170
}
159171
}
172+
if ($Global:OSDModuleUpdate) {
173+
# Check if the OSD Module in the PowerShell Gallery is newer than the installed version
174+
$TimeSpan = New-TimeSpan -Start $Global:StartnetStart -End (Get-Date)
175+
Write-Host -ForegroundColor DarkGray "$($TimeSpan.ToString("mm':'ss")) Updating OSD PowerShell Module"
176+
$PSModuleName = 'OSD'
177+
$InstalledModule = Get-Module -Name $PSModuleName -ListAvailable -ErrorAction Ignore | Sort-Object Version -Descending | Select-Object -First 1
178+
$GalleryPSModule = Find-Module -Name $PSModuleName -ErrorAction Ignore -WarningAction Ignore
179+
180+
# Install the OSD module if it is not installed or if the version is older than the gallery version
181+
if ($GalleryPSModule) {
182+
if (($GalleryPSModule.Version -as [version]) -gt ($InstalledModule.Version -as [version])) {
183+
Write-Host -ForegroundColor DarkGray "$PSModuleName $($GalleryPSModule.Version) [AllUsers]"
184+
Install-Module $PSModuleName -Scope AllUsers -Force -SkipPublisherCheck
185+
Import-Module $PSModuleName -Force
186+
}
187+
}
188+
} else {
189+
# if json contains {"OSDAutoUpdate": false} then not trying to import newer OSD module
190+
$TimeSpan = New-TimeSpan -Start $Global:StartnetStart -End (Get-Date)
191+
Write-Host -ForegroundColor DarkGray "$($TimeSpan.ToString("mm':'ss")) Skip Updating OSD PowerShell Module"
192+
}
160193
}
161194
}

0 commit comments

Comments
 (0)