Skip to content

Enable post-network script execution and optionally suppress module auto-updates (Initialize-OSDCloudStartnet.ps1) #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Public/OSDCloudSetup/OSDCloudTemplate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,8 @@ Windows Registry Editor Version 5.00
'Config\Scripts\SetupComplete',
'Config\Scripts\Shutdown',
'Config\Scripts\Startup',
'Config\Scripts\StartNet'
'Config\Scripts\StartNet',
'Config\Scripts\StartNet2'
)

if ($Name -match 'public') {
Expand Down
59 changes: 46 additions & 13 deletions Public/OSDCloudTS/Initialize-OSDCloudStartnet.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,53 @@ function Initialize-OSDCloudStartnet {
}
}

# Check if the OSD Module in the PowerShell Gallery is newer than the installed version
$TimeSpan = New-TimeSpan -Start $Global:StartnetStart -End (Get-Date)
Write-Host -ForegroundColor DarkGray "$($TimeSpan.ToString("mm':'ss")) Updating OSD PowerShell Module"
$PSModuleName = 'OSD'
$InstalledModule = Get-Module -Name $PSModuleName -ListAvailable -ErrorAction Ignore | Sort-Object Version -Descending | Select-Object -First 1
$GalleryPSModule = Find-Module -Name $PSModuleName -ErrorAction Ignore -WarningAction Ignore

# Install the OSD module if it is not installed or if the version is older than the gallery version
if ($GalleryPSModule) {
if (($GalleryPSModule.Version -as [version]) -gt ($InstalledModule.Version -as [version])) {
Write-Host -ForegroundColor DarkGray "$PSModuleName $($GalleryPSModule.Version) [AllUsers]"
Install-Module $PSModuleName -Scope AllUsers -Force -SkipPublisherCheck
Import-Module $PSModuleName -Force
Write-Host -ForegroundColor Cyan '[i] Config Post StartNet Scripts'
$Global:ScriptStartNet2 = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Name -ne 'C' } | ForEach-Object {
Write-Host -ForegroundColor DarkGray "$($_.Root)OSDCloud\Config\Scripts\StartNet2\*.ps1"
Get-ChildItem "$($_.Root)OSDCloud\Config\Scripts\StartNet2\" -Include "*.ps1" -File -Recurse -Force -ErrorAction Ignore
}
if ($Global:ScriptStartNet2) {
$Global:ScriptStartNet2 = $Global:ScriptStartNet2 | Sort-Object -Property FullName
foreach ($Item in $Global:ScriptStartNet2) {
Write-Host -ForegroundColor Gray "Execute $($Item.FullName)"
& "$($Item.FullName)"
}
$TimeSpan = New-TimeSpan -Start $Global:StartnetStart -End (Get-Date)
Write-Host -ForegroundColor DarkGray "$($TimeSpan.ToString("mm':'ss")) Tried to execute Post StartNet Scripts"
}

Write-Host -ForegroundColor Cyan '[i] OSD module update'
$Global:OSDModuleUpdate = $true # Default is trying to newer OSD module
$Global:OSDCloudStartnetJson = Get-PSDrive -PSProvider FileSystem | Where-Object {$_.Name -ne 'C'} | ForEach-Object {
Get-ChildItem "$($_.Root)OSDCloud\Config" -Include "Initialize-OSDCloudStartnet.json" -File -Force -Recurse -ErrorAction Ignore
}
if ($Global:OSDCloudStartnetJson ) {
foreach ($Item in $Global:OSDCloudStartnetJson) {
Write-Host -ForegroundColor DarkGray "$($Item.FullName)"
$Global:OSDModuleUpdate = (Get-Content -Path "$($Item.FullName)" | ConvertFrom-Json -ErrorAction "Stop").OSDAutoUpdate
Write-Host -ForegroundColor DarkGray "- OSDAutoUpdate: $($Global:OSDModuleUpdate)"
}
}
if ($Global:OSDModuleUpdate) {
# Check if the OSD Module in the PowerShell Gallery is newer than the installed version
$TimeSpan = New-TimeSpan -Start $Global:StartnetStart -End (Get-Date)
Write-Host -ForegroundColor DarkGray "$($TimeSpan.ToString("mm':'ss")) Updating OSD PowerShell Module"
$PSModuleName = 'OSD'
$InstalledModule = Get-Module -Name $PSModuleName -ListAvailable -ErrorAction Ignore | Sort-Object Version -Descending | Select-Object -First 1
$GalleryPSModule = Find-Module -Name $PSModuleName -ErrorAction Ignore -WarningAction Ignore

# Install the OSD module if it is not installed or if the version is older than the gallery version
if ($GalleryPSModule) {
if (($GalleryPSModule.Version -as [version]) -gt ($InstalledModule.Version -as [version])) {
Write-Host -ForegroundColor DarkGray "$PSModuleName $($GalleryPSModule.Version) [AllUsers]"
Install-Module $PSModuleName -Scope AllUsers -Force -SkipPublisherCheck
Import-Module $PSModuleName -Force
}
}
} else {
# if json contains {"OSDAutoUpdate": false} then not trying to import newer OSD module
$TimeSpan = New-TimeSpan -Start $Global:StartnetStart -End (Get-Date)
Write-Host -ForegroundColor DarkGray "$($TimeSpan.ToString("mm':'ss")) Skip Updating OSD PowerShell Module"
}
}
}