PowerShell skripta za podatke o MS SQL serveru i bazi

U celu situaciju su umešani i SQL Server Management Objects (SMO) koji se mogu koristiti i iz PowerShell-a.

Koristim komandu invoke-sqlcmd kojom iz PS-a prizivam SQL komande (link).

0. Sve radim iz PowerShell-a u koji sam logovana sa nalogom domenskog administratora (tako da imam manje-više sva prava)
1. Odrediti verziju PowerShell-a sa kojom se radi na lokalnoj mašini :
PS D:\Skriptici> $PSVersionTable.PSVersion
Major Minor Build Revision
—– —– —– ——–
5 0 10586 117
2. Može se koristiti PowerShell iz MS SQL SMS-a
3. Ako je instaliran MS SQL SMS automattski imate i SMO modul za korišćenje pod običnim PS-om
4. Kako videti verziju MS SQL servera na udaljenom serveru (koji je u domenu) :
PS D:\Skriptici> invoke-sqlcmd -query “select @@version” -serverinstance “testniserver.moj.domen”
Column1
——-
Microsoft SQL Server 2014 (SP1-GDR) (KB4019091) – 12.0.4237.0 (X64) …
5. Videti imena baza na udaljenom serveru (dobar link)
PS D:\Skriptici> invoke-sqlcmd -serverinstance “mojserver.mojdomen” -query “select name from sys.databases”
name
—-
master
tempdb
model
msdb
upravljanjeprojektima
test4
test2
Ovo isto kao PS1 skriptić (dobar link) :
param(
[string]$imeservera = “mojserver.mojdomen”)
write-host “Radimo sa serverom $imeservera”
invoke-sqlcmd -serverinstance $imeservera -query “select name from sys.databases”

6. Kako videti veličine pojedinačnih baza (link) :
PS D:\Skriptici> Invoke-SQLCmd -Query “sp_databases” -Database master -ServerInstance “mojserver.mojdomen”
DATABASE_NAME DATABASE_SIZE REMARKS
————- ————- ——-
master 7552
model 5312
msdb 37632
upravljanjeprojektima 6144
test2 238464
test4 257920
Ako se na kraju ovog upita doda “| Out-GridView” dobija se nov prozor sa rezultatima upita 🙂
7. Podaci o pojedinačnoj bazi, u ovom slučaju “test2” (veličine, lokacije za fajl baze i fajl loga) :
PS D:\Skriptici> Invoke-SQLCmd -Query “SELECT file_id, name, type_desc, physical_name, size*8/1024, max_size FROM sys.database_files” -ServerInstance “mojserver.mojdomen” -database test2
file_id : 1
name : test2
type_desc : ROWS
physical_name : D:\Baza\test2.mdf
size : 69
max_size : -1
file_id : 2
name : test2_log
type_desc : LOG
physical_name : D:\Log\test2_log.ldf
size : 163
max_size : 268435456
Ovde se onda veličina dobija u (KB) zbog “*8/1024” dela kod “size” stavke.
Ili za sve baze na jednom serveru :
Invoke-SQLCmd -query “select name, KB=size*8/1024, type_desc from sys.master_files” -ServerInstance mojserver.mojdomen
name KB type_desc
—- — ———
test4 88 ROWS
test4_log 163 LOG
test2 69 ROWS
test2_log 163 LOG
8. Kako NE prikazati sistemske baze
SQL upit :
select name from sys.master_files where name != ‘master’ AND name != ‘mastlog’ AND name != ‘tempdev’ AND name != ‘templog’ AND name != ‘modeldev’ AND name != ‘modellog’ AND name != ‘MSDBData’ AND name != ‘MSDBLog’;
Ili kao PS upit sa rezultatom :
PS D:\Skriptici> Invoke-SQLCmd -query “select name from sys.master_files
>> where name != ‘master’ AND name != ‘mastlog’ AND name != ‘tempdev’ AND name != ‘templog’ AND name != ‘modeldev’ AND name != ‘modellog’ AND name != ‘MSDBData’ AND name != ‘MSDBLog’;” -serverinstance mojserver2.mojdomen2
name
—-
dbtest2012
dbDtest2012_log
8. Kako izgleda cela skripta :
param(
[string]$imeservera1 = “mojserver1.mojdomen”,
[string]$imeservera2 = “mojserver2.mojdomen”,
[string]$pauza = “———-”
)
$pauza
$imeservera1
$pauza
Invoke-SQLCmd -query “select name, size*8/1024 AS KB, type_desc from sys.master_files where name != ‘master’ AND name != ‘mastlog’ AND name != ‘tempdev’ AND name != ‘templog’ AND name != ‘modeldev’ AND name != ‘modellog’ AND name != ‘MSDBData’ AND name != ‘MSDBLog’;” -ServerInstance $imeservera1
$pauza
$imeservera2
$pauza
Invoke-SQLCmd -query “select name, size*8/1024 AS KB, type_desc from sys.master_files where name != ‘master’ AND name != ‘mastlog’ AND name != ‘tempdev’ AND name != ‘templog’ AND name != ‘modeldev’ AND name != ‘modellog’ AND name != ‘MSDBData’ AND name != ‘MSDBLog’;” -ServerInstance $imeservera2

This entry was posted in Windows and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

*