PembrokePSutilities.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
Write-Verbose 'Importing from [C:\projects\pembrokepsutilities\PembrokePSutilities\private]'
Write-Verbose 'Importing from [C:\projects\pembrokepsutilities\PembrokePSutilities\public]'
# .\PembrokePSutilities\public\Get-LocalPropertySet.ps1
function Get-LocalPropertySet {
    <#
    .DESCRIPTION
        This function will gather Key value pairs from a properties file.
    .PARAMETER PropertyFilePath
        A path is Required.
    .PARAMETER LOG_FILE
        A LOG_FILE is optional.
    .EXAMPLE
        Get-LocalPropertySet -PropertyFilePath c:\pps\qman\pembrokeps.properties
    .NOTES
        This file is generated during the deployment or Registration of tha Pembroke PS component.
    #>

    [CmdletBinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
    [OutputType([System.Object[]])]
    param(
        [Parameter(Mandatory=$true)][string]$PropertyFilePath,
        [string]$LOG_FILE = "C:\temp\nolog.log"
    )
    if (Test-Path -Path $PropertyFilePath) {
        try
        {
            Write-LogLevel -Message "Gathering Local Properties" -Logfile "$LOG_FILE" -RunLogLevel CONSOLEONLY -MsgLevel CONSOLEONLY
            $PpsPropertiesHash = ConvertFrom-StringData (Get-Content $PropertyFilePath -raw)
            $PpsProperties = @()
            $PpsPropertiesHash | ForEach-Object {
                $result = New-Object psobject;
                foreach ($key in $_.keys) {
                    $result | Add-Member -MemberType NoteProperty -Name $key -Value $_[$key]
                 }
                 $PpsProperties += $result;
             }
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName        
            Throw "Get-LocalPropertySet: $ErrorMessage $FailedItem"
        }
        $PpsProperties
    } else {
        Throw "Get-LocalPropertySet: Property File path: $PropertyFilePath does not exist."
    }
    
}
# .\PembrokePSutilities\public\Get-PpsProcessStatus.ps1
function Get-PpsProcessStatus {
    <#
    .DESCRIPTION
        This function will gather Key value pairs from a properties file.
    .PARAMETER ProcessName
        A path is Required.
    .EXAMPLE
        Get-PpsProcessStatus -ProcessName WmanKicker
    .NOTES
        This file will return true or false
    #>

    [CmdletBinding()]
    [OutputType([Boolean])]
    param(
        [Parameter(Mandatory=$true)][string]$ProcessName
    )
    try 
    {
        $ReturnData = Get-Process | Where-Object {($_.Name -eq "powershell") -and ($_.MainWindowTitle -like "*$ProcessName*")}
        $ReturnDataCount = ($ReturnData | Measure-Object).count
        If($ReturnDataCount -eq 1) {
            return $true
        } else {
            return $false
        }
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName        
        Throw "Get-PpsProcessStatus: $ErrorMessage $FailedItem"
    }
}
    
# .\PembrokePSutilities\public\Invoke-InstallRequiredModuleSet.ps1
function Invoke-InstallRequiredModuleSet {
    <#
    .DESCRIPTION
        This function will Install additional Modules for a PembrokePS component.
    .PARAMETER RequiredModuleSet
        A RequiredModuleSet is Required.
    .EXAMPLE
        Invoke-InstallRequiredModuleSet -RequiredModuleSet "Powerlumber,PembrokePSrest"
    .NOTES
        This will Install additional Modules for a Workflow_Manager.
    #>

    [CmdletBinding()]
    [OutputType([Boolean])]
    param(
        [Parameter(Mandatory=$true)][string]$RequiredModuleSet
    )
    try
    {
        Write-Output "Checking for installation of required modules."
        $RequiredModuleList = $RequiredModuleSet.split(",")
        foreach ($RequiredModule in $RequiredModuleList){
            $localModuledataCount = (Get-Module -ListAvailable $RequiredModule | Measure-Object).count
            if($localModuledataCount -ge 1){
                Write-Output "Module: $RequiredModule is installed on this device."
            } else {
                Write-Output "Installing Required Module: $RequiredModule"
                Install-Module -Name $RequiredModule -Force
            }
        }
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName        
        Throw "Invoke-InstallRequiredModuleSet: $ErrorMessage $FailedItem"
    }
}
    
# .\PembrokePSutilities\public\Invoke-Wait.ps1
function Invoke-Wait
{
    <#
    .DESCRIPTION
        This function will pause for the specified amount of time.
    .PARAMETER Seconds
        A valid integer is optional. Default is 10 seconds
    .EXAMPLE
        Invoke-Wait -Seconds 15
    .NOTES
        It's pretty simple.
    #>

    [CmdletBinding()]
    [OutputType([boolean])]
    param(
        [Int]$Seconds = 10
    )
    try
    {
        Write-output "Waiting $Seconds, Seconds"
        start-sleep -s $Seconds
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName        
        Throw "Invoke-Wait: $ErrorMessage $FailedItem"
    }
}
Write-Verbose 'Importing from [C:\projects\pembrokepsutilities\PembrokePSutilities\classes]'