Public/Utilities/Publish-JyskITAutomation.ps1

#function Publish-JyskITAutomation() {
# try {
# $PSGalleryKey = Get-KeyvaultSecret -SecretName "psgallerykey"
# } catch {
# Write-Error "Failed to retreive PSGallery key from Azure Key Vault: $_"
# }
#
#
# # Remove logs and cache folder
# Write-Host "Removing logs and cache folder" -ForegroundColor Yellow
# if(Test-Path (Join-Path $script:ModuleRoot "cache")) {
# Join-Path $script:ModuleRoot "cache" | Remove-Item -Recurse -Force
# }
# if(Test-Path (Join-Path $script:ModuleRoot "logs")) {
# Join-Path $script:ModuleRoot "logs" | Remove-Item -Recurse -Force
# }
#
# Write-Host "Please make sure you have updated the version number in the module manifest. (JyskIT.Automation.psd1)" -ForegroundColor Yellow
# $Response = Read-Host "Are you sure you want to publish the module to PSGallery? [Y/N]"
# if ($Response.ToLower() -ne 'y') { return }
# Publish-Module -Path ".\" -NuGetApiKey $PSGalleryKey -
#}

function Publish-JyskITAutomation() {
    try {
        $PSGalleryKey = Get-KeyvaultSecret -SecretName "psgallerykey"
    } catch {
        Write-Error "Failed to retreive PSGallery key from Azure Key Vault: $_"
    }

    # 1. Definer temp-stien, så mappenavnet matcher dit modulnavn præcist
    $StagingFolder = Join-Path $env:TEMP "JyskIT.Automation"
    
    # 2. Ryd op fra en eventuel tidligere kørsel og opret mappen på ny
    if (Test-Path $StagingFolder) { Remove-Item $StagingFolder -Recurse -Force }
    $null = New-Item -ItemType Directory -Path $StagingFolder -Force

    Write-Host "Please make sure you have updated the version number in the module manifest. (JyskIT.Automation.psd1)" -ForegroundColor Yellow
    $Response = Read-Host "Are you sure you want to publish the module to PSGallery? [Y/N]" 
    if ($Response.ToLower() -ne 'y') { 
        Remove-Item $StagingFolder -Recurse -Force
        return 
    } 

    try {
        # 3. Kopier kun selve modulet (Public, Private, psd1, psm1) til den
        # korrekt navngivne temp-mappe - ikke test-scripts, .git osv.
        foreach ($item in @('Public', 'Private', 'JyskIT.Automation.psd1', 'JyskIT.Automation.psm1')) {
            Copy-Item -Path ".\$item" -Destination $StagingFolder -Recurse -Force
        }

        # 4. Kør Publish-PSResource på den rene temp-mappe
        # (PowerShellGet's Publish-Module kræver .NET SDK til pakning; PSResourceGet pakker selv)
        Publish-PSResource -Path $StagingFolder -Repository PSGallery -ApiKey $PSGalleryKey
        Write-Host "Module published successfully!" -ForegroundColor Green
    }
    catch {
        Write-Error "Failed to publish module: $_"
    }
    finally {
        # 5. Slet den midlertidige mappe i temp bagefter
        if (Test-Path $StagingFolder) {
            Remove-Item $StagingFolder -Recurse -Force
        }
    }
}