-
Notifications
You must be signed in to change notification settings - Fork 14
/
Test-RpcConnection.ps1
235 lines (170 loc) · 7.19 KB
/
Test-RpcConnection.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
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
Function Test-RPCConnection {
<#
.SYNOPSIS
This cmdlet is used to test RPC connections to a remote device
.DESCRIPTION
Test open RPC connections between the local machine executing this cmdlet and a remotely specified device
.PARAMETER ComputerName
Specify the FQDN, IP address, or hostname of a remote device to check your RPC connectivity with
.EXAMPLE
Test-RpcConnection -ComputerName dhcp.domain.com
# This example checks to see if any RPC communications can be opened betwnee localhost and dhcp.domain.com
.LINK
https://devblogs.microsoft.com/scripting/testing-rpc-ports-with-powershell-and-yes-its-as-much-fun-as-it-sounds/
http://bit.ly/scriptingguystwitter
http://bit.ly/scriptingguysfacebook
https://social.msdn.microsoft.com/profile/Joel%20Vickery,%20PFE
.NOTES
I stole this from the below author and made a few inconsequential changes
Author: Ryan Ries [MSFT]
Origianl date: 15 Feb. 2014
Requires: -Version 3
Contact: [email protected]
Modifications Made by Robert Osborne
Contact: [email protected]
INPUTS
System.String
OUTPUTS
PSCustomObject
#>
[CmdletBinding(SupportsShouldProcess=$True)]
param(
[Parameter(
Position=0,
Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$False,
HelpMessage="Define the hostname, FQDN, or IP address of the remote host to test RPC connectivity on")] # End Parameter
[String[]]$ComputerName = 'localhost')
BEGIN {
$Output = @()
$Source = @'
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class Rpc
{
[DllImport("Rpcrt4.dll", CharSet = CharSet.Auto)]
public static extern int RpcBindingFromStringBinding(string StringBinding, out IntPtr Binding);
[DllImport("Rpcrt4.dll")]
public static extern int RpcBindingFree(ref IntPtr Binding);
[DllImport("Rpcrt4.dll", CharSet = CharSet.Auto)]
public static extern int RpcMgmtEpEltInqBegin(IntPtr EpBinding,
int InquiryType, // 0x00000000 = RPC_C_EP_ALL_ELTS
int IfId,
int VersOption,
string ObjectUuid,
out IntPtr InquiryContext);
[DllImport("Rpcrt4.dll", CharSet = CharSet.Auto)]
public static extern int RpcMgmtEpEltInqNext(IntPtr InquiryContext,
out RPC_IF_ID IfId,
out IntPtr Binding,
out Guid ObjectUuid,
out IntPtr Annotation);
[DllImport("Rpcrt4.dll", CharSet = CharSet.Auto)]
public static extern int RpcBindingToStringBinding(IntPtr Binding, out IntPtr StringBinding);
public struct RPC_IF_ID
{
public Guid Uuid;
public ushort VersMajor;
public ushort VersMinor;
}
// Returns a dictionary of <Uuid, port>
public static Dictionary<int, string> QueryEPM(string host)
{
Dictionary<int, string> ports_and_uuids = new Dictionary<int, string>();
int retCode = 0; // RPC_S_OK
IntPtr bindingHandle = IntPtr.Zero;
IntPtr inquiryContext = IntPtr.Zero;
IntPtr elementBindingHandle = IntPtr.Zero;
RPC_IF_ID elementIfId;
Guid elementUuid;
IntPtr elementAnnotation;
try
{
retCode = RpcBindingFromStringBinding("ncacn_ip_tcp:" + host, out bindingHandle);
if (retCode != 0)
throw new Exception("RpcBindingFromStringBinding: " + retCode);
retCode = RpcMgmtEpEltInqBegin(bindingHandle, 0, 0, 0, string.Empty, out inquiryContext);
if (retCode != 0)
throw new Exception("RpcMgmtEpEltInqBegin: " + retCode);
do
{
IntPtr bindString = IntPtr.Zero;
retCode = RpcMgmtEpEltInqNext (inquiryContext, out elementIfId, out elementBindingHandle, out elementUuid, out elementAnnotation);
if (retCode != 0)
if (retCode == 1772)
break;
retCode = RpcBindingToStringBinding(elementBindingHandle, out bindString);
if (retCode != 0)
throw new Exception("RpcBindingToStringBinding: " + retCode);
string s = Marshal.PtrToStringAuto(bindString).Trim().ToLower();
if(s.StartsWith("ncacn_ip_tcp:"))
if (ports_and_uuids.ContainsKey(int.Parse(s.Split('[')[1].Split(']')[0])) == false) ports_and_uuids.Add(int.Parse(s.Split('[')[1].Split(']')[0]), elementIfId.Uuid.ToString());
RpcBindingFree(ref elementBindingHandle);
}
while (retCode != 1772); // RPC_X_NO_MORE_ENTRIES
}
catch(Exception ex)
{
Console.WriteLine(ex);
return ports_and_uuids;
}
finally
{
RpcBindingFree(ref bindingHandle);
}
return ports_and_uuids;
}
}
'@
} PROCESS {
ForEach ($C in $ComputerName) {
$EPMOpen = $False
$Socket = New-Object -TypeName System.Net.Sockets.TcpClient
Try {
$Socket.Connect($C, 135)
If ($Socket.Connected) {
$EPMOpen = $True
} # End If
$Socket.Close()
} Catch {
$Output += New-Object -TypeName PSCustomObject -Property @{
ComputerName=$C;
Port=135;
Status="Unreachable"
} # End New-Object -Property
$Socket.Dispose()
} # End Try Catch
If ($EPMOpen) {
Add-Type -TypeDefinition $Source
$RpcPortsUuids = [Rpc]::QueryEPM($C)
$PortDeDup = ($RpcPortsUuids.Keys) | Sort-Object -Unique
Foreach ($Port In $PortDeDup) {
$Socket = New-Object -TypeName System.Net.Sockets.TcpClient
Try {
$Socket.Connect($C, $Port)
If ($Socket.Connected) {
$Output += New-Object -TypeName PSCustomObject -Property @{
ComputerName=$C;
Port=135;
Status="Open"
} # End New-Object -Property
} # End If
$Socket.Close()
} Catch {
$Output += New-Object -TypeName PSCustomObject -Property @{
ComputerName=$C;
Port=135;
Status="Unreachable"
} # End New-Object -Property
$Socket.Dispose()
} # End Try Catch
} # End ForEach
} # End If
} # End ForEach
} END {
Write-Verbose -Message "Completed RPC connection testing"
Return $Output
} # End BPE
} # End Function Test-RPCConnection