RetiredDBA.com

All things for Microsoft SQL Server

Dbas if you are up late at night ….. again…. check this out!

Aug 16, 2018

DBA’s take a look at these FREE technology tutorials from the MidnightDBA on youtube

I recently watched this MidnightDBA tutorial on youtube: Beginning Powershell for DBAs This tutorial runs 1 hr and 23 minutes by Sean McCown – SQL Server MVP.

If for no other other reason, listen for some interesting jokes! But be warned, the jokes are much funnier late at night!

According to Sean, “The time has come where DBAs can no longer afford to ignore PowerShell. Not only has it become an integral part of SQL Server, but we’re being asked to do things on a much grander scale than ever before; things that can only be done with PowerShell.’.

Some interesting points Sean discusses in this session

  • PowerShell is a great scripting language.
  • Command prompt commands work in PowerShell.
  • PowerShell scales well but watch out for doing the wrong thing on multiple servers!
  • Many SQL Server tool vendors are adding PowerShell providers.
  • Powershell cmdlets are like stored procs or functions.
  • Get the Microsoft download to install PowerShell: PowerShell Documentation.

Skip to 18 minutes into video for PowerShell commands that are discussed
get-service – lists all services
get-service | where-object{$_.displayname -match “sql”} – lists just SQL services
get-service | ?{$_.displayname -match “sql”} – lists just SQL services using an alias for where-object
cls -clear screen
psdrive – lists providers and root of live objects and everything is considered a drive
cd \sqlserver
cd \sql
cd localhost\default
dir | format-table — lists all info
dir | ft — lists all info alias
dir | format-table Name — only list name column
get-member — you will use this all the time
dir | gm name, schema (alias for get-member) — lists properties
dir | ft name, rowcount, dataspaceused -auto — list tales by rowcount
dir | ?{$_.Schema -eq “person “} | ft name, rowcount, dataspaceused -auto — list tables by rowcount for Schema Person
$a = dir | ?{$_.Schema -eq “person “} | ft name, rowcount, dataspaceused -auto — set variable a to the command
$a – same list as using the entire command
get-help dir – help on dir command
get-service | gm
get-service | x{$_.Stop{} } — will stop all Services on the box —-WATCH OUT !!!!!!!!!!!!!!!!
get-service | ?{$_.Displayname -match “sql” | ?{$ x{$_.Stop{} } — will stop all SQL Services on the box
dir | ft Name
dir | x{$_.Script{}} — generates SQL code for objects
dir | x{$_.Script{} | out-file c:\zztables.txt -a }
dir | ft Processors, ProductLevel -auto
invoke-sqlcmd -database adventureworks -query”select * from person.address” -ft —starts sql command line
$a = invoke-sqlcmd -database adventureworks -query”select * from person.address” -ft —set to a variable
$a = get-content c:\servers.txt — reads the text file
$a — lists everything from the txt file
$a | %{cd sqlserver:sql\$_\default} — connect to server from list of servers
$a = dir
$a | %{…commands go here}

Sorry about any mistypes on the commands! Well worth your time to watch this video to get a jump start on using PowerShell with some basic commands.

As I wrote this… I saw this article in my inbox: PowerShell Core 6.1 Arriving This Month but Compatibility Will Lag. According to the article: ‘Microsoft is deprecating Windows PowerShell in favor of the cross-platform PowerShell Core, which works across various Linux platforms, in addition to Windows. Windows PowerShell development essentially ended with version 5.1 and Windows PowerShell 6.0 ‘.