Public/Install-DSCPullServer.ps1

function Install-DSCPullServer {
    [CmdletBinding()]  
    param(
        [Parameter (Mandatory=$True)]
        [string]$VMName,

        [Parameter (Mandatory=$True)]
        [PSCredential]$Credential,

        [Parameter (Mandatory=$True)]
        [Version]$XPSDesiredStateConfigurationVersion,

        [int]$Attempts=1
    )
    try {
        if (-not((Get-PackageProvider).name.contains('Nuget'))) {
            Install-PackageProvider -Name NuGet -Confirm:$false -Force | Out-Null
        }
        if ((Test-Path "$env:TEMP\xPSDesiredStateConfiguration\$XPSDesiredStateConfigurationVersion") -eq $false) {
            Save-Module xPSDesiredStateConfiguration -RequiredVersion $XPSDesiredStateConfigurationVersion -Path $env:TEMP
        }
        $ErrorActionPreference = 'Stop'
        $Session = Wait-ForPSDirectSession -VMName $VMName -Credential $Credential -Attempts $Attempts
        $VMDirs = Invoke-Command -Session $Session -ScriptBlock {$env:ProgramFiles;$env:TEMP}
        Copy-Item "$env:TEMP\xPSDesiredStateConfiguration" -ToSession $Session -Destination "$($VMDirs[0])\WindowsPowerShell\Modules" -Recurse -Force
        Get-DSCLabConfiguration | Set-Content "$env:TEMP\DSCLabConfiguration.ps1" -Force
        Copy-Item "$env:TEMP\DSCLabConfiguration.ps1" -ToSession $Session -Destination "$($VMDirs[1])" -Force
        Invoke-Command -Session $Session -ScriptBlock {
            . $env:TEMP\DSCLabConfiguration.ps1
            $ThumbPrint = Get-ChildItem -Path cert:\localMachine\my | Where-Object {$_.FriendlyName -like "$($env:COMPUTERNAME)Cert"} | 
            Select-Object -ExpandProperty ThumbPrint -ErrorAction 'SilentlyContinue'
            if (-not($Thumbprint)) {
                $ThumbPrint = New-SelfSignedCertificate -DnsName $env:COMPUTERNAME -FriendlyName "$env:COMPUTERNAME`Cert" -CertStoreLocation "cert:\LocalMachine\My" |
                Select-Object -ExpandProperty Thumbprint
            }
            New-Item -ItemType Directory -Path "$env:TEMP\MOF" -Force | Out-Null
            Sample_xDscWebService -certificateThumbPrint $ThumbPrint -OutputPath "$env:TEMP\MOF" | Out-Null
            Start-DscConfiguration -Path "$env:TEMP\MOF" -Wait -Verbose -Force
        }
    }
    catch [System.Net.Sockets.SocketException],
          [System.Management.Automation.Remoting.PSDirectException],
          [System.Management.Automation.Remoting.PSRemotingDataStructureException] {
        $Attempts--
        if ($Attempts -lt 1) {
            Get-PSSession | Remove-PSSession -ErrorAction 'SilentlyContinue'
            throw "Ran out of attempts while trying to configure $VMName as a pull server"
        }
        $params = @{VMName                              = $VMName
                    Credential                          = $Credential
                    XPSDesiredStateConfigurationVersion = $XPSDesiredStateConfigurationVersion
                    Attempts                            = $Attempts}
        Install-DSCPullServer @params
    }
    catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }
    finally {
        Get-PSSession | Remove-PSSession -ErrorAction 'SilentlyContinue'
    }
}