Diagnostics/Simple/OVF.SharePoint.Tests.ps1

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

#Functions
function Test-OPSValServiceState{
    
    param(
        $serviceName,
        $computerName = "."
    )
    
    if(($computerName -eq ".") -or ($computerName -eq "$env:computername") -or ($computerName -eq "localhost") -or ($computerName -eq "$env:computername.$env:USERDNSDOMAIN")){
        
        $service = Get-Service -Name $serviceName
        
    }
    else{
        
        $service = Get-Service -Name $serviceName -ComputerName $computerName
        
    }

    return $service

}

function Test-OPSValWebAppPoolState{
    
    param(
        $computerName = "."
    )


    if(($computerName -eq ".") -or ($computerName -eq "$env:computername") -or ($computerName -eq "localhost") -or ($computerName -eq "$env:computername.$env:USERDNSDOMAIN")){

        $iisPool = Get-ChildItem -Path IIS:\AppPools | Where-Object{($_.Name -ne "SharePoint Web Services Root") -and ($_.State -ne "Started")}

    }
    else{

        $template = 'Import-Module WebAdministration;Get-ChildItem -Path IIS:\AppPools | Where-Object{($_.Name -ne "SharePoint Web Services Root") -and ($_.State -ne "Started")}'
        $sb = [ScriptBlock]::Create($template)

        $session = New-PSSession -ComputerName $computerName
        
        $iisPool =  Invoke-Command -Session $session -ScriptBlock $sb
        
        Remove-PSSession -Session $session

    }

    return $iispool

}

#Modules
Import-Module Pester
Import-Module OperationValidation
Add-PSSnapin Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue
Import-Module WebAdministration


Describe "Operational Validation of SharePoint 2013" {
    
    Context "Windows Services" {
    
        #Windows Service Area
        $serviceName = "W3SVC"
        $computerName = "localhost"

        It "Service $serviceName State should be running" {

            $service = Test-OPSValServiceState -serviceName $serviceName -ComputerName $computerName
            $service.Status | Should be running
        }

        $serviceName = "SPSearchHostController"

        It "Service $serviceName State should be running" {

            $service = Test-OPSValServiceState -serviceName $serviceName -ComputerName $computerName
            $service.Status | Should be running
        }

        $serviceName = "OSearch15"

        It "Service $serviceName State should be running" {

            $service = Test-OPSValServiceState -serviceName $serviceName -ComputerName $computerName
            $service.Status | Should be running
        }


        $serviceName = "SPTimerV4"

        It "Service $serviceName State should be running" {

            $service = Test-OPSValServiceState -serviceName $serviceName -ComputerName $computerName
            $service.Status | Should be running
        }


        $serviceName = "SPTraceV4"

        It "Service $serviceName State should be running" {

            $service = Test-OPSValServiceState -serviceName $serviceName -ComputerName $computerName
            $service.Status | Should be running
        }

        
        $serviceName = "AppFabricCachingService"

        It "Service $serviceName State should be running" {

            $service = Test-OPSValServiceState -serviceName $serviceName -ComputerName $computerName
            $service.Status | Should be running
        }

        
    }

    Context "WebApp Pools"{
    
        #Web App Pools Area
        It "Web Application Pools in IIS should be running" {

            $iispool = Test-OPSValWebAppPoolState

            ($iisPool | Measure-Object).Count | Should be 0    
        }
    
    }
    
    Context "URLCheck" {
        #Url Areas
        $url = "https://sharepoint-test.mydomain.com"

        It "WebSite $url should be running and login working" {

            $request = Invoke-WebRequest -Uri $url -UseDefaultCredentials
            $request.StatusCode | Should be 200                
        }
        
        $url = "https://server:centralAdminPort/"

        It "The Central Administration on $url should be running and login working" {

            $request = Invoke-WebRequest -Uri $url -UseDefaultCredentials
            $request.StatusCode | Should be 200                
        }
    
    }
    
    Context "SP Sites Test"{

        #SharePoint Site Tests
        $webAppUrl = "https://sharepoint-test.domain.com/"

        It "The SharePoint Sites should have no Errors"{
            
            $webApp = Get-SPWebApplication -Identity $webAppUrl

            $sites = Get-SPSite -WebApplication $webApp -Limit All

            $result = $sites | Test-SPSite

            $sitesWithErrors = $null
            $sitesWithErrors = $result | Where-Object{$_.FailedErrorCount -gt 0}

            if(-not([String]::IsNullOrEmpty($sitesWithErrors))){
                $testResult = "Failed"
            }
            else{
                $testResult = "Succeeded"
            }

            $testResult | Should be Succeeded

        }
    }

    
    Context "SP Upgrade Status"{

        #SharePoint DB Tests

        It "The SharePoint Databases should have no Upgrades pending"{
            
            $dbs = Get-SPDatabase
            $dbsWithUpgradeNeeded = $dbs | Where-Object{$_.NeedsUpgrade -eq $true}

            if($dbsWithUpgradeNeeded){
                Write-Warning "DBs needing Upgrade:"
                Write-Host ($dbsWithUpgradeNeeded | Out-String)
            }
            
            $dbsWithUpgradeNeeded | Should be $null

        }
    }

}