SifHelper.psm1

#Requires -RunAsAdministrator
#Requires -Modules PKI,WebAdministration

Set-StrictMode -Version Latest

try {

}
catch {

}
# Pass verbose on if required
$verbose = $false
if ($PSBoundParameters.ContainsKey('Verbose')) {
    $verbose = $PSBoundParameters["Verbose"]
}

$whatif = $false
if ($PSBoundParameters.ContainsKey('WhatIf')) {
    $whatif = $PSBoundParameters["WhatIf"]
}

try {

    $DestinationFolder = Split-Path $PSScriptRoot -Parent
    $filename = "SifHelper.{0:yyMMdd}" -f (Get-Date)

    $counter = 2
    $fullPath = Join-Path $DestinationFolder "$filename.log"
    while (Test-Path $fullPath) {
        $fullPath = Join-Path $DestinationFolder "$filename ($($counter)).log"
        $counter++
    }

    Start-Transcript -Path $fullpath -IncludeInvocationHeader

    $nugetPackageProvider = Get-PackageProvider -Name NuGet -Force -Verbose:$verbose -ErrorAction Stop
    if (!$nugetPackageProvider) {
        Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Confirm:$false -Verbose:$verbose
    }

    # TODO remove the module imports for PKI and WebAdministration.. should be done in module .PSD1
    if (!(Get-Module PKI)) { Import-Module PKI -ErrorAction Stop -Verbose:$verbose }

    if (!(Get-Module WebAdministration) -or (!(Get-PSDrive IIS -ErrorAction SilentlyContinue -Verbose:$verbose))) {
        Get-Module WebAdministration | Remove-Module -Force -Confirm:$false
        Import-Module WebAdministration -ErrorAction Stop -Force -Verbose:$verbose
    }

    $SitecoreGalleryUrl = "https://sitecore.myget.org/F/sc-powershell/api/v2"

    if (-not(Get-PSRepository -name SitecoreGallery)) {
        Register-PSRepository -Name SitecoreGallery -SourceLocation $SitecoreGalleryUrl -InstallationPolicy Trusted -Verbose:$verbose
    }

    ("SitecoreInstallFramework", "SqlServer") | ForEach-Object -Verbose:$verbose {

        if ((Get-InstalledModule -Name $_ -Verbose:$verbose -ErrorAction SilentlyContinue)) {
            Update-Module $_ -Confirm:$false -Verbose:$verbose -Force
        }
        else {
            Install-Module $_ -Confirm:$false -AllowClobber -SkipPublisherCheck -Force -Verbose:$verbose
        }

        if (!(Get-Module $_)) {
            Write-Warning "Module '$_' is not loaded!"
            Import-Module $_ -Force -Verbose:$verbose
        }
    }

    $scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path

    $privates = Join-Path -Path $scriptRoot -ChildPath 'Private'
    $publics = Join-Path -Path $scriptRoot -ChildPath 'Public'

    Get-ChildItem -Path $privates -Filter '*.psm1' |
        ForEach-Object {
        Import-Module $_.FullName -Force -Verbose:$verbose
    }

    Get-ChildItem -Path $publics -Filter '*.psm1;*.ps1' |
        ForEach-Object {
        Import-Module $_.FullName -Global -Force -Verbose:$verbose
    }

    # Dot source to scope
    # Private must be sourced first - usage in public functions during load
    ('Private' , 'Public') | ForEach-Object {

        Get-ChildItem -Path (Join-Path $scriptRoot $_) -Include *.ps1 -Exclude *.Tests.ps1 -File -Recurse |
            ForEach-Object {
            try {
                . $_.FullName
            }
            catch {
                Write-Warning $_.Exception.Message
            }
        }
    }

    # Export Public functions from the module
    Get-ChildItem (Join-Path $scriptRoot Public) -Include *.ps1 -Exclude *.Tests.ps1 -File -Recurse |
        ForEach-Object {
        Export-ModuleMember -Function $_.BaseName
    }

    $webpicmd = Join-Path $env:ProgramFiles 'Microsoft\Web Platform Installer\webpicmd.exe'
    $products = ('WDeploy36PS', 'UrlRewrite2', 'DACFx') -join ','
    & $webpicmd /Install /Products:$products /AcceptEula /SuppressReboot

}
catch {
    Write-Error $_
    throw
}
finally {
    Stop-Transcript
}