functions/Get-DbaUptime.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
function Get-DbaUptime
{
<#
.SYNOPSIS
Returns the uptime of the SQL Server instance, and if required the hosting windows server
  
.DESCRIPTION
By default, this command returns for each SQL Server instance passed in:
SQL Instance last startup time, Uptime as a PS TimeSpan, Uptime as a formatted string
Hosting Windows server last startup time, Uptime as a PS TimeSpan, Uptime as a formatted string
  
.PARAMETER SqlInstance
The SQL Server that you're connecting to.
 
.PARAMETER SqlCredential
Credential object used to connect to the SQL Server as a different user
 
.PARAMETER WindowsCredential
Credential object used to connect to the SQL Server as a different user
 
.PARAMETER SqlOnly
Excludes the Windows server information
 
.PARAMETER WindowsOnly
Excludes the SQL server information
 
.PARAMETER Silent
Use this switch to disable any kind of verbose messages
 
.NOTES
Tags: CIM
Original Author: Stuart Moore (@napalmgram), stuart-moore.com
  
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
Copyright (C) 2016 Chrissy LeMaire
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>
 
.LINK
https://dbatools.io/Get-DbaUptime
 
.EXAMPLE
Get-DbaUptime -SqlInstance SqlBox1\Instance2
 
Returns an object with SQL Server start time, uptime as TimeSpan object, uptime as a string, and Windows host boot time, host uptime as TimeSpan objects and host uptime as a string for the sqlexpress instance on winserver
 
.EXAMPLE
Get-DbaUptime -SqlInstance winserver\sqlexpress, sql2016
 
Returns an object with SQL Server start time, uptime as TimeSpan object, uptime as a string, and Windows host boot time, host uptime as TimeSpan objects and host uptime as a string for the sqlexpress instance on host winserver and the default instance on host sql2016
  
.EXAMPLE
Get-DbaUptime -SqlInstance sqlserver2014a, sql2016 -SqlOnly
 
Returns an object with SQL Server start time, uptime as TimeSpan object, uptime as a string for the sqlexpress instance on host winserver and the default instance on host sql2016
 
.EXAMPLE
Get-DbaRegisteredServerName -SqlInstance sql2014 | Get-DbaUptime
 
Returns an object with SQL Server start time, uptime as TimeSpan object, uptime as a string, and Windows host boot time, host uptime as TimeSpan objects and host uptime as a string for every server listed in the Central Management Server on sql2014
  
#>

    [CmdletBinding(DefaultParameterSetName = "Default")]
    Param (
        [parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [Alias("ServerInstance", "SqlServer", "ComputerName")]
        [DbaInstanceParameter[]]$SqlInstance,
        [parameter(ParameterSetName = "Sql")]
        [Switch]$SqlOnly,
        [parameter(ParameterSetName = "Windows")]
        [Switch]$WindowsOnly,
        [Alias("Credential")]
        [PSCredential]$SqlCredential,
        [PSCredential]$WindowsCredential,
        [switch]$Silent
    )
    
    begin {
        $nowutc = (Get-Date).ToUniversalTime()
    }
    process
    {
        foreach ($instance in $SqlInstance)
        {
            if ($instance.Gettype().FullName -eq [System.Management.Automation.PSCustomObject] )
            {
                $servername = $instance.SqlInstance
            }
            elseif ($instance.Gettype().FullName -eq [Microsoft.SqlServer.Management.Smo.Server])
            {
                $servername = $instance.NetName
            }
            else
            {
                $servername = $instance
            }

            if ($WindowsOnly -ne $true)
            {
                try {
                    Write-Message -Level Verbose -Message "Connecting to $instance"
                    $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $sqlcredential
                }
                catch {
                    Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
                }
                Write-Message -Level Verbose -Message "Getting Start times for $servername"
                #Get tempdb creation date
                $SQLStartTime = $server.Databases["tempdb"].CreateDate
                $SQLUptime = New-TimeSpan -Start $SQLStartTime.ToUniversalTime() -End $nowutc
                $SQLUptimeString = "{0} days {1} hours {2} minutes {3} seconds" -f $($SQLUptime.Days), $($SQLUptime.Hours), $($SQLUptime.Minutes), $($SQLUptime.Seconds)
            }
            
            if ($SqlOnly -ne $true)
            {
                $WindowsServerName = (Resolve-DbaNetworkName $servername -Credential $WindowsCredential).FullComputerName

                try
                {
                    Write-Message -Level Verbose -Message "Getting WinBootTime via CimInstance for $servername"
                    $WinBootTime = (Get-DbaOperatingSystem -ComputerName $windowsServerName -Credential $WindowsCredential -ErrorAction SilentlyContinue).LastBootUpTime
                    $WindowsUptime = New-TimeSpan -start $WinBootTime.ToUniversalTime() -end $nowutc
                    $WindowsUptimeString = "{0} days {1} hours {2} minutes {3} seconds" -f $($WindowsUptime.Days), $($WindowsUptime.Hours), $($WindowsUptime.Minutes), $($WindowsUptime.Seconds)
                }
                catch
                {
                    try
                    {
                        Write-Message -Level Verbose -Message "Getting WinBootTime via CimInstance DCOM"
                        $CimOption = New-CimSessionOption -Protocol DCOM
                        $CimSession = New-CimSession -Credential:$WindowsCredential -ComputerName $WindowsServerName -SessionOption $CimOption
                        $WinBootTime = ($CimSession | Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime
                        $WindowsUptime = New-TimeSpan -start $WinBootTime.ToUniversalTime() -end $nowutc
                        $WindowsUptimeString = "{0} days {1} hours {2} minutes {3} seconds" -f $($WindowsUptime.Days), $($WindowsUptime.Hours), $($WindowsUptime.Minutes), $($WindowsUptime.Seconds)
                    }
                    catch
                    {
                        Stop-Function -Message "Failure getting WinBootTime" -ErrorRecord $_ -Target $instance -Continue
                    }
                }
                
                if ($null -eq $WinBootTime)
                {
                    #Skip the windows results as they'll either be garbage or not there.
                    $SqlOnly = $true
                }
            }
            
            if ($SqlOnly -eq $true)
            {
                [PSCustomObject]@{
                    ComputerName = $server.NetName
                    InstanceName = $server.ServiceName
                    SqlServer = $server.Name
                    SqlUptime = $SQLUptime
                    SqlStartTime = $SQLStartTime
                    SinceSqlStart = $SQLUptimeString
                }
            }
            elseif ($WindowsOnly -eq $true)
            {
                [PSCustomObject]@{
                    ComputerName = $WindowsServerName
                    WindowsUptime = $WindowsUptime
                    WindowsBootTime = $WinBootTime
                    SinceWindowsBoot = $WindowsUptimeString
                }
            }
            else
            {
                [PSCustomObject]@{
                    ComputerName = $WindowsServerName
                    InstanceName = $server.ServiceName
                    SqlServer = $server.Name
                    SqlUptime = $SQLUptime
                    WindowsUptime = $WindowsUptime
                    SqlStartTime = $SQLStartTime
                    WindowsBootTime = $WinBootTime
                    SinceSqlStart  = $SQLUptimeString
                    SinceWindowsBoot = $WindowsUptimeString
                }
            }
        }
    }
}