functions/Get-DbaDefaultPath.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
function Get-DbaDefaultPath {
<#
 .SYNOPSIS
  Gets the default SQL Server paths for data, logs and backups
  
 .DESCRIPTION
  Gets the default SQL Server paths for data, logs and backups
  
 .PARAMETER SqlInstance
  The SQL Server instance, or instances.
  
 .PARAMETER SqlCredential
  Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
  
 .PARAMETER EnableException
  By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
  This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
  Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
   
 .NOTES
  Tags: Config
  Website: https://dbatools.io
  Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
  License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0    
  
 .LINK
  https://dbatools.io/Get-DbaDefaultPath
  
 .EXAMPLE
  Get-DbaDefaultPath -SqlInstance sql01\sharepoint
   
  Returns the default file paths for sql01\sharepoint
  
 .EXAMPLE
  $servers = "sql2014","sql2016", "sqlcluster\sharepoint"
  $servers | Get-DbaDefaultPath
   
  Returns the default file paths for "sql2014","sql2016" and "sqlcluster\sharepoint"
 
#>
    
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true)]
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstanceParameter[]]$SqlInstance,
        [Alias("Credential")]
        [PSCredential]
        $SqlCredential,
        [switch][Alias('Silent')]$EnableException
    )
    process {
        foreach ($instance in $SqlInstance) {
            Write-Message -Level Verbose -Message "Attempting to connect to $instance"
            
            try {
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential
            }
            catch {
                Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
            }
            
            $datapath = $server.DefaultFile
            
            if ($datapath.Length -eq 0) {
                $datapath = $server.ConnectionContext.ExecuteScalar("SELECT SERVERPROPERTY('InstanceDefaultDataPath')")
            }
            
            <# eh, this can be a failback if we can get it to work ;)
   if ($datapath -eq [System.DBNull]::Value -or $datapath.Length -eq 0) {
    $datapath = $server.ConnectionContext.ExecuteScalar("master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultData'")
   }
   #>

            
            if ($datapath -eq [System.DBNull]::Value -or $datapath.Length -eq 0) {
                $datapath = Split-Path (Get-DbaDatabase -SqlInstance $server -Database model).FileGroups[0].Files[0].FileName
            }
            
            if ($datapath.Length -eq 0) {
                $datapath = $server.Information.MasterDbPath
            }
            
            $logpath = $server.DefaultLog
            
            if ($logpath.Length -eq 0) {
                $logpath = $server.ConnectionContext.ExecuteScalar("SELECT SERVERPROPERTY('InstanceDefaultLogPath')")
            }
            
            <#
   eh, this can be a failback if we can get it to work ;)
   right now, the Get-Database thing below causes enumeration which slows stuff down on systems with hundreds of dbs
   if ($logpath -eq [System.DBNull]::Value -or $logpath.Length -eq 0) {
    $logpath = $server.ConnectionContext.ExecuteScalar("master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultLog'")
   }
   #>

            
            if ($logpath -eq [System.DBNull]::Value -or $logpath.Length -eq 0) {
                $logpath = Split-Path (Get-DbaDatabase -SqlInstance $server -Database model).LogFiles.FileName
            }
            
            if ($logpath.Length -eq 0) {
                $logpath = $server.Information.MasterDbLogPath
            }
            
            $datapath = $datapath.Trim().TrimEnd("\")
            $logpath = $logpath.Trim().TrimEnd("\")
            
            [pscustomobject]@{
                ComputerName  = $server.NetName
                InstanceName  = $server.ServiceName
                SqlInstance   = $server.DomainInstanceName
                Data          = $datapath
                Log              = $logpath
                Backup          = $server.BackupDirectory
                ErrorLog      = $server.ErrorLogPath
            }
        }
    }
}