-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path2.1-sqlcmd_SQLPS.ps1
More file actions
134 lines (99 loc) · 3.55 KB
/
2.1-sqlcmd_SQLPS.ps1
File metadata and controls
134 lines (99 loc) · 3.55 KB
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
127
128
129
130
131
132
133
134
#Using SQL CMD
#sqlcmd - Just call within the script
sqlcmd -S TARKIN -d tempdb -Q "select count(1) from sys.objects"
$sql="
SET NOCOUNT ON
select sp.name,count(1) db_count
from sys.server_principals sp
join sys.databases d on (sp.sid = d.owner_sid)
group by sp.name
"
sqlcmd -S TARKIN -d tempdb -Q $sql
#Multi-instance execution
Clear-Host
$instances = @('TARKIN','VADER')
$instances | ForEach-Object {"Instance:$_";sqlcmd -S $_ -Q $sql;"`n"}
#Let's look at SQLPS/SQLServer
#Where does the module live?
Get-Module -ListAvailable *SQL*
#Lets look in that location and check out some of the files.
dir 'C:\Program Files\WindowsPowerShell\Modules\SqlServer\21.0.17152'
powershell_ise 'C:\Program Files\WindowsPowerShell\Modules\SqlServer\SqlServer.PS1'
powershell_ise 'C:\Program Files\WindowsPowerShell\Modules\SqlServer\SqlServerPostScript.PS1'
#Cool, now load the module
Import-Module SqlServer
#If you're not using SSMS 2016, you'll probably see an error
Import-Module SqlServer -Verbose
Import-Module SqlServer -DisableNameChecking
#Providers and the SQL Provider
#--------------------------------------
Clear-Host
Get-PSDrive
Get-PSDrive C | Get-Member
Get-PSDrive ENV | Get-Member
cd ENV:\ #Same as 'Set-Location ENV:\'
dir
dir HKLM:\SOFTWARE
#we can easily refer to provider elements in some cases
$env:COMPUTERNAME
$env:UserName
$env:PATH
#Change to the SQL Server Provider
CD SQLSERVER:\
dir
#We can browse our SQL Servers as if they were directories
Clear-Host
CD SQL\TARKIN\
dir
CD DEFAULT
dir
dir Databases
dir databases -Force
$dbout = dir databases -Force
$dbout | gm
$dbout | Where-Object {$_.Owner -ne 'sa'}
dir databases -Force|
select name,createdate,@{name='DataSizeMB';expression={$_.dataspaceusage/1024}} | Format-Table -AutoSize
#How does this show up in SQL Server?
#Let's go look at an XE session (Go into SSMS)
#let's work with logins
$dblogins = dir logins
$dblogins
$dblogins | gm
dir logins -Force| Select-Object name,defaultdatabase
#set all default dbs for non-system logins to tempdb
foreach($dblogin in $dblogins){
if($dblogin.issystemobject -eq $false){
$dblogin.defaultdatabase = 'tempdb'
$dblogin.Alter()
}
}
dir logins -Force| Select-Object name,defaultdatabase
#We'll set them back now
foreach($dblogin in $dblogins){
if($dblogin.issystemobject -eq $false){
$dblogin.defaultdatabase = 'master'
$dblogin.Alter()
}
}
dir logins -Force| Select-Object name,defaultdatabase
#Some of the generic functions won't work
New-Item database\poshtest
#So we will need to use traditional methods
Invoke-Sqlcmd -ServerInstance TARKIN -Database tempdb -Query "CREATE DATABASE poshtest"
dir databases
#But other things do work
Remove-Item databases\poshtest
dir databases
#Let's look at the CMS
CD "SQLSERVER:\SQLRegistration\Central Management Server Group\TARKIN"
dir
#With the right approach, we can query across servers.
$servers= @((dir "SQLSERVER:\SQLRegistration\Central Management Server Group\TARKIN").Name)
$servers += 'TARKIN'
#Check your SQL Server versions
$servers | ForEach-Object {Get-Item “SQLSERVER:\SQL\$_\DEFAULT”} | Select-Object Name,VersionString,IsClustered,IsHADREnabled
#Report on all your databases
$servers | ForEach-Object {dir SQLSERVER:\SQL\$_\DEFAULT\DATABASES} |
Select-Object @{n='Server';e={$_.Parent.Name}},name,createdate,@{name='DataSizeMB';expression={$_.dataspaceusage/1024}},LastBackupDate |
Format-Table -AutoSize