functions/Get-DbaMsdtc.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
function Get-DbaMsdtc {
    <#
.SYNOPSIS
Displays information about the Distributed Transaction Coordinator (MSDTC) on a server
 
.DESCRIPTION
Returns a custom object with Computer name, state of the MSDTC Service, security settings of MSDTC and CID's
 
Requires: Windows administrator access on Servers
 
.PARAMETER ComputerName
The SQL Server (or server in general) that you're connecting to.
 
.NOTES
Tags: WSMan, CIM
Author: Klaas Vandenberghe ( powerdbaklaas )
 
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
Copyright (C) 2016 Chrissy LeMaire
License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
 
.LINK
https://dbatools.io/Get-DbaMsdtc
 
.EXAMPLE
Get-DbaMsdtc -ComputerName srv0042
 
Get DTC status for the server srv0042
 
ComputerName : srv0042
DTCServiceName : Distributed Transaction Coordinator
DTCServiceState : Running
DTCServiceStatus : OK
DTCServiceStartMode : Manual
DTCServiceAccount : NT AUTHORITY\NetworkService
DTCCID_MSDTC : b2aefacb-85d1-46b8-8047-7bb88e08d38a
DTCCID_MSDTCUIS : 205bd32c-b022-4d0c-aa3e-2e5dc65c6d35
DTCCID_MSDTCTIPGW : 3e743aa0-ead6-4569-ba7b-fe1aaea0a1eb
DTCCID_MSDTCXATM : 667fc4b8-c2f5-4c3f-ad75-728b113f36c5
networkDTCAccess : 0
networkDTCAccessAdmin : 0
networkDTCAccessClients : 0
networkDTCAccessInbound : 0
networkDTCAccessOutBound : 0
networkDTCAccessTip : 0
networkDTCAccessTransactions : 0
XATransactions : 0
 
 
.EXAMPLE
$Computers = (Get-Content D:\configfiles\SQL\MySQLInstances.txt | % {$_.split('\')[0]})
$Computers | Get-DbaMsdtc
 
Get DTC status for all the computers in a .txt file
 
 
.EXAMPLE
Get-DbaMsdtc -Computername $Computers | where { $_.dtcservicestate -ne 'running' }
 
Get DTC status for all the computers where the MSDTC Service is not running
 
 
.EXAMPLE
Get-DbaMsdtc -ComputerName srv0042 | Out-Gridview
 
 
Get DTC status for the computer srv0042 and show in a grid view
 
 
 
#>


    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline = $true)]
        [Alias('cn', 'host', 'Server')]
        [string[]]$Computername = $env:COMPUTERNAME
    )

    BEGIN {
        $functionName = (Get-PSCallstack)[0].Command
        $ComputerName = $ComputerName | ForEach-Object {$_.split("\")[0]} | Select-Object -Unique
        $query = "Select * FROM Win32_Service WHERE Name = 'MSDTC'"
        $DTCSecurity = {
            Get-ItemProperty -Path HKLM:\Software\Microsoft\MSDTC\Security |
                Select-Object PSPath, PSComputerName, AccountName, networkDTCAccess,
            networkDTCAccessAdmin, networkDTCAccessClients, networkDTCAccessInbound,
            networkDTCAccessOutBound, networkDTCAccessTip, networkDTCAccessTransactions, XATransactions
        }
        $DTCCIDs = {
            New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT | Out-Null
            Get-ItemProperty -Path HKCR:\CID\*\Description |
                Select-Object @{ l = 'Data'; e = { $_.'(default)' } }, @{ l = 'CID'; e = { $_.PSParentPath.split('\')[-1] } }
            Remove-PSDrive -Name HKCR | Out-Null
        }
    }
    PROCESS {
        ForEach ($Computer in $Computername) {
            $reg = $CIDs = $null
            $CIDHash = @{}
            if ( Test-PSRemoting -ComputerName $Computer ) {
                $dtcservice = $null
                Write-Verbose "$functionName - Getting DTC on $computer via WSMan"
                $dtcservice = Get-Ciminstance -ComputerName $Computer -Query $query
                if ( $null -eq $dtcservice ) {
                    Write-Warning "$functionName - Can't connect to CIM on $Computer via WSMan"
                }

                Write-Verbose "$functionName - Getting MSDTC Security Registry Values on $Computer"
                $reg = Invoke-Command -ComputerName $Computer -ScriptBlock $DTCSecurity
                if ( $null -eq $reg ) {
                    Write-Warning "$functionName - Can't connect to MSDTC Security registry on $Computer"
                }
                Write-Verbose "$functionName - Getting MSDTC CID Registry Values on $Computer"
                $CIDs = Invoke-Command -ComputerName $Computer -ScriptBlock $DTCCIDs
                if ( $null -ne $CIDs ) {
                    foreach ($key in $CIDs) { $CIDHash.Add($key.Data, $key.CID) }
                }
                else {
                    Write-Warning "$functionName - Can't connect to MSDTC CID registry on $Computer"
                }
            }
            else {
                Write-Verbose "$functionName - PSRemoting is not enabled on $Computer"
                try {
                    Write-Verbose "$functionName - Failed To get DTC via WinRM. Getting DTC on $computer via DCom"
                    $SessionParams = @{ }
                    $SessionParams.ComputerName = $Computer
                    $SessionParams.SessionOption = (New-CimSessionOption -Protocol Dcom)
                    $Session = New-CimSession @SessionParams
                    $dtcservice = Get-Ciminstance -CimSession $Session -Query $query
                }
                catch {
                    Write-Warning "$functionName - Can't connect to CIM on $Computer via DCom"
                    continue
                }
            }
            if ( $dtcservice ) {
                [PSCustomObject]@{
                    ComputerName                 = $dtcservice.PSComputerName
                    DTCServiceName               = $dtcservice.DisplayName
                    DTCServiceState              = $dtcservice.State
                    DTCServiceStatus             = $dtcservice.Status
                    DTCServiceStartMode          = $dtcservice.StartMode
                    DTCServiceAccount            = $dtcservice.StartName
                    DTCCID_MSDTC                 = $CIDHash['MSDTC']
                    DTCCID_MSDTCUIS              = $CIDHash['MSDTCUIS']
                    DTCCID_MSDTCTIPGW            = $CIDHash['MSDTCTIPGW']
                    DTCCID_MSDTCXATM             = $CIDHash['MSDTCXATM']
                    networkDTCAccess             = $reg.networkDTCAccess
                    networkDTCAccessAdmin        = $reg.networkDTCAccessAdmin
                    networkDTCAccessClients      = $reg.networkDTCAccessClients
                    networkDTCAccessInbound      = $reg.networkDTCAccessInbound
                    networkDTCAccessOutBound     = $reg.networkDTCAccessOutBound
                    networkDTCAccessTip          = $reg.networkDTCAccessTip
                    networkDTCAccessTransactions = $reg.networkDTCAccessTransactions
                    XATransactions               = $reg.XATransactions
                }
            }
        }
    }
}