cIBMWebSpherePortalUtils.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
##############################################################################################################
######## IBM WebSphere Portal CmdLets #########
##############################################################################################################

enum PortalEdition {
    MP
    EXPRESS
    WCM
    EXTEND
}

##############################################################################################################
# Get-IBMWebSpherePortalVersionInfo
# Returns a hashtable containing version information of the IBM Portal Product/Components installed
##############################################################################################################
Function Get-IBMWebSpherePortalVersionInfo() {
    [CmdletBinding(SupportsShouldProcess=$False)]
    param (
        [parameter(Mandatory=$true,position=0)]
        [string]
        $InstallationDirectory
    )

    Write-Verbose "Get-IBMWebSpherePortalVersionInfo::ENTRY(InstallationDirectory=$InstallationDirectory)"
    
    #Validate Parameters
    [string] $versionInfoBat = Join-Path -Path $InstallationDirectory -ChildPath "PortalServer\bin\WPVersionInfo.bat"
    if (!(Test-Path($versionInfoBat))) {
        Write-Error "Invalid InstallationDirectory: $versionInfoBat not found"
        Return $null
    }
        
    [hashtable] $VersionInfo = @{}
    $versionInfoProcess = Invoke-ProcessHelper -ProcessFileName $versionInfoBat
    
    if ($versionInfoProcess -and ($versionInfoProcess.ExitCode -eq 0)) {
        $output = $versionInfoProcess.StdOut
        if ($output) {
            # Parse installation info
            $matchFound = $output -match "\nInstallation\s+\n\-+\s\n((.|\n)*?)Technology\sList"
            if ($matchFound -and $matches -and ($matches.Count -gt 1)) {
                $matches[1] -Split "\n" | % {
                    $matchLine = $_.trim()
                    if (!([string]::IsNullOrEmpty($matchLine))) {
                        $nameValue = $matchLine -split "\s\s+"
                        $VersionInfo.Add($nameValue[0].trim(), $nameValue[1].trim())
                    }
                }
            }
            # Parse list of installed products
            $matchFound = $output -match "\nTechnology\sList\s+\n\-+\s\n((.|\n)*?)Installed\sProduct"
            if ($matchFound -and $matches -and ($matches.Count -gt 1)) {
                [hashtable] $products = @{}
                $matches[1] -Split "\n" | % {
                    $matchLine = $_.trim()
                    if (!([string]::IsNullOrEmpty($matchLine))) {
                        $nameValue = $matchLine -split "\s\s+"
                        $products.Add($nameValue[0].trim(), $null)
                    }
                }

                # Parse product specific info
                $pattern = "Installed\s(Product|Component)\s+\n\-+\s\n(.|\n)*?\n\s((\n|\nPackage)*?)"
                $output | Select-String -AllMatches $pattern | Select-Object -ExpandProperty Matches | Select-Object -ExpandProperty Value | % {
                    $prodMatchFound = $_ -match "Installed\s(Product|Component)\s+\n\-+\s\n((.|\n)*?)\n\s"
                    if ($prodMatchFound -and $matches -and ($matches.Count -gt 1)) {
                        [hashtable] $product = @{}
                        $currentKey = $null
                        $matches[2] -Split "\n" | % {
                            [string] $matchLine = $_.trim()
                            if (!([string]::IsNullOrEmpty($matchLine))) {
                                if ($matchLine.IndexOf(" ") -gt 0) {
                                    $nameValue = $matchLine -split "\s\s+"
                                    if ($nameValue) {
                                        $currentKey = $nameValue[0].trim()
                                        $product.Add($currentKey, $nameValue[1].trim())
                                    }
                                } else {
                                    $valueArray = @()
                                    $currentValue = $product[$currentKey]
                                    $valueArray += $currentValue
                                    $valueArray += $matchLine
                                    $product[$currentKey] = $valueArray
                                }
                            }
                        }
                        if (($product.Count -gt 0) -and $products.ContainsKey($product.ID)) {
                            $products[$product.ID] = $product
                        }
                    }
                }
                $VersionInfo.Add("Products", $products)
            } else {
                Write-Error "Unable to parse any product from output: $output"
            }
        } else {
            Write-Error "No output returned from WPVersionInfo.bat"
        }
    } else {
        $errorMsg = (&{if($versionInfoProcess) {$versionInfoProcess.StdOut} else {$null}})
        Write-Error "An error occurred while executing the WPVersionInfo.bat process: $errorMsg"
    }
    
    return $VersionInfo
}

##############################################################################################################
# Get-IBMWebSpherePortalFixesInstalled
# Returns an array containing the list of the IBM WebSphere Portal interim fixes installed
##############################################################################################################
Function Get-IBMWebSpherePortalFixesInstalled() {
    [CmdletBinding(SupportsShouldProcess=$False)]
    param (
        [parameter(Mandatory=$true,position=0)]
        [string]
        $InstallationDirectory
    )

    #Validate Parameters
    if (!(Test-Path($InstallationDirectory))) {
        Write-Error "Get-IBMWebSpherePortalFixesInstalled:ERROR:Parameter InstallationDirectory with value=$InstallationDirectory does not exists" -ForegroundColor DarkYellow
        Return
    }
    
    [string[]] $installedFixes = @()
    $wpver_bat = Join-Path -Path $InstallationDirectory -ChildPath "\PortalServer\bin\WPVersionInfo.bat"
    $wpver_args = @("-fixes")
    $versionInfoProcess = Invoke-ProcessHelper -ProcessFileName $wpver_bat -ProcessArguments $wpver_args
    
    if ($versionInfoProcess -and ($versionInfoProcess.ExitCode -eq 0)) {
        $fixesOutput = ($versionInfoProcess.StdOut) -split "\n"
        $fixesOutput | Select-String -Pattern "^(Ifix ID\s\s)" -CaseSensitive | % {
            $installedFixes += ((($_.Line).Substring("Ifix ID".Length)).Trim())
        }
    }
    
    return $installedFixes
}

##############################################################################################################
# Install-IBMWebSpherePortal
# Installs IBM WebSphere Portal on the current machine
##############################################################################################################
Function Install-IBMWebSpherePortal() {
    [CmdletBinding(SupportsShouldProcess=$False)]
    param (
        [parameter(Mandatory = $true)]
        [System.String]
        $InstallMediaConfig,
        
        [parameter(Mandatory = $true)]
        [System.String]
        $ResponseFileTemplate,
        
        [System.String]
        $InstallationDirectory = "C:\IBM\WebSphere",
        
        [parameter(Mandatory = $true)]
        [System.String]
        $IMSharedLocation,
        
        [System.String]
        $CellName = "wpCell",
        
        [System.String]
        $ProfileName = "wp_profile",

        [System.String]
        $NodeName = "wpNode",
        
        [System.String]
        $ServerName = "WebSphere_Portal",
        
        [parameter(Mandatory = $true)]
        [System.String]
        $HostName,
        
        [parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $WebSphereAdministratorCredential,
        
        [System.Management.Automation.PSCredential]
        $PortalAdministratorCredential,

        [parameter(Mandatory = $true)]
        [System.String]
        $SourcePath,

        [System.Management.Automation.PSCredential]
        $SourcePathCredential
    )
    
    $installed = $false
    # Populate variables needed by response file templates
    [Hashtable] $Variables = @{}
    $Variables.Add("sharedLocation", $IMSharedLocation)
    $Variables.Add("wasInstallLocation", $InstallationDirectory)
    $Variables.Add("cellName", $CellName)
    $Variables.Add("profileName", $ProfileName)
    $Variables.Add("nodeName", $NodeName)
    $Variables.Add("hostName", $HostName)
    $Variables.Add("wasadmin", $WebSphereAdministratorCredential.UserName)
    $Variables.Add("wasadminPwd", $WebSphereAdministratorCredential)
    if ($PortalAdministratorCredential) {
        $Variables.Add("wpadmin", $PortalAdministratorCredential.UserName)
        $Variables.Add("wpadminPwd", $PortalAdministratorCredential)
    } else {
        $Variables.Add("wpadmin", $WebSphereAdministratorCredential.UserName)
        $Variables.Add("wpadminPwd", $WebSphereAdministratorCredential)
    }
    
    # Install Portal via IBM Installation Manager
    $installed = Install-IBMProduct -InstallMediaConfig $InstallMediaConfig `
        -ResponseFileTemplate $ResponseFileTemplate -Variables $Variables `
        -SourcePath $SourcePath -SourcePathCredential $SourcePathCredential -ErrorAction Stop
    
    Write-Verbose "IBM WebSphere Portal installation process finished"
    
    $wpProfileHome = Join-Path -Path $InstallationDirectory -ChildPath $ProfileName
    $WASInsDir = Get-IBMWebSphereAppServerInstallLocation -WASEdition ND
    
    if ($installed -and (Test-Path($wpProfileHome)) -and $WASInsDir -and (Test-Path($WASInsDir))) {
        $installed = $false
        # Create a windows service for the WAS server
        $wasWinSvcName = New-IBMWebSphereAppServerWindowsService -ProfilePath $wpProfileHome -ServerName $ServerName `
                            -WASEdition ND -WebSphereAdministratorCredential $WebSphereAdministratorCredential
        if ($wasWinSvcName -and (Get-Service -DisplayName $wasWinSvcName)) {
            Write-Verbose "IBM WebSphere Windows Service configured successfully"
            $installed = $true
        } else {
            Write-Verbose "IBM WebSphere Portal was not installed correctly (Windows Service Does Not Exists). Please check the installation logs"
        }
    } else {
        Write-Verbose "An error occurred while installing IBM WebSphere Portal, please check installation logs"
    }

    Return $installed
}