-
Notifications
You must be signed in to change notification settings - Fork 63
/
RegEx.ps1
126 lines (95 loc) · 6.65 KB
/
RegEx.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
#########################################################
##############################################################################
## function to return a sql query, return the results in an array.
function SQL-Query{
param([string]$Query,
[string]$SqlServer = $DEFAULT_SQL_SERVER,
[string]$DB = $DEFAULT_SQL_DB,
[string]$RecordSeparator = "`t")
$conn_options = ("Data Source=$SqlServer; Initial Catalog=$DB;" + "Integrated Security=SSPI")
$conn = New-Object System.Data.SqlClient.SqlConnection($conn_options)
$conn.Open()
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.CommandTimeout = "300"
$sqlCmd.CommandText = $Query
$sqlCmd.Connection = $conn
$reader = $sqlCmd.ExecuteReader()
[array]$serverArray
$arrayCount = 0
while($reader.Read()){
$serverArray += ,($reader.GetValue(0), $reader.GetValue(1))
$arrayCount++
}
$serverArray
}
function SQL-NONQuery{
param([string]$Statement,
[string]$SqlServer = $DEFAULT_SQL_SERVER,
[string]$DB = $DEFAULT_SQL_DB )
$conn_options = ("Data Source=$SqlServer; Initial Catalog=$DB;" + "Integrated Security=SSPI")
$conn = New-Object System.Data.SqlClient.SqlConnection($conn_options)
$conn.Open()
$cmd = $conn.CreateCommand()
$cmd.CommandText = $Statement
$returnquery = $cmd.ExecuteNonQuery()
$returnquery
}
function parseString{
Param($string,
$regexpattern)
$regstring = ""
#set up the regular expressions for each patern that I'm trying to find
if ($regexpattern -eq $null){
$regexpattern = "(\w+)\.(\w+)\.(\w+)\.(\w+)"
}
$regex = New-Object System.Text.RegularExpressions.Regex $regexPattern
#we create an object that returnes the matched string and other info we need.
$match = $regex.Match($string)
if ($match.Success -and $match.Length -gt 0){
#the regex returns two values seperated by a space, this splits it into an array
#$text = ($match.value.ToString()).split(" ")
#because we are passing these variables by reference, need to set the value
$regstring = $match.value.ToString()
#write-host "$regstring"
}
$regstring
}
###################
##Start Script here
###################
#Parameters are as follows 1. Database name. 2. DNS 3. sproc name or sql statement 4. Regex pattern
$DB = $args[0]
$DNS = $args[1]
$sproc = $args[2]
$regex = $args[3]
$string = ""
$statusDB = "status"
$statusDNS = "status.db.prod.dexma.com"
$SQLinsert = ""
if ($DB -eq $null) {
write-host "Database name is required"
exit
}
if ($DNS -eq $null) {
write-host "Dns name is required"
exit
}
if ($sproc -eq $null) {
write-host "SQL statement is required"
exit
}
SQL-NONQuery $sproc $DNS $DB
$sql = "SELECT * FROM `#`#Result WHERE TextField LIKE '%.%.dbo.%'"
$returnQuery = SQL-Query $sql $DNS $DB
foreach ($row in $returnQuery){
if ($row -ne $null){
$string = $row[0]
$name = $row[1]
$returnstring = parsestring $string $regex
if (($returnstring -ne $null) -and ($returnstring -ne "")){
write-host "Parse: $returnstring Sproc: $name"
}
}
}
$sql = "DROP TABLE `#`#Result"
$returnQuery = SQL-NONQuery $sql $DNS $DB