Book Image

PowerShell for SQL Server Essentials

By : Donabel Santos
Book Image

PowerShell for SQL Server Essentials

By: Donabel Santos

Overview of this book

Table of Contents (15 chapters)
PowerShell for SQL Server Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Implementing Reusability with Functions and Modules
Index

Current instance configuration


PowerShell makes it easy to query a SQL Server instance and export current configurations. We can make use of the SMO server object and query all the properties. The following is an example script that performs this export:

#current server name
$servername = "ROGUE"

$server = New-Object "Microsoft.SqlServer.Management.Smo.Server" $servername

$server |
Get-Member |
Where-Object Name -ne "SystemMessages" |
Where-Object MemberType -eq "Property" |
Select-Object Name,
       @{Name="Value";Expression={$server.($_.Name)}} |
Format-Table -AutoSize

Once the script runs, you should find all the instance properties and corresponding values displayed on your screen:

Let's walk through the script. The preceding sample script creates an SMO server object based on the SQL Server instance named ROGUE:

#current server name
$servername = "ROGUE" #or localhost

$server = New-Object "Microsoft.SqlServer.Management.Smo.Server" $servername

Once the SMO server object is created...