-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlinst.ps1
59 lines (45 loc) · 2.14 KB
/
sqlinst.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
$null = [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Smo')
$ClusterInstances = @{}
# try to get the instance and instance server name when SQL runs in a cluster
$ClusterWMIdata = get-wmiobject -class "MSCluster_Resource" -namespace "root\mscluster" -Authentication PacketPrivacy -ErrorAction SilentlyContinue
if ($ClusterWMIdata){
$ClusterWMIdata | where {$_.type -eq "SQL Server"} | % { $ClusterInstances[$_.PrivateProperties.InstanceName]=$_.PrivateProperties.VirtualServerName}
} # end if clusterwmidata
# fetch running SQL server services on localhost
$services = Get-Service | Where-Object -FilterScript {
$_.displayname -match '^SQL Server.\(' -and $_.status -eq 'running'
}
# determine the SQL instance network name
if ($services -match '[A-Za-z0-9]')
{
$services | ForEach-Object -Process {
$servicename = $_.name
$dbs = $null
$logins = $null
$instancematch = $null
$sql_instance = $null
$InstanceName = $servicename
if ($servicename.contains("$")) {
$InstanceName = $ServiceName.split('$')[1]
}
# if service is a SQL instance then retrieve the full instance name
if ($ClusterInstances)
{
$InstanceMatch = $ClusterInstances[$InstanceName]
} # end if clusterinstances
# if the instance is running in a cluster then uses the cluster server name fetched from cluster wmi to connect
# otherwise connect to localhost instead
if ($instancematch -and $instancename -eq 'MSSQLSERVER')
{
$sql_instance = $instancematch
} elseif ( $instancematch)
{
$sql_instance = "$instancematch\$instancename"
} elseif ( $servicename.contains("$"))
{
$sql_instance = "localhost\$instancename"
} # end if networkinstances
# fetch data from sql with pass-through windows authentication
$sql_instance
} # end foreach service
} # end if services