-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0.CheatSheet-SqlServerSmo.ps1
117 lines (100 loc) · 3.12 KB
/
0.CheatSheet-SqlServerSmo.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
# Script Name:
# SQL Server SMO Cheatsheet (0.CheatSheet-SqlServerSmo.ps1)
# Author:
# Scott Sutherland (@_nullbind), 2015 NetSPI
# Description:
# This file contains basic examples that show how to query SQL Server
# for configuration information using the SQL Server SDK SMO APIs.
# Requirements:
# The examples in this cheatsheet require two SMO libraries that get installed with SQL Server.
# The file names have been listed below:
# - Microsoft.SqlServer.Smo.dll
# - Microsoft.SqlServer.SmoExtended.dll
# References:
# https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.server.aspx
# Import SMO Libs - required for all examples below
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended")| Out-Null
# Authenticate - Integrated Windows Auth - works
$srv = new-object ('Microsoft.SqlServer.Management.Smo.Server') "server\instance"
# Get instance option
[System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources()
# Authenticate - SQL Server authentication - mixed mode - works
$srv = new-object ('Microsoft.SqlServer.Management.Smo.Server') "10.1.1.1"
$srv.ConnectionContext.LoginSecure=$false;
$srv.ConnectionContext.set_Login("user");
$srv.ConnectionContext.set_Password("password")
$srv.Information
# Get version / server information
$srv.Information
$srv.Name
$srv.NetName
$srv.ComputerNamePhysicalNetBIOS
$srv.Version
$srv.VersionMajor
$srv.VersionMinor
$srv.Edition
$srv.EngineEdition
$srv.OSVersion
$srv.DomainInstanceName
$srv.DomainName
$srv.SqlDomainGroup
# Get service informaiton
$srv.ServiceName
$srv.ServiceAccount
$srv.ServiceStartMode
$srv.BrowserServiceAccount
# Get state information
$srv.State
$srv.Status
# Get listener information
$srv.NamedPipesEnabled
$srv.TcpEnabled
# Get directory path information
$srv.RootDirectory
$srv.InstallDataDirectory
$srv.InstallSharedDirectory
$srv.ErrorLogPath
$srv.MasterDBLogPath
$srv.MasterDBPath
$srv.BackupDirectory
# Logins, roles, and privilege information
$srv.ConnectionContext
$srv.LoginMode
$srv.Logins
$srv.Roles
$srv.EnumServerPermissions()
# Window accounts / groups assigned logins in SQL Server
$srv.EnumWindowsUserInfo()
$srv.EnumWindowsUserInfo() | select "account name"
$srv.EnumWindowsDomainGroups()
$srv.EnumWindowsGroupInfo("Domain Admins")
# Credentials / proxy_account
$srv.Credentials
$srv.ProxyAccount
# Databse information
$srv.Databases
# cluster / mirror information
$srv.IsClustered
$srv.ClusterName
$srv.EnumClusterMembersState
$srv.EnumClusterSubnets
$srv.EnumDatabaseMirrorWitnessRoles()
# SQL Server settings
$srv.Configuration
$srv.Settings
$srv.Properties
$srv.Mail
$srv.MailProfile
$srv.Triggers
$srv.AuditLevel
$srv.Audits
$srv.LinkedServers
$srv.Endpoints
$srv.JobServer
$srv.EnumServerAttributes()
# SQL Server enumeration
# https://msdn.microsoft.com/en-us/library/ms210366.aspx
$srv.PingSqlServerVersion("server\Standard")
$srv.PingSqlServerVersion("1.1.1.1",'sa','password')
$SQLSvr = [Microsoft.SqlServer.Management.Smo.SmoApplication]::EnumAvailableSqlServers($true); $SQLSvr | Out-GridView