PublishLocal.ps1
# ============================ # Publish Az.NCache module to a Local PowerShell Repository # ============================ $ModuleName = "Az.NCache" $CurrentDir = Get-Location $LocalRepoPath = "C:\LocalPSRepo" $RepositoryName = "LocalRepo" # 1️⃣ Create temporary module folder to package before publishing $TempModulePath = Join-Path $env:TEMP $ModuleName if (Test-Path $TempModulePath) { Remove-Item $TempModulePath -Recurse -Force } New-Item -Path $TempModulePath -ItemType Directory | Out-Null # Copy psd1 & psm1 files Copy-Item -Path (Join-Path $CurrentDir "$ModuleName.psd1") -Destination $TempModulePath -Force Copy-Item -Path (Join-Path $CurrentDir "$ModuleName.psm1") -Destination $TempModulePath -Force Write-Host "Copied module files to temp path: $TempModulePath" # Create local repo folder if not exists if (-Not (Test-Path $LocalRepoPath)) { New-Item -Path $LocalRepoPath -ItemType Directory | Out-Null Write-Host "Created local repository folder at $LocalRepoPath" } else { Write-Host "Local repository folder already exists at $LocalRepoPath" } # Register local repository if not already registered $repo = Get-PSRepository -Name $RepositoryName -ErrorAction SilentlyContinue if (-not $repo) { Register-PSRepository -Name $RepositoryName ` -SourceLocation $LocalRepoPath ` -PublishLocation $LocalRepoPath ` -InstallationPolicy Trusted Write-Host "Registered local PowerShell repository '$RepositoryName'" } else { Write-Host "Repository '$RepositoryName' is already registered." } # Publish the module to the local repo Write-Host "Publishing module '$ModuleName' to $RepositoryName ..." Publish-Module -Path $TempModulePath -Repository $RepositoryName -Force Write-Host "Published '$ModuleName' successfully to local repository." # Test installation from local repo Write-Host "Installing module from local repository..." try { # Uninstall if already installed if (Get-Module -ListAvailable -Name $ModuleName) { Uninstall-Module -Name $ModuleName -AllVersions -Force -ErrorAction SilentlyContinue } Install-Module -Name $ModuleName -Repository $RepositoryName -Force Write-Host "Installed '$ModuleName' from '$RepositoryName'" -ForegroundColor Green Import-Module $ModuleName -Force Write-Host "Module '$ModuleName' imported successfully." -ForegroundColor Green Get-Module $ModuleName | Format-Table Name, Version, Path } catch { Write-Host "Error during installation or import: $($_.Exception.Message)" -ForegroundColor Red } |