DSCResources/ROSSDatabase/ROSSDatabase.schema.psm1

configuration ROSSDatabase {
<#
    .SYNOPSIS
        Creates the RES ONE Service Store database using the legacy console.
    .NOTES
        This also installs the legacy console (Setup & Sync Tool).
#>

    param (
        ## RES ONE Service Store database server name/instance (equivalient to DBSERVER).
        [Parameter(Mandatory)] [ValidateNotNullOrEmpty()]
        [System.String] $DatabaseServer,
        
        ## RES ONE Service Store database name (equivalient to DBNAME).
        [Parameter(Mandatory)] [ValidateNotNullOrEmpty()]
        [System.String] $DatabaseName,
        
        ## Microsoft SQL username/password to create (equivalent to DBUSER/DBPASSWORD).
        [Parameter(Mandatory)]
        [System.Management.Automation.PSCredential] $Credential,
        
        ## Microsoft SQL database credentials used to create the database (equivalient to DBCREATEUSER/DBCREATEPASSWORD).
        [Parameter(Mandatory)]
        [System.Management.Automation.PSCredential] $SQLCredential,
        
        ## RES ONE Service Store Catalog Services password (equivalient to CATALOGSERVICESPASSWORD).
        [Parameter(Mandatory)]
        [System.Management.Automation.PSCredential] $CatalogServicesCredential,
        
        ## File path containing the RES ONE Service Store MSIs or the literal path to the legacy console/Sync Tool MSI.
        [Parameter(Mandatory)] [ValidateNotNullOrEmpty()]
        [System.String] $Path,

        ## RES ONE Service Store component version to be installed, i.e. 8.0.3.0
        [Parameter()] [ValidateNotNullOrEmpty()]
        [System.String] $Version,
        
        ## The specified Path is a literal file reference (bypasses the $Version and $Architecture checks).
        [Parameter()]
        [System.Boolean] $IsLiteralPath,
        
        ## The target node's architecture.
        [Parameter()] [ValidateSet('x64','x86')]
        [System.String] $Architecture = 'x64',

        [Parameter()] [ValidateSet('Present','Absent')]
        [System.String] $Ensure = 'Present'
    )

    Import-DscResource -ModuleName xPSDesiredStateConfiguration;

    $resourceName = 'ROSSDatabase';
    if (([System.String]::IsNullOrWhitespace($Version)) -and (-not $IsLiteralPath)) {
        throw "$resourceName : Version number is required when not using a literal path.";
    }
    elseif ($IsLiteralPath -and ($Path -notmatch '\.msi$')) {
        throw "$resourceName : Specified path '$Path' does not point to an MSI file.";
    }
    elseif ($Version -notmatch '^\d+\.\d+\.\d+\.\d+$') {
        throw "$resourceName : The specified version '$Version' does not match '1.2.3.4' format.";
    }
    
    if (-not $IsLiteralPath) {
        [System.Version] $Version = $Version;
        switch ($Version.Major) {
            7 {
                $setup = 'RES-ITS-Console({0})-{1}.msi' -f $Architecture, $Version.ToString();
                $name = 'RES IT Store Console 2014';
            }
            8 {
                $setup = 'RES-ONE-ServiceStore-2015-Setup-Sync-Tool({0})-{1}.msi' -f $Architecture, $Version.ToString();
                $name = 'RES ONE Service Store 2015 Setup & Sync Tool';
            }
            Default {
                throw "$resourceName : Version '$($Version.Tostring())' is not currently supported :(.";
            }
        }
        $Path = Join-Path -Path $Path -ChildPath $setup;
    }

    if ($Ensure -eq 'Present') {
        xPackage $resourceName {
            Name = $name;
            ProductId = '';
            Path = $Path;
            Arguments = 'DBSERVER="{0}" DBNAME="{1}" DBUSER="{2}" DBPASSWORD="{3}" DBTYPE="MSSQL" CATALOGSERVICESPASSWORD="{4}" DBCREATE="yes" DBCREATEUSER="{5}" DBCREATEPASSWORD="{6}"' -f $DatabaseServer, $DatabaseName, $Credential.Username, $Credential.GetNetworkCredential().Password, $CatalogServicesCredential.GetNetworkCredential().Password, $SQLCredential.Username, $SQLCredential.GetNetworkCredential().Password;
            ReturnCode = 0;
            InstalledCheckRegKey = 'Software\RES\ITStore\Console';
            InstalledCheckRegValueName = 'DBName';
            InstalledCheckRegValueData = $DatabaseName;
            Ensure = $Ensure;
        }
    }
    elseif ($Ensure -eq 'Absent') {
        xPackage $resourceName {
            Name = $name;
            ProductId = '';
            Path = $Path;
            ReturnCode = 0;
            InstalledCheckRegKey = 'Software\RES\ITStore\Console';
            InstalledCheckRegValueName = 'DBName';
            InstalledCheckRegValueData = $DatabaseName;
            Ensure = $Ensure;
        }
    }

} #end configuration ROSSDatabase