private/Step-BuildBootSetContentStartnet.ps1

#Requires -PSEdition Core

function Step-BuildBootSetContentStartnet {
    <#
    .SYNOPSIS
        Configures startnet.cmd and WinpeStartup directories in the mounted image
 
    .DESCRIPTION
        Replaces Windows\System32\startnet.cmd in the mounted image with a command file
        that sets a dated OSDCloud WinpeStartup title and invokes Invoke-WinpeStartup in
        Windows PowerShell. It also creates WinpeStartup\profiles when that directory
        does not exist. Terminating errors are reported as nonterminating errors so
        they do not abort the calling build.
 
    .EXAMPLE
        PS> Step-BuildBootSetContentStartnet
 
        Writes the standard startup command and ensures its profile directory exists.
 
    .INPUTS
        None. This function does not accept pipeline input.
 
    .OUTPUTS
        None.
 
    .NOTES
        Author: David Segura
        Company: Recast Software
        Version: 0.1.0
        Date: 2026-08-28
 
        Requires global BuildMedia.MountPath. Destructively replaces startnet.cmd and
        may create directories in the mounted image. The generated command assumes
        Invoke-WinpeStartup will be available when WinPE starts.
    #>

    [CmdletBinding()]
    param ()

    try {
    $MountPath = $global:BuildMedia.MountPath
    $ScriptSource = $MyInvocation.MyCommand.Name

    $BuildDate = Get-Date -Format 'yy.M.d'
$startnetCmd = @"
@echo off
title OSDCloud WinpeStartup $BuildDate (close window to restart computer)
PowerShell -Nol -C Invoke-WinpeStartup
"@

    $startnetCmdPath = "$MountPath\Windows\System32\startnet.cmd"
    Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] [INFO] [$ScriptSource] Adding $startnetCmdPath"
    $startnetCmd | Out-File -FilePath $startnetCmdPath -Encoding ascii -Width 2000 -Force

    if (-not (Test-Path "$MountPath\WinpeStartup\profiles")) {
        Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] [INFO] [$ScriptSource] Creating directory $MountPath\WinpeStartup\profiles"
        New-Item -Path "$MountPath\WinpeStartup\profiles" -ItemType Directory -Force | Out-Null
    }
    }
    catch {
        Write-Error -ErrorRecord $_ -ErrorAction Continue
    }
}