-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.ps1
73 lines (61 loc) · 2.17 KB
/
utils.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
# Importable script containing utility helpful functions
function exit_script {
param (
[int][Parameter(Mandatory = $false, Position = 0)]
$code = 0,
[bool][Parameter(Mandatory = $false, Position = 1)]
$quiet = $false
)
if (-not $quiet) {
cmd /c 'pause'
}
exit $code
}
# MSSQLSERVER instance name indicates it is a Default Instance, which we can connect via localhost or dot,
# any other name indicates it is a Named Instance.
function ValidateServerNameInput {
param ([string][Parameter(Mandatory, Position = 0)] $server_name)
$mssql_reg = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server' -ErrorAction Ignore
if (-not $mssql_reg) {
MessageError "Error: MSSQL Server is not installed. Please follow the prerequisite section in the readme file."
return $false
}
$sql_instances = @($mssql_reg.InstalledInstances)
if ($server_name -in "localhost", ".") {
$server_instance_name = "MSSQLSERVER"
} else {
$server_instance_name = $server_name.Split("\")[-1]
}
if (-not ($sql_instances -contains $server_instance_name)) {
MessageError "Error: Invalid sql server name: [$($server_name)]"
$sql_instances = @($sql_instances | Where-Object { $_ -ne "MSSQLSERVER" })
if ($sql_instances) {
MessageError "Available sql named instances: [$($sql_instances -join ', ')]"
$invoker_script_name = ($MyInvocation.PSCommandPath -split "\\")[-1]
MessageError "Example: .\$invoker_script_name -server_name "".\$($sql_instances[-1])"""
}
return $false
}
return $true
}
# Simple and colorful log messages
function Message {
param ([string][Parameter(Mandatory)] $message)
Write-Host "$message" -ForegroundColor White
}
function MessageInfo {
param ([string][Parameter(Mandatory)] $message)
Write-Host "$message" -ForegroundColor Blue
}
function MessageSuccess {
param ([string][Parameter(Mandatory)] $message)
Write-Host "$message" -ForegroundColor Green
}
function MessageWarn {
param ([string][Parameter(Mandatory)] $message)
Write-Host "$message" -ForegroundColor Yellow
}
function MessageError {
param ([string][Parameter(Mandatory)] $message)
Write-Host "$message" -ForegroundColor Red
}