Jenkins DSC Get Dependancies

# Configure Environment
$ErrorActionPreference = 'Stop'
Set-Location -Path $env:WORKSPACE

# Initialize variables
[string[]]$detectedmodules = @()
[string[]]$stagedmodules = @()
[string[]]$ignoremodules = "PSDesiredStateConfiguration"
[string]$modulespath = 'C:\Program Files\WindowsPowerShell\DscService\Modules'
write-warning -Message "This script does not upgrade existing modules. Please test your scripts with modules already installed on the pull server."

Write-Output "Checking for Internal NuGet repository"
If (Get-PSRepository -Name mynuget -ErrorAction SilentlyContinue) {
    write-output "Internal NuGet respository already registered."
else {
    write-output "Registering Internal NuGet repository..."
    Register-PSRepository -Name MyNuGet -SourceLocation -PublishLocation -ScriptSourceLocation -ScriptPublishLocation -InstallationPolicy Trusted -PackageManagementProvider NuGet -ErrorAction Stop | Out-Null

write-output "Importing NuGet package provider..."
Import-PackageProvider -Name NuGet -Force -ForceBootstrap -ErrorAction Stop | Out-Null

write-output "Setting PSModulePath..."
$env:PSModulePath = 'C:\Program Files\WindowsPowerShell\Modules;C:\Program Files (x86)\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules'

Write-Output "Detecting required modules..."
Get-Content -Path ".\$($env:ConfigName).ps1" | Where-Object { $_ -match '(import-dscresource\s+)' } | ForEach-Object {
    $detectedmodules += $_.ToString().Trim().Split(" ") | Select-Object -Last 1
$detectedmodules = $detectedmodules | Where-Object { $_ -notcontains $ignoremodules }

Write-Output "Getting currently staged modules..."
Get-ChildItem -Path $modulespath -Filter *.zip | ForEach-Object {
    $stagedmodules += $_.Name.ToString().Trim().Split("_")[0]

Write-Output "Checking for modules in the Jenkins server's powershell library..."
foreach ($detectedmodule In $detectedmodules) {
    write-output "Checking for $detectedmodule..."
    $checkmod = Get-DscResource -Module $detectedmodule -ErrorAction Stop -WarningAction SilentlyContinue
    If ($checkmod) {
        write-output "$detectedmodule already installed."
    else {
        Write-Output "Installing $detectedmodule..."
        Install-Module -Name $detectedmodule -Repository mynuget -ErrorAction Stop

Write-Output "Identifying modules missing from the pullserver repository..."
$missingmodules = $detectedmodules | Where-Object { $stagedmodules -NotContains $_ }

foreach ($missingmodule In $missingmodules) {
    Write-Output "Downloading $missingmodule..."
    Save-Module -Name $missingmodule -Path . -ErrorAction Stop

    Write-Output "Unhiding files to enable compression..."
    Get-ChildItem -path ".\$missingmodule\$version\*" -force -Recurse | Where-Object { $_.Attributes -match "hidden" } | ForEach-Object { $_.Attributes = "" }

    Write-Output "Compressing module..."
    $version = (Get-ChildItem -Path ".\$missingmodule").Name
    $archive = '.\' + $missingmodule + '_' + $version + '.zip'
    Compress-Archive -Path ".\$missingmodule\$version\*" -DestinationPath $archive -ErrorAction Stop

    Write-Output "Creating checksum..."
    New-DscChecksum -Path $archive -OutPath . -ErrorAction Stop

    write-output "Copying modules to module share..."
    Copy-Item -Path $archive -Destination $modulespath -ErrorAction Stop

    write-output "Copying checksum to module share..."
    Copy-Item -Path "$archive.checksum" -Destination $modulespath -ErrorAction Stop

Document Actions