Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

What Is SQLPSX?

$
0
0

I was and still am a big fan of SQLPSX (SQL PowerShell Extensions). Its is a complete PowerShell library for SQL Server written by Chad Miller and Mike Shepard, and I was one of the developers on some of the modules. It’s composed of 13 modules with 163 advanced functions, two cmdlets, and seven scripts for working with ADO.NET, SMO (SQL Server Management Objects), SQL Server Agent, RMO (Replication Management Objects), SSIS (SQL Server Integration Services), SQL script files, PBM (Policy Based Management), Oracle, and mysql. It uses the PowerShell ISE as a SQL and Oracle query tool. Also, optional backend databases and SQL Server Reporting Services 2008 reports are provided with the SQL Server and PBM modules. It works with any version of SQL Server, 2005 or later.

The idea behind SQLPSX is “Give a man a fish, and he eats for a day. Teach a man how to fish, he eats for a lifetime.” There are plenty of great libraries that, for example, give the fish to you. If you want a fish baked instead of fried, you will be in trouble, because the function only returns fried or with a lot of parameters to expose this misleading flexibility. SQLPSX has the flexibility to do whatever you want because you choose the output. SQLPSX doesn’t try to be an end-to-end solution for common problems. It is a toolbox you can use to assemble your own solutions.

Installing SQLPSX

Mike Shepard has made the SQLPSX available on GitHub, and you can just download it and add it to your profile. I like to put it in $PsHome, or all users all hosts: c:\windows\system32\windowspowershell\v1.0\Modules\

Unzip the file downloaded from GitHub into a temp directory. Open PowerShell and navigate to the Modules folder of the unzipped folder. cd C:\temp\SQLPSX-master\SQLPSX-master\Modules Run this command to unblock the files. get-childitem -Recurse | unblock-file Copy the contents of the Modules folder from temp into the Modules folder in the $PSHome path.

NOTE: Always be sure to fully review any PowerShell modules or scripts before executing them.

For a good understanding of the profiles you can have in PowerShell, review this article: Understanding the Six PowerShell Profiles .

Playing with SQLPSX

To use the module, you must import it:

Import-Module sqlpsxserver

Then you will have access to all the features of this powerful library:

get-command -Module sqlpsxserver
What Is SQLPSX?

You can see from the list that there is a wealth of commands for managing SQL Server. To get the information about a SQL Server instance run:

Get-SqlServer -sqlserver <server\instance>
What Is SQLPSX?
Gathering Information from SQL Server

Several commands allow you to gather information from the instance:

Command Description get-sqlprocess <server\instance>

Get the server process

Get-SqlVersion -sqlserver <server\instance>

Check the version

Get-SqlServer -sqlserver <server\instance> | Select-ExpandPropertyDatabases

Return a list of the databases

Get-SysDatabases -sqlserver <server\instance>

Return the system databases

Get-SqlServer -sqlserver <server\instance> | Select-ExpandPropertyDatabases | Where-Object {$_.name -eq 'Master'}| Select *

Return all the information about the Master database

Get-SqlServer -sqlserver <server\instance> | Select-ExpandPropertyDatabases | Where-Object {$_.name -eq 'Master'} | Select -ExpandProperty tables

Return a list of the tables in Master

get-SqlpsxDatabase -sqlserver <server\instance>-dbname <database> | Get-SqlTable

Check for tables

get-SqlpsxDatabase -sqlserver <server\instance>-dbname <database> | Get-SqlStoredProcedure

Check for procedures

get-SqlpsxDatabase -sqlserver <server\instance>-dbname <database> | Get-sqlview

Check for views

Get-SQLPSXDatabase <server\instance> <database> | Get-SqlTable | Get-SqlScripter

Creating the script of all the tables in the database

Get-SqlServerPermission -sqlserver <server\instance>

Check the permissions

Get-sqllogin -sqlserver <server\instance>

List the logins

Get-InvalidLogins -sqlserver <server\instance>

Check for invalid logins (from restore)

Get-SQLErrorLog -sqlserver <server\instance> -lognumber 1

Check error Log

Get-SqlServerRole <server\instance>

Return the server roles

$server = Get-SqlServer -sqlserver <server\instance> $server.Configuration.XPCmdShellEnabled.ConfigValue $server.Configuration.XPCmdShellEnabled.RunValue

Check to see if XP_CMDSHELL is enabled

$server = Get-SqlServer -sqlserver <server\instance> $server.Configuration.DefaultBackupCompression.ConfigValue $server.Configuration.DefaultBackupCompression.runvalue

Check the default backup compression setting

$server = Get-SqlServer -sqlserver <server\instance> $server.Configuration.PriorityBoost.ConfigValue $server.Configuration.PriorityBoost.runvalue

Check the priority boost setting

$server = Get-SqlServer -sqlserver <server\instance> $server.Configuration.MaxServerMemory.ConfigValue $server.Configuration.MaxServerMemory.RunValue

Check the max server memory value

$server = Get-SqlServer -sqlserver <server\instance> $server.Configuration.MaxDegreeOfParallelism.ConfigValue $server.ConfigurationMaxDegreeOfParallelism.runvalue

Check the max degree of parallelism setting

get-agentjob <server\instance> | Where-Object {$_.lastrunoutcome -eq 'Failed'}

Get a list of failed SQL Agent jobs

You can control the output of each command by using the format-table or format-list cmdlets, for instance:

get-agentjob <server\instance> | Where-Object {$_.lastrunoutcome -eq 'Failed'} |Format-list get-agentjob <server\instance> | Where-Object {$_.lastrunoutcome -eq 'Failed'} | SELECT Parent, Name, LastRunDate | Format-table

Viewing all articles
Browse latest Browse all 3160

Trending Articles