PembrokePSui.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
Write-Verbose 'Importing from [C:\projects\pembrokepsui\PembrokePSui\private]'
# .\PembrokePSui\private\New-WebDirectory.ps1
function New-WebDirectory
{
    <#
    .DESCRIPTION
        Create Destination Directory.
    .PARAMETER Destination
        A valid Path String is required.
    .EXAMPLE
        New-WebDirectory -Destination c:\wamp\www\PembrokePS
    .NOTES
        It will create the directory if it does not exist.
    #>

    [CmdletBinding(
        SupportsShouldProcess = $true,
        ConfirmImpact = "Low"
    )]
    [OutputType([boolean])]
    param(
        [Parameter(Mandatory = $true)][String]$Destination
    )
    begin
    {
        # Nothing to see here.
    }
    process 
    {
        if ($pscmdlet.ShouldProcess("Creating New Directory."))
        {
            try
            {
                if(Test-Path -Path "$Destination") {
                    return $true
                } else {
                    New-Item -Path "$Destination" -ItemType Directory -Force -Confirm:$false
                }
            }
            catch
            {
                $ErrorMessage = $_.Exception.Message
                $FailedItem = $_.Exception.ItemName        
                Throw "Invoke-CreateWebDirectory: $ErrorMessage $FailedItem"

            }
        }
        else
        {
            # -WhatIf was used.
            return $false
        }
    }
}
Write-Verbose 'Importing from [C:\projects\pembrokepsui\PembrokePSui\public]'
# .\PembrokePSui\public\Invoke-DeployWeb.ps1
function Invoke-DeployWeb
{
    <#
    .DESCRIPTION
        Deploy .php pages to web server.
    .PARAMETER Destination
        A valid Path String is required for destination.
    .PARAMETER Source
        A valid Path String is required.
    .EXAMPLE
        Invoke-DeployWeb -Destination c:\wamp\www\PembrokePS -Source c:\OpenProjects\ProjectPembroke\PembrokePSui\PembrokePSui\php
    .NOTES
        It will create the directory if it does not exist.
    #>

    [CmdletBinding()]
    [OutputType([boolean])]
    param(
        [String]$Destination = 'C:\wamp\www\PembrokePS',
        [Parameter(Mandatory = $true)][String]$Source
    )
    try
    {
        New-WebDirectory -Destination $Destination
        
        if(Test-Path -Path "$Source") {
            Copy-Item -Path "$Source\*" -Destination $Destination -Recurse -Confirm:$false -Force
            $Directories = Get-ChildItem -Path $Source -Directory | Select-Object Name
            $DirectoryCount = ($Directories | Measure-Object).count
            if($DirectoryCount -gt 0){
                foreach($Directory in $Directories){
                    $DirectoryName = $Directory.Name
                    $NewSource = $Source + "\" + $DirectoryName
                    Copy-Item -Path "$NewSource" -Destination $Destination -Container -Recurse -Confirm:$false -Force
                }
            }
        } else {
            Throw "Invoke-DeployWeb: Source Directory: $Source does not exist."
        }
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName        
        Throw "Invoke-DeployWeb: $ErrorMessage $FailedItem"
        
    }

}
Write-Verbose 'Importing from [C:\projects\pembrokepsui\PembrokePSui\classes]'