-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathServerNameps.ps1
295 lines (278 loc) · 9.22 KB
/
ServerNameps.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
function QuerySQLListner{
[cmdletbinding(
DefaultParameterSetName = '',
ConfirmImpact = "low"
)]
Param(
[Parameter(
Mandatory = $True,
Position = 0,
ParameterSetName = '',
ValueFromPipeline = $True)]
[string]$Computer
)
Begin {
$ErrorActionPreference = "SilentlyContinue"
$Port = 1434
$ConnectionTimeout = 1000
$Responses = @();
}
Process {
$UDPClient = new-Object system.Net.Sockets.Udpclient
$UDPClient.client.ReceiveTimeout = $ConnectionTimeout
$IPAddress = [System.Net.Dns]::GetHostEntry($Computer).AddressList[0].IPAddressToString
$UDPClient.Connect($IPAddress,$Port)
$ToASCII = new-object system.text.asciiencoding
$UDPPacket = 0x03,0x00,0x00
Try {
$UDPEndpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0)
$UDPClient.Client.Blocking = $True
[void]$UDPClient.Send($UDPPacket,3)#$UDPPacket.length
$BytesRecived = $UDPClient.Receive([ref]$UDPEndpoint)
[string]$Response = $ToASCII.GetString($BytesRecived)
$res = ""
If ($Response) {
$Response = $Response.Substring(3,$Response.Length-3).Replace(";;","~")
#$i = 0;
$Response.Split("~") | ForEach {
$Responses += $_
}
$socket = $null;
$UDPClient.close()
}
}
Catch {
$Error[0].ToString()
if(!(Test-Connection -Cn $Computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{"Problem still exists in connecting to $s"}
$UDPClient.Close()
}
}
End {
return ,$Responses
}
}
try {
Import-Module FailoverClusters;
$isfcavail = "TRUE";
}
catch [Exeption] {
$isfcavail = "FALSE";
}
#---------------------------------------------------------------
# Determine if a sql command is given or the path to a file.
#
$table = "";
$sqlFile = (Resolve-Path .\Management.GatherServernameps.sql).Path
$server = "CATAUSLR9PV8P2";
$rserver = "CATAUSLR9PV8P2";
$database = "master";
$rdatabase = "Management";
$loginfailed = 0;
$pingfailed = 0;
$command = $sqlFile.Trim();
if (Test-Path $command -pathType leaf) {
#---------------------------------------------------------------
# Since a file path was given, pull the content as the sql command.
#
$command = Get-Content $command;
}
#---------------------------------------------------------------
# Create a connection object, if this cannot be create we must fail
#
$conn = New-Object System.Data.SqlClient.SqlConnection;
$rconn = New-Object System.Data.SqlClient.SqlConnection;
if (!$conn) {
Throw "SqlConnection could not be created!";
return;
}
$i = 0;
$File = (Resolve-Path .\NetworkMachines.txt).Path
$serverList = @();
Get-Content $File | foreach-Object {
$i = $i + 1;
$server = $_;
$pingfailed = 0;
$loginfailed = 0;
Write-Host "Count :"$i;
Write-Host "Server :"$server
if(!(Test-Connection -Cn $server -BufferSize 16 -Count 1 -ea 0 -quiet))
{
$pingfailed = 1;
}
$serverList += $server;
try{
QuerySQLListner $server | foreach-Object{
foreach($str in $_) {
$str = $str.Replace("ServerName;","").Replace("InstanceName;","")
if($str.Trim().Length -gt 0)
{ $str
$svr = $str.Substring(0,$str.IndexOf(";"))
$inst = $str.ToString().Substring($str.IndexOf(";")+1,$str.IndexOf(";",$str.IndexOf(";",$str.IndexOf(";")+1))-$str.IndexOf(";")-1)
if(!$inst.Contains("MSSQLSERVER"))
{
$svr = $svr+"\"+$inst
}
$serverList += $svr
}
}
}
}
catch
{}
if ($isfcavail) {
try {
Get-ClusterResource -Cluster $server -ea "0" | ForEach-Object {
if ($_.Name -match "SQL Network Name") {
Write-Host "cluster :" $_.Name.Replace("SQL Network Name (","").Replace(")","")
$serverList += $_.Name.Replace("SQL Network Name (","").Replace(")","").Trim()
}
}
}
catch {
Write-Host "not a cluster"
}
}
try {
Get-Service MSSQLServer -ComputerName $server -ea "0" | ForEach-Object {
if ($_.Status -eq "Running") {
Write-Host "default instance :" $server
}
}
}
catch {
if(!(Test-Connection -Cn $server -BufferSize 16 -Count 1 -ea 0 -quiet)) {
$pingfailed = 1;
}
}
try {
Get-Service MSSQL$* -ComputerName $server -ea "0" | ForEach-Object {
if ($_.Status -eq "Running") {
Write-Host "named instance :"$_.Name.Replace("MSSQL",$server).Replace("$", "\")
$serverList += $_.Name.Replace("MSSQL",$server).Replace("$", "\").Trim()
Write-Host "named instance added"
}
}
}
catch {
if(!(Test-Connection -Cn $server -BufferSize 16 -Count 1 -ea 0 -quiet))
{
$pingfailed = 1;
}
}
}
$serverList.Count;
$serverList = $serverList | select -uniq
$serverList.Count;
foreach ($s in $serverList){
$pingfailed = 0;
$loginfailed = 0;
$server = $s;
Write-Host "Test Server :"$server
$connString = "Server = $($server);";
#---------------------------------------------------------------
# Include the database in our connection string if one is given.
#
if ($database)
{
$connString = $connString + " Database = $($database);";
}
#---------------------------------------------------------------
# Base security on the existence of a username/password
#
if ($userName -and $password)
{
$connString = $connString + " User Id = $($userName); Password = $($password)";
}
else
{
$connString = $connString + " Integrated Security = True";
}
#---------------------------------------------------------------
# Default the repository server to local if one was not provided
#
if (!$rserver)
{
$rserver = "(local)";
}
$rconnString = "Server = $($rserver);";
#---------------------------------------------------------------
# Include the repository database in our connection string if one is given.
#
if ($rdatabase)
{
$rconnString = $rconnString + " Database = $($rdatabase);";
}
#---------------------------------------------------------------
# repository connection string trusted auth only!
$rconnString = $rconnString + " Integrated Security = True";
#---------------------------------------------------------------
# Now that we have built our connection string, attempt the connection.
#
$conn.ConnectionString = $connString;
$rconn.ConnectionString = $rconnString;
$error.clear();
try {
$conn.Open();
}
catch [System.Management.Automation.MethodInvocationException]
{
if($error[0].Exception -match "Login failed"){
$loginfailed = 1
}
}
$rconn.Open();
if (($conn.State -eq 1) -and ($rconn.State -eq 1))
{
$cmd = New-Object System.Data.SqlClient.SqlCommand $command, $conn;
if ($cmd)
{
$data = New-Object System.Data.SqlClient.SqlDataAdapter;
if ($data)
{
$ds = New-Object System.Data.DataSet;
if ($ds)
{
$data.SelectCommand = $cmd;
$data.Fill($ds) | Out-Null;
foreach ($table in $ds.Tables)
{
foreach ($row in $table)
{
$rcmd = New-Object System.Data.SqlClient.SqlCommand;
$rcmd.connection = $rconn;
$rcmd.commandtext = "INSERT INTO "+$rdatabase+".dbo.ServerList (QueriedServer,ReturnedServer,Version,PingFailed,LoginFailed) VALUES('"+$server+"','"+$row.ItemArray.GetValue(0)+"','"+$row.ItemArray.GetValue(1)+"',"+$pingfailed+","+$loginfailed+")"
$rcmd.executenonquery() | Out-Null;
}
}
$ds.Dispose();
}
else
{
Write-Host "Failed creating the data set object!";
}
$data.Dispose();
}
else
{
Write-Host "Failed creating the data adapter object!";
}
$cmd.Dispose();
}
else
{
Write-Host "Failed creating the command object!";
}
$conn.Close();
$rconn.close();
}
else
{
$rcmd = New-Object System.Data.SqlClient.SqlCommand;
$rcmd.connection = $rconn;
$rcmd.commandtext = "INSERT INTO "+$rdatabase+".dbo.ServerList (QueriedServer,ReturnedServer,Version,PingFailed,LoginFailed) VALUES('"+$server+"','','',"+$pingfailed+","+$loginfailed+")"
$rcmd.executenonquery() | Out-Null;
}
$conn.Dispose();
$rconn.Dispose();
}