Microsoft SQL Server 2008 Administration With Windows PowerShell 1st Edition Ananthakumar Muthusamy Instant Download
Microsoft SQL Server 2008 Administration With Windows PowerShell 1st Edition Ananthakumar Muthusamy Instant Download
https://ebookname.com/product/microsoft-sql-
server-2008-administration-with-windows-powershell-1st-edition-
ananthakumar-muthusamy/
https://ebookname.com/product/microsoft-windows-
server-2008-r2-administration-instant-reference-1st-edition-
matthew-hester/
https://ebookname.com/product/beginning-t-sql-with-microsoft-sql-
server-2005-and-2008-paul-turley/
https://ebookname.com/product/microsoft-sql-server-2008-for-
dummies-1st-edition-chapple/
https://ebookname.com/product/historical-dictionary-of-
journalism-historical-dictionaries-of-professions-and-
industries-1st-edition-ross-eaman/
Silius Italicus Punica Rome s War with Hannibal 1st
Edition Antony Augoustakis
https://ebookname.com/product/silius-italicus-punica-rome-s-war-
with-hannibal-1st-edition-antony-augoustakis/
https://ebookname.com/product/welcome-to-the-cornish-country-
hospital-the-cornish-country-hospital-1-1st-edition-jo-bartlett/
https://ebookname.com/product/quick-reference-dictionary-of-
eyecare-terminology-4th-edition-janice-k-ledford-comt/
https://ebookname.com/product/teach-yourself-visually-iphoto-for-
ipad-1st-edition-richard-wentk/
https://ebookname.com/product/metric-standards-for-worldwide-
manufacturing-2007-ed-edition-kverneland/
Drafting a Conservation Blueprint A Practitioner s
Guide To Planning For Biodiversity 1st Edition Craig
Groves
https://ebookname.com/product/drafting-a-conservation-blueprint-
a-practitioner-s-guide-to-planning-for-biodiversity-1st-edition-
craig-groves/
Wrox Programmer to Programmer TM
Microsoft ®
Administration ™
Contact Us.
We always like to get feedback from our readers. Have a book idea?
Need community support? Let us know by e-mailing [email protected]
Professional SQL Server® 2008 Administration with
Windows PowerShell™
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Chapter 1: What Is Windows PowerShell? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 2: Windows PowerShell Command Types, Snap-ins, and cmdlets . . . 15
Chapter 3: Windows PowerShell Programming, Scripting, Error Handling,
and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Chapter 4: Windows PowerShell Functions, Parameters, Sourcing,
Scopes, and User Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Chapter 5: Working with the File System, Registry, and Variables . . . . . . . . . . . 93
Chapter 6: Working with Event Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Chapter 7: Working with Windows Services and Processes . . . . . . . . . . . . . . . . 141
Chapter 8: Working with WMI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Chapter 9: WMI Provider for Configuration Management. . . . . . . . . . . . . . . . . . . 197
Chapter 10: WMI Provider for Server Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Chapter 11: Windows PowerShell in SQL Server 2008 Environment,
SQL Server PowerShell Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Chapter 12: Managing Policies through SQLSERVER:\SQLPolicy . . . . . . . . . . 259
Chapter 13: Windows PowerShell and SMO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Chapter 14: Building SQL Server Standards and PowerShell Coding
Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Chapter 15: Building SQL Server Inventory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Chapter 16: SQL Server Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Chapter 17: Collecting SQL Server Performance and Host Performance
Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Chapter 18: Monitoring SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Chapter 19: Monitoring Disk Space Usage, Database Files, and Backups . . 451
Chapter 20: Defining Policies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Chapter 21: Generating Database Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Appendix A: cmdlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
Professional
SQL Server® 2008 Administration with
Windows PowerShell™
Professional
SQL Server® 2008 Administration with
Windows PowerShell™
MAK has been working in information technology for more than 10 years. He has worked in the technol-
ogy side of the finance, dot com, B2B, and software industries. His past clients include Fort James, Boston
Financial, PCConnection, PurchaseSoft, Merrill Lynch, and Jefferies. MAK holds an M.B.A. degree and
various certifications in the computer field.
He also teaches Microsoft SQL Server and has taught many SQL Server developers and DBAs. He has
master’s degree in Business Administration.
Yan Pan (MCITP, MCDBA, OCA) is a Senior DBA for a leading global financial services firm, where
her daily duties include administering hundreds of SQL Server and Oracle servers of every possible
version, working with business units, and troubleshooting database performance problems. She is also a
contributing columnist for DatabaseJournal.com. Her articles can be found at www.yanpansql.com.
Previously, Yan worked as a SQL Server DBA and a .NET developer at Data Based Ads, Inc., and devel-
oped two .NET Web applications. Before that, she worked at AT&T Research Labs. She designed OLAP
cubes with SQL Server Analysis Services and developed PivotTable reports for regional managers. She
has master’s degrees in Computer Science and Physics.
First of all, I would like to thank my fiancée, Claire Hsu. Without her moral support, this book would not
have been possible. I would like to thank my mom, Jayalakshmi Muthusamy, for bringing me into this
world. I still admire her for not losing her patience and always keeping her sense of humor. I would like
to thank my dad, Muthusamy Srinivasan, for being a friend and a teacher. I want to take this opportunity
to thank both my parents for all the compromises they made to raise both my sister and me. I would also
like to thank my sister, Anuradha, for supporting me and taking care of me.
I would like to thank my co-author, Yan Pan, especially for being both very patient when needed and
pushing me when necessary. She is a very good friend and colleague. She could always get me back on
track when I was distracted with other projects.
I would also like to thank all my friends, current and past colleagues, and relatives for their constant
inspiration.
I am very grateful to all the readers and fans of my online articles from various parts of the world. With-
out their support, comments, motivation, and critiques, this book wouldn’t have shaped up so well.
Yan and I are both grateful to Haidong Ji, aka Alex, for introducing us to Wrox-Wiley and being our
knowledgeable technical editor. We also want to thank Bob Elliot for bringing our book proposal to the
executive team and making it happen.
Last but not least, we would like to thank Rosanne Koneval and Daniel Scribner. Your questioning at the
right place, proper decision-making, and quick turnaround made this book much neater and clearer.
It is hoped that I haven’t forgotten anyone, but if I have, it was an oversight. Honest.
— MAK
First and foremost, I would like to thank my beloved husband, Yue Guo. You have always supported
and encouraged me in everything I have ever done. Thank you for your incredible patience throughout
the writing of this book. I would also like to thank my parents for all they have done for me to give me
the best life they could possibly give.
I would like to express my sincere appreciation to MAK for introducing me to technical writing and
sharing the book idea with me. Your optimism, enthusiasm, and humor have made the book-writing
journey a wonderful experience.
I am also deeply indebted to Haidong (Alex) Ji. Haidong, thank you for introducing us to Wrox. Thank
you for your suggestions, guidance, and encouragement throughout the course of this book. Your tech-
nical input and attention to detail helped us improve the quality of this book greatly.
I would also like to thank Bob Elliot, Rosanne Koneval and Daniel Scribner at Wrox. Bob, thank you for
believing in us. We started this project with plenty of enthusiasm and passion for new technology, but
not a book contract. Thank you for making our dream come true and allowing our vision of a Windows
Acknowledgments
PowerShell–powered SQL Server enterprise infrastructure to reach thousands of readers, and hopefully
making their lives easier. Rosanne, thank you for your incredible patience in cleaning up the words and
polishing everything up to get this book ready for the printer. Daniel, thank you for giving a final touch
to this book.
Finally, I’d like to thank you, my readers, for reading what I have written! Without your support and
feedback, I could not have accomplished so much.
— Yan
xii
Contents
Introduction xxi
xiv
Contents
Function Scope 87
User Profiles 88
Transcripts 90
Summary 91
Chapter 5: Working with the File System, Registry, and Variables 93
Using Get-PSDrive and Get-PSProvider 93
Working with File System 95
Navigating the File System 96
Managing Files and Directories 98
Managing File Contents 106
Working with the Registry 108
Working with Variables 111
Automatic Variables 113
Preference Variables 115
User-Created Variables 116
Working with Environment Variables 121
Summary 122
Chapter 6: Working with Event Logs 123
Event Log Service 124
Event Viewer 125
Event Logs 125
Log Entry Types 128
Exporting the event logs 134
PowerShell cmdlets Related to event log 136
Summary 140
Chapter 7: Working with Windows Services and Processes 141
What Is a Windows Service? 141
Windows PowerShell and Windows Services 143
Get-Service 144
Stop-Service 146
Start-Service 148
Set-Service 150
Working with Windows Processes 151
Get-Process 153
Stop-Process 155
Summary 157
xv
Contents
Chapter 8: Working with WMI 159
Permission Issues and WMI 159
The WMI Model 161
Working with Event Log 164
Working with Services 170
Working with Processes 179
Working with Environment Variables 186
Working with the Registry 190
Summary 195
xvi
Contents
Chapter 12: Managing Policies through SQLSERVER:\SQLPolicy 259
SQLSERVER:\SQLPolicy Folder 259
Conditions 263
Policies 268
Summary 281
Chapter 13: Windows PowerShell and SMO 283
PowerShell and the SQLConnection .NET Class 283
Working with SQL Server using SMO 290
Methods and Properties 291
Getting Version Information 291
SQL Server SPID Information 292
SQL Server Server-Related Information 293
SQL Server Database-Related Information 295
Changing the Login Mode 296
Host Information 296
Performance Counters 297
Working with Databases using SMO 297
Working with Tables using SMO 304
Creating Tables 305
Listing Columns 305
Removing Columns 306
Adding Columns 307
Dropping a Table 308
Backup and Restore with SMO 308
Database Backup 308
Transaction Log Backup 311
Differential Backup 312
Restoring Full Backup 313
Restoring a Full Backup and Transaction Log Backup 313
Summary 316
xvii
Contents
Use Comments Generously 321
Select * 322
Cursors 322
Temporary Tables 322
Things to Avoid 322
Things to Use 323
Check @@ERROR 323
Use SQL Server Date Data Types 323
DML Statements 323
ANSI-Standard Join Clauses 323
Deprecated Features 324
Database Design Standard and Best Practices 324
User-Defined Tables 324
Logs 324
Split tempdb 325
Databases 325
Security and Roles 325
Auto Create and Auto Update 325
Size 325
Auto Shrink and Auto Close 325
Design and Performance 326
Store Unstructured Data 326
More Performance Guidelines 326
Data Protection Standards and Best Practices 327
Backup Policy 327
SQL Server Production Standards 327
High Availability and Disaster Recovery 327
The Administration Database 328
The Scratch Database 328
Centralized Inventory Server 328
Database File Location and RAID 328
Segregation 328
Features 328
PowerShell Coding Standard 329
Default Parameters 329
Log File 329
Log Format 329
Comments 330
Display 330
Variable Naming Convention 330
Exception Handling 330
Summary 331
xviii
Contents
Chapter 15: Building SQL Server Inventory 333
SQL Server Inventory 333
Hosts 335
Clusters 346
ClusterNodes 350
Servers 355
Databases 368
Supplementary Tables 373
Summary 376
xix
Contents
Chapter 20: Defining Policies 475
Stored Procedure Naming Convention Policy 475
Auto_Close and Auto_Shrink Off Policy 480
Summary 485
Chapter 21: Generating Database Scripts 487
Scripting Databases 487
Scripting Schemas 489
Scripting User-Defined Data Types 491
Scripting Tables 494
Scripting User Views 496
Scripting Stored Procedures 498
Scripting Functions 500
Scripting XML Schemas 502
Scripting Users 503
Summary 506
Index 531
xx
Introduction
Welcome to SQL Server 2008 Administration with Windows PowerShell. This book is a nuts-and-bolts guide
to creating Windows PowerShell scripts that can be used to administer every aspect of the SQL Server
2008 Database Engine. Using a very hands-on approach, this book guides you through the basics of
Windows PowerShell, the available Windows PowerShell components that have been integrated into
SQL Server 2008, and the actual administration tasks. By providing clear, well-structured examples, SQL
Server 2008 Administration with Windows PowerShell shows you how to construct effective and practical
solutions that can improve SQL Server administration and monitoring at your company.
SQL Server 2008 delivers a dynamic, smart, and productive data platform for all your data-related
needs. SQL Server 2008 offers secure, reliable, and consistent performance. It is also very scalable and
flexible, handling any form of data. It is well known in the technology world that SQL Server 2008 is not
only a RDBMS, but also has built-in features such as Integration Services for ETL, Analysis Services for
Business Intelligence, Reporting Services for report management, and more. This is why Microsoft SQL
Server is a cut above all other database products.
Windows PowerShell 2.0 is the new extensible command-line interface shell and scripting language that
provides an environment for interactive scripting and non-interactive scripted administration of local
and remote computers. Because Windows PowerShell integrates with the Microsoft .NET Framework,
we can take advantage of all the features of .NET as well. Windows PowerShell 2.0 helps system admin-
istrators to automate and manage various aspects of the Windows environment. PowerShell 2.0 also has
many new and useful features such as remoting, eventing, and many more. For these reasons, Windows
PowerShell 2.0 stands above all the system administration products on the market.
This book brings together the best of both worlds — namely, the database world and the administra-
tion world — to help you manage, automate, and control your environment. This book combines three
technologies — SQL Server 2008, the .NET Framework, and Windows PowerShell 2.0 — and guides SQL
Server database administrators in managing a server plant using Windows PowerShell 2.0.
We’ve provided a wide range of material in a tutorial-based book to get you over the learning curve of
Windows PowerShell 2.0 and SQL Server 2008 database administration.
If you are a Microsoft SQL Server database administrator, you know that you cannot manage and access
all the system resources from SQL Server. Because SQL Server runs on top of the Windows operating
system and relies on the health of the system, you need information from the system side as well. This
book will teach you to manage both SQL Server and system resources using Windows PowerShell 2.0.
Introduction
If you are a systems administrator and would like to know more about SQL Server database adminis-
tration, this book introduces you to the features in SQL Server 2008 and provides plenty of examples
demonstrating how to manage SQL Server 2008 using Windows PowerShell 2.0.
This book is also appropriate for those who have some exposure to systems administration and SQL
Server administration, or for those who want to expand their skill set to include administration.
It also introduces Windows Management Instrumentation (WMI) and explains how to write WMI scripts
in Windows PowerShell to perform system and SQL Server 2008 administrative tasks.
This book also covers the WMI providers for SQL Server. These useful providers enable you to manage
SQL Server services and network connectivity, and proactively monitor SQL Server events. You will also
discover the new Eventing feature in Windows PowerShell 2.0.
Additionally, the new SQL Server 2008 support for Windows PowerShell is presented. It illustrates the
SQL Server PowerShell provider and SQLSERVER: drive. You will also learn how to manage SQL Server
objects, including the new policy objects.
One more important topic this book covers is using SQL Server Management Objects (SMO) to create
databases and tables and perform backups and restores. The SMO scripts shown in this book are all
written in Windows PowerShell 2.0.
This book also explains the need for SQL Server standards; and you will learn how to build a SQL Server
inventory over an existing or new environment.
Finally, you will learn how to perform the various SQL Server administrative tasks on servers in the
inventory, such as installation, monitoring, performance data collection, policy definition, backups,
restores, database scripting, and more, using Windows PowerShell 2.0.
Chapters 2, 3, and 4 cover the various cmdlets available in Windows PowerShell, and the various pro-
gramming features such as inputs, outputs, debugging, error handling, functions, profiles, and so on.
xxii
Introduction
Chapters 5, 6, 7, and 8 cover the various systems administration features, including accessing file systems,
registry information, variables, and Event logs. It also explains the Windows Management Instrumenta-
tion (WMI) model and shows you how to use WMI to manage system resources.
Chapters 9 and 10 show you how to access the WMI providers for SQL Server. You will learn how to use
the WMI Provider for Configuration Management to manage SQL Server services and network connec-
tivity, and the WMI Provider for Server Events to manage SQL Server events. You will also discover the
new Eventing feature in Windows PowerShell 2.0.
Chapters 11 and 12 take on the Windows PowerShell support that has been integrated into SQL Server
2008, namely, the SQLSERVER: drive. These two chapters will focus on the SQL and SQLPolicy folders
separately.
Chapter 13 shows you how to write SQL Server Management Objects (SMO) programs in Windows
PowerShell 2.0. This chapter covers various tasks such as creating a database and database objects, and
backing up and restoring a database.
Chapters 14 and 15 provide you with ample examples of building a SQL Server tasks inventory over an
existing or new environment, and how to define various standards. Standards include both SQL Server
standards and Windows PowerShell standards.
The remaining chapters explain installation, data collection, monitoring, and how to create Windows
PowerShell scripts to handle a wide range of SQL Server administrative tasks for servers in the inventory.
Chapter 16 illustrates the installation of SQL Server 2008. Chapter 17 covers tasks related to collecting
SQL Server host and server performance data. Chapters 18 and 19 cover monitoring aspects of SQL
Server 2008 administration. Chapter 20 defines policies to enforce SQL Server standards, and Chapter 21
generates various scripts at both the database level and the database object level.
❑ For the server-side component, you need SQL Server 2008 Developer or Enterprise or Standard
edition.
❑ For client-side components, you need Windows PowerShell 2.0 CTP3. To install Windows
PowerShell 2.0 CTP3, the operating system of your computer needs to be either Windows XP
Service Pack 3, Windows 2003 Service Pack 2, Windows Vista Service Pack 1, or Windows Server
2008. You also need to pre-install the following components on your computer:
xxiii
Random documents with unrelated
content Scribd suggests to you:
have no objection Miss, and if you don't think as it would be a
liberty, us has all made up our minds upon having it christened
Clara, and please to say Miss if it be too high, or any way unfitty.
Father be 'most afeared that it sound too grand for the like of us,
but mother says as the Huxtables was thought brave things on, to
Coom and Parracombe a hundred years agone.
Please Miss, father heard to Coom market last week, as there's
going to be a French invasion, and they be sure to go to London
first, and he beg you to let him know as soon as ever there be one,
and he come up at once with the big ash-stick and the ivy on it as
growed in Challacombe wood, and see as they doesn't hurt you,
Miss.
Please Miss, the young chap as saved you from the great goyal
come here to ask for you, day after you was gone, and mother
believes he baint after no good, by token he would not come in nor
drink a drop of cider.
Please Miss, father say it make his heart ache every night, to
think of you all to yourself in the wicked London town, and he go
down the lane to the white gate every evening in the hope to see
you acoming, and mother say if you be a selling red and blue picturs
her hope you will send for they as father gave the hog's puddens for,
and us wont miss them at all.
And Miss Clara dear, I expect you'll be mazed to see how I
writes and spells, father say it must be in the family, and I won't
write no more till I have finished another dozen of copy books; and
oh dear how I do wish that you were come back again, but father
say to me to say no more about it for fear to make you cry, Miss. All
the little childers except the new babby who have not seen you yet,
sends their hearts' loves and duty and a hundred kisses, and father
and mother the same, and Timothy Badcock, and Tabby, and Suke,
and Beany Dawe, now he knows it.
I remain, Miss Clara dear, your thankful and loving scholar to
command,
SARAH HUXTABLE.
Signed all this here papper scrawl in the settle by the fire.
JOHN HUXTABLE his mark X
HONOR HUXTABLE hern X."
CHAPTER XVI.
END OF VOL. I.
LONDON:
R. CLAY, SON, AND TAYLOR, PRINTERS,
BREAD STREET HILL.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must,
at no additional cost, fee or expense to the user, provide a copy,
a means of exporting a copy, or a means of obtaining a copy
upon request, of the work in its original “Plain Vanilla ASCII” or
other form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.1. Project Gutenberg volunteers and employees expend
considerable effort to identify, do copyright research on,
transcribe and proofread works not protected by U.S. copyright
law in creating the Project Gutenberg™ collection. Despite these
efforts, Project Gutenberg™ electronic works, and the medium
on which they may be stored, may contain “Defects,” such as,
but not limited to, incomplete, inaccurate or corrupt data,
transcription errors, a copyright or other intellectual property
infringement, a defective or damaged disk or other medium, a
computer virus, or computer codes that damage or cannot be
read by your equipment.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com