-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceControl.ps1
57 lines (42 loc) · 2.01 KB
/
SourceControl.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
$location=(Get-Location).Path
Clear-Host
Write-Host ""
Write-Host "---------------------------------------------------------------------------------------------"
Write-Host " SimpleSQLServerSourceControl - https://github.com/austineric/SimpleSQLServerSourceControl "
Write-Host "---------------------------------------------------------------------------------------------"
Write-Host ""
Write-Host "Running..."
#retrieve list of servers and databases from ServersAndDatabases.txt
$sourcelist=@(Import-Csv -LiteralPath ".\ServersAndDatabases.txt")
#notify and exit if no servers and databases have been added
If ($sourcelist.Count -eq 0) {
Write-Host ""
Write-Host "Please add server and database names to $location\ServersAndDatabases.txt"
Write-Host ""
Write-Host "Exiting now"
Write-Host ""
Pause
Exit
}
#loop through data sources
$sourcelist | ForEach {
$servername=$_.Server
$databasename=$_.Database
#build query
$eventquery="EXECUTE SourceControlProc"
#essentially union all source control results from each data source
$data+=@(Invoke-Sqlcmd $eventquery -ServerInstance $servername -Database $databasename -MaxCharLength 100000 )
}
#check if destination directory exists and create it if it does not
[boolean]$destination=Test-Path -Path ".\DefinitionFiles"
If ($destination -eq 0) {New-Item -ItemType Directory -Path ".\DefinitionFiles"}
#remove previous files
Remove-Item ".\DefinitionFiles\*"
#display sourcecontrol results for user selection
(($data | Sort-Object -Property EventTimestamp, RowID -Descending) | Out-GridView -OutputMode Multiple -Title "Choose one or more rows") | Sort-Object -Property RowID | ForEach {
$filepath=".\DefinitionFiles\" + $_.RowID + ".txt"
$definitionquery="SELECT [Definition] FROM SourceControl WHERE RowID=" +$_.RowID
(Invoke-Sqlcmd $definitionquery -ServerInstance $_.Server -Database $_.SourceControlDatabase -MaxCharLength 100000).Definition | Out-File -FilePath $filepath
Start Notepad++ $filepath
}