SQL PowerShell Demo - Intro
Cindy Gross, Dedicated Support Engineer
Audience: SQL Server DBAs and/or developers who are familiar with basic SQL Server commands, SQL Server Management Studio (SSMS), and SQL Agent jobs.
0. Prerequisites
1. Install Basics
# Get the version
$host.version
2. Hints
3. Explore Basic PowerShell Functionality
# Run this command to see what your security level is:
Get-ExecutionPolicy
# For information about execution policies:
get-help about_execution_policies
# If the execution policy is restricted then change it to unrestricted, RemoteSigned, or whatever level
# is most appropriate http://technet.microsoft.com/en-us/library/ee176961.aspx
# Example of changing the execution policy:
Set-ExecutionPolicy unrestricted
# List of available commands and info about each:
get-help
# “Dir” is an alias for get-childitem so we see the help info for get-children when we pass in dir:
get-help dir
# “Ls” is also an alias for get-childitem:
get-help ls
# The “full” option includes examples, parameters, etc.:
get-help dir –full
# Returns one row per running instance of sqlservr.exe:
get-process sqlservr
# Returns one row per running instance of sqlservr.exe with different output:
get-process sqlservr -fileversioninfo
# one row per service with SQL in the name:
get-service *sql*
# system path from environment variables/set:
$env:path
# All environment variables that start with proc:
Dir env:proc*
# List of all PowerShell “drives” including basic regkey hives:
Get-psdrive
# Look at a registry key, in this case we can see which SQL instances are installed:
# Note that you can leave out HKEY_LOCAL_MACHINE since that is covered by the switch to hklm
# It appears that the HKEY_LOCAL_MACHINE part of the name is case sensitive
#32bit instances on an x64 box have keys in the Wow6432Node key
CD hklm:dir “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names”
dir “HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\Instance Names”
4. Formatting
# Look at some of the output format options
# Switch to the registry
CD hklm:
dir “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names” | format-list
dir “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names” | format-list –property name, property
dir “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names” | format-list –property *
5. Run a PowerShell Script
# Change to the directory where the PowerShell script resides
cd c:\installs\PowerShell\test
# Specify the current directory with a period (.) and call the PowerShell script
.\getsysproperties.ps1
REM Execute the script
PowerShell.exe -noexit .\getsysproperties.ps1
6. Run a SQL PowerShell cmdlet
# Look at the syntax and options for the cmdlet that lets you execute any SQL query
Get-help invoke-sqlcmd
# Connect to SQL and return the version information
invoke-sqlcmd -ServerInstance localhost -Query "select @@version"
# The output was cut off, let’s try a different format
invoke-sqlcmd -ServerInstance localhost -Query "select @@version" | format-list
# Close the sqlps window
# No instance name is specified, and it puts me in the default database for my Windows auth login
invoke-sqlcmd -Query "select @@version, db_name()"
# I can specify a specific database as a parameter
invoke-sqlcmd -Query "select @@version, db_name()" –database tempdb
# Close the “SQL Server PowerShell” window
# The database context is the database I clicked on in SSMS
invoke-sqlcmd -Query "select @@servername, db_name()"
# We can change to another db (if you do not have AdventureWorks, choose another database)
cd SQLSERVER:\sql\serenityhome\kaylee\databases\AdventureWorks
# Now look at the current database context
invoke-sqlcmd "select @@servername, db_name()"
7. Execute from a SQL Agent Job
8. SQL from Windows PowerShell
# Change to the SQL mini-shell
Cd sqlserver:\
# Load SQLPS
sqlps
# Test a SQL cmdlet
9. Run a SQL PowerShell Script
Open notepad and create c:\temp\TestSQL.ps1 (make sure .txt is not added to the end). Paste in this text:
# get a list of instances on this server
cd hklm:
dir "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\"
# see what we can admin
cd sqlserver:\
ls
# see what the machine name is
cd \sql
# issue a query
invoke-sqlcmd -ServerInstance localhost -query "SELECT top 5 name FROM sys.databases ORDER BY name"
# Load the SQL cmdlets
Sqlps
# Change to the C:\temp folder
Cd c:\temp
# Execute the PowerShell script
.\testsql.ps1
10. Cleanup
My other PowerShell blogs:
Powershell for SQL - Getting Started http://blogs.msdn.com/b/cindygross/archive/2011/09/23/powershell-for-sql-getting-started.aspx
PowerShell for SQL Server - Basics http://blogs.msdn.com/b/cindygross/archive/2011/09/23/powershell-for-sql-server-basics.aspx