SlideShare a Scribd company logo
1 of 51
WELCOME TO POWERSHELL
FOR DATABASE DEVELOPERS
Bryan Cafferky
Business Intelligence Consultant
BPC Global Solutions LLC
bryan256@msn.com
www.sql-fy.com
http://www.meetup.com/The-RI-Microsoft-BIUG/
GOALS OF THIS PRESENTATION
PowerShell for Database Developers 2
• Amaze you with the capabilities of PowerShell.
• Convince you that you need PowerShell.
• Provide a basic understanding of PowerShell programming.
• Point you to where you can get support.
WHAT IS POWERSHELL?
PowerShell for Database Developers 3
PowerShell is like Linux Scripting on Steroids!
Thank you to Pragmatic Works and Robert Cain whose
presentation inspired me to prepare this presentation.
A LITTLE HISTORY
PowerShell for Database Developers 4
• Before Windows was DOS (Disk Operating System).
• DOS had no graphical interface. Every command had to be entered at a
command prompt.
• This is known as a Command Line Interface (CLI). Also known as a shell.
• A series of commands can be stored in a text file ending with a .BAT
extension. By entering the batch file name, you can run the commands
in the file. This is called a batch script.
OK, BUT WHAT IS A SHELL?
PowerShell for Database Developers 5
• Linux, the legacy of Unix, and other Command Line Orientated
Operating Systems such as VMS, TSO/CLISTS.
• Historically shell scripts are used to perform administrative tasks
like backups, moving files around or running batch jobs.
• There may be a number of different shells for the same operating
system. Linux supports a number of shells: bash (bourne again
shell, ksh (korn shell), tcsh (enhanced C shell), zsh (Z shell).
THE WINDOWS SHELL
PowerShell for Database Developers 6
• The legacy Windows Shell is the Command Prompt, a carry over
from DOS. It is not designed to work with modern Windows.
• The completely new and re-architected Windows Shell is
PowerShell.
• PowerShell was designed from the ground up to integrate with
.Net and anything in the Windows environment.
WHY DOES WINDOWS
NEEDS A SHELL?
PowerShell for Database Developers 7
• In the early days of Windows, the GUI could do it all.
• Now that Windows has grown to support large, complex
environments, a programmatic scripting tool was needed to help
administer it.
• Consider changing security permissions on five thousand users
one mouse click at time or manually backing up three hundred
SQL Servers.
CONNECTING IT ALL
PowerShell for Database Developers 8
SQL Server
Exchange
Server
SharePoint
Active
Directory
Office
File
System
PowerShell
Windows
Server
IIS
Restart-Computer
REMOTE EXECUTION
PowerShell for Database Developers 9
Dev Server
1
Prod
Server 1
Dev Server
2
Dev Server
3
Prod
Server 2
DNS
Server
PowerShell
Mary
Smith’s
Computer
John Doe’s
Computer
Invoke-Command
Enable-PSRemoting -Force
Restart-Service
Enable-PSRemoting -Force
Restart-Computer
Show-EventLog
Test-Connection
Get-EventLog
Set-Service
PowerShell – Scripting on Steroids
• Full Support of Traditional Shell features:
– Piping
– Access to Hardware environment
• Built In Integration with .NET
• Support for Variables and Objects
• Support for Lists, Arrays and Hash Tables
• Secure Model
PowerShell for Database Developers 10
PowerShell – Two Modes
• The CLI
• Integrated Scripting Environment
PowerShell for Database Developers 11
PowerShell CMDLET - Pronounced "command-let“.
• Is a single-feature command that manipulates objects in Windows
PowerShell.
• You can recognize cmdlets by their name format -- a verb and noun
separated by a dash (-), such as Get-Help, Get-Process, and Start-
Service
• Are designed to be used in combination with other cmdlets
– "get" cmdlets only retrieve data,
– "set" cmdlets only establish or change data,
– "format" cmdlets only format data,
– "out" cmdlets only direct the output to a specified destination..
• Each cmdlet has a help file that you can access by typing:
– get-help <cmdlet-name> -detailed
– Example: get-help write-host
Note: You can create your own cmdlet using PowerShell Software Developers Kit (SDK).
PowerShell for Database Developers 12
CMDLET Examples
• Get-Help *
• Get-Process
• Get-Location
• Set-Location C:
• Write-Host “Hello”
• Get-WMIObject Win32_BIOS
• Set-ExecutionPolicy RemoteSigned
PowerShell for Database Developers 13
PowerShell – Common Uses
• Use by developers to do pre and post job processing.
• Database Support (SQL Server Integration)
– Backups, Utilitities, Security
• Special database processes like database copy (SMO)
• Security Administration (Active Directory)
• File System (.NET library and CMDLETS)
• Network Administration
• Working with MS Products; SharePoint, Office, etc.
• Working with XML
PowerShell for Database Developers 14
PowerShell – Recipes
• Say it using SAPI. (scr_say_something, ufn_out_html)
• SQL Server. (scr_SQL_Server, scr_winform_sql, scr_job_runner)
• Wait for file polling. (ufn_wait_for_file)
• Maintaining Data. (scr_gridview_passthru, scr_maintain_data)
• Combining files. (ufn_combine_files)
• Fun with CSV Files. (scr_load_csv_file_to_table)
• Import/Export Excel files. (scr_SQLServer_2_Excel)
PowerShell for Database Developers 15
PowerShell – Learning Challenges
• Have to think differently than with other tools and CLIs due to object
orientation, piping, and unusual syntax conventions.
• Simple and powerful yet unintuitive.
• Most examples are targeted towards system administrators.
• Easy to do things the hard way when there is often a command-let for
the task or a way to leverage piping.
• Can find a work around such as a .bat file to avoid using PowerShell.
• With great power comes many options which adds to the learning curve
to master this tool.
PowerShell for Database Developers 16
PowerShell – Periods Matter
PowerShell will not default to the current folder.
.my_function.ps1 # Refers to a script in the current folder.
C:ScriptsTest.ps1 # Refers to a script not in the current folder.
Use Dot Sourcing to call the script and keep it in memory.
. c:scriptstest.ps1 # Run the script and keep the functions and variables
after it is done, i.e. available to the session.
. .my_function.ps1 # Run script which is in the current folder and keep the
functions and variables after it is done.
Space in the path?
& "C:My ScriptsTest.ps1“ # Run script with space in the path.
PowerShell for Database Developers 17
Starting PowerShell
PowerShell for Database Developers 18
• Click Start→All Programs→Accessories→Windows
PowerShell.
• You may have several versions to choose from.
• The ISE is suited to script development and testing.
PowerShell Environments
PowerShell for Database Developers 19
The PowerShell ISE The PowerShell CLI… or …
SQL Agent: Built In Support for PowerShell
PowerShell for Database Developers 20
SSMS: Built In Support for PowerShell
PowerShell for Database Developers 21
PowerShell: Before You Can Run a Script…
• Code Signing and Certificates
• You need to set the security policy for PowerShell using the
Set-ExecutionPolicy cmdlet
• Set-ExecutionPolicy RemoteSigned
– Restricts scripts to run from local machine only.
PowerShell for Database Developers 22
PowerShell: Set-ExecutionPolicy options
PowerShell for Database Developers 23
• Set-Executionpolicy unrestricted
• Set-ExecutionPolicy Restricted
• invoke-command -computername
Server01 -scriptblock {get-executionpolicy}
| set-executionpolicy –force
• set-executionpolicy -scope CurrentUser -
executionPolicy AllSigned –force
PowerShell – Quick Overview
PowerShell for Database Developers 24
Running a Script
PowerShell for Database Developers 25
Set-Location UsersbcafferkyDocumentsPowershell
.Test_CLI.ps1
. tells PowerShell to find the script in the current folder.
To include the path to the script just add it before the script
name.
UsersbcafferkyDocumentsPowershellTest_CLI.ps1
Variables
PowerShell for Database Developers 26
User Created – These variables are the ones we create in the shell and in
scripts. This variables are present only in the current process we are on and
are lost when we close the session. We can create variables in scripts with
global, script, or local scope.
Automatic – These variables keep the state of the PowerShell session and
can not be modified directly. The values of this variables change as we
execute and use the PowerShell session. This variables will save last run
state of cmdlets, commands as well as other objects and information.
Preference – These variables store user preferences for PowerShell. These
variables are created by PowerShell when a session is started and are
populated with default values. We can change the values of these variables.
For example, MaximumHistoryCount that sets the maximum number of
entries in the session history.
Environment – These variables are the variables set by the system for
Command and PowerShell environments.
Variables Are Objects
PowerShell for Database Developers 27
• A variable is an object with methods and properties.
• You can set the scope of a variable such as
Set-Variable -scope Global -name SqlServerMaximumChildItems -Value 0
• By default a variable is created to be in the current scope and any child
scopes.
• A variable can hold different types of data unless it was type casted on
creation.
$myvar = “test”
$myvar = 1
[string] $myvar = “test”
$myvar = 3 # will produce an error
Logical Operators…
PowerShell for Database Developers 28
Comparison Operators…
PowerShell for Database Developers 29
Comparison Operators are NOT Case Sensitive By Default…
Variables Methods
PowerShell for Database Developers 30
(123).equals(456) will return false
(123).CompareTo(150)will return -1
(123).ToString()
($myStringVar1).ToLower()
($myStringVar1).equals($myStringVar2)
($myStringVar1).CompareTo($myStringVar2)
($myStringVar1).PadRight() + "**"
("a b c").split("b")
• Note that CompareTo() differs from equals() in that it returns different values if
the item is greater than or less than.
Finding methods for a string: "The world is everlasting" | get-member will return the methods: Clone,
CompareTo, Contains, CopyTo, EndsWith, Equals,GetEnumerator, GetHashCode, GetType, GetTypeCode,
GetChars, GetLength, IndexOf, IndexOfAny, Insert, IsNormalised, LastIndexOf, LastIndexOfAny, Normalize,
PadLeft, PadRight, Remove, Replace,Split, StartsWith, Substring, ToCharArray, ToLower, ToLowerInvariant,
ToString, ToUpper,ToUpperInvariant, Trim,TrimEnd,TrimStart, Chars, Length
PowerShell Scripting Best Practices
PowerShell for Database Developers 31
• Use naming conventions like ufn_do_something.ps1 where ufn means it’s a
user defined function, scr prefix could signify a script, etc.
• Have comment header at the tops of the file and consider using the built in
reserved comment block tags.
• Separate your programs by type like Script and Function.
• Keep your code under source control.
• Make your code modular and reusable, i.e. functions.
• Resist the temptation to use the system delivered naming convention for your
own scripts, i.e. don’t call a custom function Invoke-This . It is confusing for
users of the code and it may run into a conflict in a later release of
PowerShell. Note: Most examples out there do this.
Don’t Forget the CLI…
PowerShell for Database Developers 32
• The PowerShell CLI is the place to go when you need a
command line.
And Don’t Forget the Profile…
PowerShell for Database Developers 33
• The profile lets you customize the PowerShell environment
to your needs.
• To see the profile path, enter “$profile”.
• Test-Path $profile will tell you if the file exists.
• New-Item -path $profile -type file –force
• This will create a profile file for you.
Taken from: http://technet.microsoft.com/en-us/library/ee692764.aspx
notepad $profile
• Enter “notepad $profile” to edit your
profile settings.
PowerShell – A Very Well Supported Tool
PowerShell for Database Developers 34
Resources:
Downloads and Information
http://www.microsoft.com/windowsserver2003/technologies/management/
Training/Documentation
http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx
Documentation
http://technet.microsoft.com/en-us/library/bb978526.aspx
Free Training
http://pragmaticworks.com/LearningCenter/FreeTrainingWebinars/FutureWebinars.aspx
REVISITING THE GOALS OF THIS PRESENTATION
PowerShell for Database Developers 35
• Amaze you with capabilities of PowerShell.
• Convince you that you need PowerShell.
• Provide a basic understanding of PowerShell programming.
• Point you to where you can get support.
WRAPPING UP:
WHY POWERSHELL?
PowerShell for Database Developers 36
• Full Support by Microsoft.
• Very Powerful.
• Integrated into Windows and Windows Based Products.
• Lot’s of Free Scripts Available.
• Microsoft and Other Vendors Are Including Them More and
More With Products.
WHAT CAN’T YOU DO WITH POWERSHELL?
PowerShell for Database Developers 37
Answer: Not Much!
QUESTIONS?
PowerShell for Database Developers 38
APPENDIX
PowerShell for Database Developers 39
A Sample Script…
PowerShell for Database Developers 40
[int]$valA = 2;
[int]$valB = 3;
[int]$valC = 0;
$valC = $valA + $valB;
write-host ("The sum is: " + $valC);
write-host ("The sum is: $valC ");
Variables
PowerShell for Database Developers 41
• User assigned variables start with a ‘$’ such as $MyVar = “Bryan”
• PowerShell has predefined variables such as $Error that can be viewed in
a script.
• You can set the scope of a variable such as
Set-Variable -scope Global -name SqlServerMaximumChildItems -Value 0
• By default a variable is created to be in the current scope and any child
scopes.
PowerShell: Pipelining
PowerShell for Database Developers 42
PS> ipconfig | findstr "Address"
IP Address. . . . . . . . . . . . : 172.28.21.5 IP
Address. . . . . . . . . . . . : 172.30.160.225
Pipeline commands, - to pass the output of one command to
another command as input
Examples:
# Pipelining - combine CmdLets for power
Get-ChildItem | Where-Object { $_.Length -gt 10kb }
Using Arrays…
PowerShell for Database Developers 43
## Create an array named $myArray that contains the ten numeric (int)
values:1,2,3,4,5,6,7,8,9,10;
$myArray = 1,2,3,4,5,6,7,8,9,10;
[int] $sum = 0;
foreach ($val in $myArray)
{
write-host ("Index with value:$val");
$sum = $sum + $val;
}
write-host ("The sum is: $sum");
Using Associative Arrays…
PowerShell for Database Developers 44
• A compact data structure for storing a collection of keys and values where
each key is paired with a value.
• PowerShell uses the hash table data type for storing the contents of an
associative array because this data structure provides a fast lookup
mechanism.
##Declaration syntax: $<array name> = @{<keyName = Value>;...}
$countries = @{'88' = 'Bangladesh'; '44' = 'United Kingdom'; '11' = 'United States';
'1' = 'Canada'};
$countries ;
For Loops…
PowerShell for Database Developers 45
for (<init>; <condition>; <repeat>)
{<command_block>}
for ($i=0; $i<10;$i++) {Write-Host $i} $i = 1 for
(;;){Write-Host $i}
foreach ($<item> in
$<collection>){<command_block>}
$letterArray = "a","b","c","d"
foreach ($letter in $letterArray)
{
Write-Host $letter
}
While Loop…
PowerShell for Database Developers 46
while (<condition>){<command_block>}
while($val -ne 3)
{
$val++
Write-Host $val
}
IF Blocks…
PowerShell for Database Developers 47
if (<test1>)
{
<code_block1>
}
[elseif (<test2)
{
<code_block2>
}]
[else <code_block3>}]
Functions…
PowerShell for Database Developers 48
As in other programming languages, you can define callable functions…
IF Examples (Logical AND/OR)…
PowerShell for Database Developers 49
IF Examples (Logical Not/Not Equal)…
PowerShell for Database Developers 50
Variables Types
PowerShell for Database Developers 51
Shortcut Data Type
[datetime] Date or time
[string] String of characters
[char] Single character
[double] Double-precision floating number
[single] Single-precision floating number
[int] 32-bit integer
[wmi] Windows Management
Instrumentation (WMI) instance or
collection
[adsi] Active Directory Services object
[wmiclass] WMI class
[Boolean] True or False value

More Related Content

What's hot

Data modeling trends for analytics
Data modeling trends for analyticsData modeling trends for analytics
Data modeling trends for analyticsIke Ellis
 
Building an Effective Data Warehouse Architecture
Building an Effective Data Warehouse ArchitectureBuilding an Effective Data Warehouse Architecture
Building an Effective Data Warehouse ArchitectureJames Serra
 
Azure Synapse Analytics Overview (r1)
Azure Synapse Analytics Overview (r1)Azure Synapse Analytics Overview (r1)
Azure Synapse Analytics Overview (r1)James Serra
 
AWS Cloud Kata 2013 | Singapore - Getting to Scale on AWS
AWS Cloud Kata 2013 | Singapore - Getting to Scale on AWSAWS Cloud Kata 2013 | Singapore - Getting to Scale on AWS
AWS Cloud Kata 2013 | Singapore - Getting to Scale on AWSAmazon Web Services
 
Should I move my database to the cloud?
Should I move my database to the cloud?Should I move my database to the cloud?
Should I move my database to the cloud?James Serra
 
(BI Advanced) Hiram Fleitas - SQL Server Machine Learning Predict Sentiment O...
(BI Advanced) Hiram Fleitas - SQL Server Machine Learning Predict Sentiment O...(BI Advanced) Hiram Fleitas - SQL Server Machine Learning Predict Sentiment O...
(BI Advanced) Hiram Fleitas - SQL Server Machine Learning Predict Sentiment O...Hiram Fleitas León
 
Choosing technologies for a big data solution in the cloud
Choosing technologies for a big data solution in the cloudChoosing technologies for a big data solution in the cloud
Choosing technologies for a big data solution in the cloudJames Serra
 
Azure SQL Database Managed Instance
Azure SQL Database Managed InstanceAzure SQL Database Managed Instance
Azure SQL Database Managed InstanceJames Serra
 
Modern-Data-Warehouses-In-The-Cloud-Use-Cases-Slim-Baltagi
Modern-Data-Warehouses-In-The-Cloud-Use-Cases-Slim-BaltagiModern-Data-Warehouses-In-The-Cloud-Use-Cases-Slim-Baltagi
Modern-Data-Warehouses-In-The-Cloud-Use-Cases-Slim-BaltagiSlim Baltagi
 
Data Vault Automation at the Bijenkorf
Data Vault Automation at the BijenkorfData Vault Automation at the Bijenkorf
Data Vault Automation at the BijenkorfRob Winters
 
Data Lake Overview
Data Lake OverviewData Lake Overview
Data Lake OverviewJames Serra
 
Data Modeling on Azure for Analytics
Data Modeling on Azure for AnalyticsData Modeling on Azure for Analytics
Data Modeling on Azure for AnalyticsIke Ellis
 
Modernize & Automate Analytics Data Pipelines
Modernize & Automate Analytics Data PipelinesModernize & Automate Analytics Data Pipelines
Modernize & Automate Analytics Data PipelinesCarole Gunst
 
Microsoft Data Platform - What's included
Microsoft Data Platform - What's includedMicrosoft Data Platform - What's included
Microsoft Data Platform - What's includedJames Serra
 
AI for an intelligent cloud and intelligent edge: Discover, deploy, and manag...
AI for an intelligent cloud and intelligent edge: Discover, deploy, and manag...AI for an intelligent cloud and intelligent edge: Discover, deploy, and manag...
AI for an intelligent cloud and intelligent edge: Discover, deploy, and manag...James Serra
 
Data warehouse con azure synapse analytics
Data warehouse con azure synapse analyticsData warehouse con azure synapse analytics
Data warehouse con azure synapse analyticsEduardo Castro
 

What's hot (20)

Data modeling trends for analytics
Data modeling trends for analyticsData modeling trends for analytics
Data modeling trends for analytics
 
Building an Effective Data Warehouse Architecture
Building an Effective Data Warehouse ArchitectureBuilding an Effective Data Warehouse Architecture
Building an Effective Data Warehouse Architecture
 
Azure Synapse Analytics Overview (r1)
Azure Synapse Analytics Overview (r1)Azure Synapse Analytics Overview (r1)
Azure Synapse Analytics Overview (r1)
 
Synapse for mere mortals
Synapse for mere mortalsSynapse for mere mortals
Synapse for mere mortals
 
Disaster Recovery Site Implementation with MySQL
Disaster Recovery Site Implementation with MySQLDisaster Recovery Site Implementation with MySQL
Disaster Recovery Site Implementation with MySQL
 
SQL Server Disaster Recovery Implementation
SQL Server Disaster Recovery ImplementationSQL Server Disaster Recovery Implementation
SQL Server Disaster Recovery Implementation
 
AWS Cloud Kata 2013 | Singapore - Getting to Scale on AWS
AWS Cloud Kata 2013 | Singapore - Getting to Scale on AWSAWS Cloud Kata 2013 | Singapore - Getting to Scale on AWS
AWS Cloud Kata 2013 | Singapore - Getting to Scale on AWS
 
2022 02 Integration Bootcamp
2022 02 Integration Bootcamp2022 02 Integration Bootcamp
2022 02 Integration Bootcamp
 
Should I move my database to the cloud?
Should I move my database to the cloud?Should I move my database to the cloud?
Should I move my database to the cloud?
 
(BI Advanced) Hiram Fleitas - SQL Server Machine Learning Predict Sentiment O...
(BI Advanced) Hiram Fleitas - SQL Server Machine Learning Predict Sentiment O...(BI Advanced) Hiram Fleitas - SQL Server Machine Learning Predict Sentiment O...
(BI Advanced) Hiram Fleitas - SQL Server Machine Learning Predict Sentiment O...
 
Choosing technologies for a big data solution in the cloud
Choosing technologies for a big data solution in the cloudChoosing technologies for a big data solution in the cloud
Choosing technologies for a big data solution in the cloud
 
Azure SQL Database Managed Instance
Azure SQL Database Managed InstanceAzure SQL Database Managed Instance
Azure SQL Database Managed Instance
 
Modern-Data-Warehouses-In-The-Cloud-Use-Cases-Slim-Baltagi
Modern-Data-Warehouses-In-The-Cloud-Use-Cases-Slim-BaltagiModern-Data-Warehouses-In-The-Cloud-Use-Cases-Slim-Baltagi
Modern-Data-Warehouses-In-The-Cloud-Use-Cases-Slim-Baltagi
 
Data Vault Automation at the Bijenkorf
Data Vault Automation at the BijenkorfData Vault Automation at the Bijenkorf
Data Vault Automation at the Bijenkorf
 
Data Lake Overview
Data Lake OverviewData Lake Overview
Data Lake Overview
 
Data Modeling on Azure for Analytics
Data Modeling on Azure for AnalyticsData Modeling on Azure for Analytics
Data Modeling on Azure for Analytics
 
Modernize & Automate Analytics Data Pipelines
Modernize & Automate Analytics Data PipelinesModernize & Automate Analytics Data Pipelines
Modernize & Automate Analytics Data Pipelines
 
Microsoft Data Platform - What's included
Microsoft Data Platform - What's includedMicrosoft Data Platform - What's included
Microsoft Data Platform - What's included
 
AI for an intelligent cloud and intelligent edge: Discover, deploy, and manag...
AI for an intelligent cloud and intelligent edge: Discover, deploy, and manag...AI for an intelligent cloud and intelligent edge: Discover, deploy, and manag...
AI for an intelligent cloud and intelligent edge: Discover, deploy, and manag...
 
Data warehouse con azure synapse analytics
Data warehouse con azure synapse analyticsData warehouse con azure synapse analytics
Data warehouse con azure synapse analytics
 

Similar to PowerShellForDBDevelopers

VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware En...
VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware En...VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware En...
VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware En...VMworld
 
Holy PowerShell, BATman! - dogfood edition
Holy PowerShell, BATman! - dogfood editionHoly PowerShell, BATman! - dogfood edition
Holy PowerShell, BATman! - dogfood editionDave Diehl
 
PowerShell for Penetration Testers
PowerShell for Penetration TestersPowerShell for Penetration Testers
PowerShell for Penetration TestersNikhil Mittal
 
24HOP Introduction to Linux for SQL Server DBAs
24HOP Introduction to Linux for SQL Server DBAs24HOP Introduction to Linux for SQL Server DBAs
24HOP Introduction to Linux for SQL Server DBAsKellyn Pot'Vin-Gorman
 
Windows 2012 R2 Multi Server Management
Windows 2012 R2 Multi Server ManagementWindows 2012 R2 Multi Server Management
Windows 2012 R2 Multi Server ManagementSharkrit JOBBO
 
Hi! Ho! Hi! Ho! SQL Server on Linux We Go!
Hi! Ho! Hi! Ho! SQL Server on Linux We Go!Hi! Ho! Hi! Ho! SQL Server on Linux We Go!
Hi! Ho! Hi! Ho! SQL Server on Linux We Go!SolarWinds
 
Tech-Spark: SQL Server on Linux
Tech-Spark: SQL Server on LinuxTech-Spark: SQL Server on Linux
Tech-Spark: SQL Server on LinuxRalph Attard
 
Adding Security to Your Workflow With InSpec - SCaLE17x
Adding Security to Your Workflow With InSpec - SCaLE17xAdding Security to Your Workflow With InSpec - SCaLE17x
Adding Security to Your Workflow With InSpec - SCaLE17xMandi Walls
 
DevOpsDays InSpec Workshop
DevOpsDays InSpec WorkshopDevOpsDays InSpec Workshop
DevOpsDays InSpec WorkshopMandi Walls
 
Sql Server & PowerShell
Sql Server & PowerShellSql Server & PowerShell
Sql Server & PowerShellAaron Shilo
 
Evolutionary Database Design
Evolutionary Database DesignEvolutionary Database Design
Evolutionary Database DesignAndrei Solntsev
 
Client side attacks using PowerShell
Client side attacks using PowerShellClient side attacks using PowerShell
Client side attacks using PowerShellNikhil Mittal
 
InSpec For DevOpsDays Amsterdam 2017
InSpec For DevOpsDays Amsterdam 2017InSpec For DevOpsDays Amsterdam 2017
InSpec For DevOpsDays Amsterdam 2017Mandi Walls
 
PowerShell Workshop Series: Session 2
PowerShell Workshop Series: Session 2PowerShell Workshop Series: Session 2
PowerShell Workshop Series: Session 2Bryan Cafferky
 
XPages -Beyond the Basics
XPages -Beyond the BasicsXPages -Beyond the Basics
XPages -Beyond the BasicsUlrich Krause
 
2019 Blackhat Booth Presentation - PowerUpSQL
2019 Blackhat Booth Presentation - PowerUpSQL2019 Blackhat Booth Presentation - PowerUpSQL
2019 Blackhat Booth Presentation - PowerUpSQLScott Sutherland
 
OSDC 2017 - Mandi Walls - Building security into your workflow with inspec
OSDC 2017 - Mandi Walls - Building security into your workflow with inspecOSDC 2017 - Mandi Walls - Building security into your workflow with inspec
OSDC 2017 - Mandi Walls - Building security into your workflow with inspecNETWAYS
 

Similar to PowerShellForDBDevelopers (20)

VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware En...
VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware En...VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware En...
VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware En...
 
Holy PowerShell, BATman! - dogfood edition
Holy PowerShell, BATman! - dogfood editionHoly PowerShell, BATman! - dogfood edition
Holy PowerShell, BATman! - dogfood edition
 
PowerShell for Penetration Testers
PowerShell for Penetration TestersPowerShell for Penetration Testers
PowerShell for Penetration Testers
 
24HOP Introduction to Linux for SQL Server DBAs
24HOP Introduction to Linux for SQL Server DBAs24HOP Introduction to Linux for SQL Server DBAs
24HOP Introduction to Linux for SQL Server DBAs
 
Windows 2012 R2 Multi Server Management
Windows 2012 R2 Multi Server ManagementWindows 2012 R2 Multi Server Management
Windows 2012 R2 Multi Server Management
 
Hi! Ho! Hi! Ho! SQL Server on Linux We Go!
Hi! Ho! Hi! Ho! SQL Server on Linux We Go!Hi! Ho! Hi! Ho! SQL Server on Linux We Go!
Hi! Ho! Hi! Ho! SQL Server on Linux We Go!
 
Tech-Spark: SQL Server on Linux
Tech-Spark: SQL Server on LinuxTech-Spark: SQL Server on Linux
Tech-Spark: SQL Server on Linux
 
Adding Security to Your Workflow With InSpec - SCaLE17x
Adding Security to Your Workflow With InSpec - SCaLE17xAdding Security to Your Workflow With InSpec - SCaLE17x
Adding Security to Your Workflow With InSpec - SCaLE17x
 
Ipc mysql php
Ipc mysql php Ipc mysql php
Ipc mysql php
 
DevOpsDays InSpec Workshop
DevOpsDays InSpec WorkshopDevOpsDays InSpec Workshop
DevOpsDays InSpec Workshop
 
Sql Server & PowerShell
Sql Server & PowerShellSql Server & PowerShell
Sql Server & PowerShell
 
Evolutionary Database Design
Evolutionary Database DesignEvolutionary Database Design
Evolutionary Database Design
 
Client side attacks using PowerShell
Client side attacks using PowerShellClient side attacks using PowerShell
Client side attacks using PowerShell
 
powershell.pdf
powershell.pdfpowershell.pdf
powershell.pdf
 
InSpec For DevOpsDays Amsterdam 2017
InSpec For DevOpsDays Amsterdam 2017InSpec For DevOpsDays Amsterdam 2017
InSpec For DevOpsDays Amsterdam 2017
 
PowerShell Workshop Series: Session 2
PowerShell Workshop Series: Session 2PowerShell Workshop Series: Session 2
PowerShell Workshop Series: Session 2
 
XPages -Beyond the Basics
XPages -Beyond the BasicsXPages -Beyond the Basics
XPages -Beyond the Basics
 
2019 Blackhat Booth Presentation - PowerUpSQL
2019 Blackhat Booth Presentation - PowerUpSQL2019 Blackhat Booth Presentation - PowerUpSQL
2019 Blackhat Booth Presentation - PowerUpSQL
 
Powering up on PowerShell - BSides Greenville 2019
Powering up on PowerShell  - BSides Greenville 2019Powering up on PowerShell  - BSides Greenville 2019
Powering up on PowerShell - BSides Greenville 2019
 
OSDC 2017 - Mandi Walls - Building security into your workflow with inspec
OSDC 2017 - Mandi Walls - Building security into your workflow with inspecOSDC 2017 - Mandi Walls - Building security into your workflow with inspec
OSDC 2017 - Mandi Walls - Building security into your workflow with inspec
 

PowerShellForDBDevelopers

  • 1. WELCOME TO POWERSHELL FOR DATABASE DEVELOPERS Bryan Cafferky Business Intelligence Consultant BPC Global Solutions LLC bryan256@msn.com www.sql-fy.com http://www.meetup.com/The-RI-Microsoft-BIUG/
  • 2. GOALS OF THIS PRESENTATION PowerShell for Database Developers 2 • Amaze you with the capabilities of PowerShell. • Convince you that you need PowerShell. • Provide a basic understanding of PowerShell programming. • Point you to where you can get support.
  • 3. WHAT IS POWERSHELL? PowerShell for Database Developers 3 PowerShell is like Linux Scripting on Steroids! Thank you to Pragmatic Works and Robert Cain whose presentation inspired me to prepare this presentation.
  • 4. A LITTLE HISTORY PowerShell for Database Developers 4 • Before Windows was DOS (Disk Operating System). • DOS had no graphical interface. Every command had to be entered at a command prompt. • This is known as a Command Line Interface (CLI). Also known as a shell. • A series of commands can be stored in a text file ending with a .BAT extension. By entering the batch file name, you can run the commands in the file. This is called a batch script.
  • 5. OK, BUT WHAT IS A SHELL? PowerShell for Database Developers 5 • Linux, the legacy of Unix, and other Command Line Orientated Operating Systems such as VMS, TSO/CLISTS. • Historically shell scripts are used to perform administrative tasks like backups, moving files around or running batch jobs. • There may be a number of different shells for the same operating system. Linux supports a number of shells: bash (bourne again shell, ksh (korn shell), tcsh (enhanced C shell), zsh (Z shell).
  • 6. THE WINDOWS SHELL PowerShell for Database Developers 6 • The legacy Windows Shell is the Command Prompt, a carry over from DOS. It is not designed to work with modern Windows. • The completely new and re-architected Windows Shell is PowerShell. • PowerShell was designed from the ground up to integrate with .Net and anything in the Windows environment.
  • 7. WHY DOES WINDOWS NEEDS A SHELL? PowerShell for Database Developers 7 • In the early days of Windows, the GUI could do it all. • Now that Windows has grown to support large, complex environments, a programmatic scripting tool was needed to help administer it. • Consider changing security permissions on five thousand users one mouse click at time or manually backing up three hundred SQL Servers.
  • 8. CONNECTING IT ALL PowerShell for Database Developers 8 SQL Server Exchange Server SharePoint Active Directory Office File System PowerShell Windows Server IIS
  • 9. Restart-Computer REMOTE EXECUTION PowerShell for Database Developers 9 Dev Server 1 Prod Server 1 Dev Server 2 Dev Server 3 Prod Server 2 DNS Server PowerShell Mary Smith’s Computer John Doe’s Computer Invoke-Command Enable-PSRemoting -Force Restart-Service Enable-PSRemoting -Force Restart-Computer Show-EventLog Test-Connection Get-EventLog Set-Service
  • 10. PowerShell – Scripting on Steroids • Full Support of Traditional Shell features: – Piping – Access to Hardware environment • Built In Integration with .NET • Support for Variables and Objects • Support for Lists, Arrays and Hash Tables • Secure Model PowerShell for Database Developers 10
  • 11. PowerShell – Two Modes • The CLI • Integrated Scripting Environment PowerShell for Database Developers 11
  • 12. PowerShell CMDLET - Pronounced "command-let“. • Is a single-feature command that manipulates objects in Windows PowerShell. • You can recognize cmdlets by their name format -- a verb and noun separated by a dash (-), such as Get-Help, Get-Process, and Start- Service • Are designed to be used in combination with other cmdlets – "get" cmdlets only retrieve data, – "set" cmdlets only establish or change data, – "format" cmdlets only format data, – "out" cmdlets only direct the output to a specified destination.. • Each cmdlet has a help file that you can access by typing: – get-help <cmdlet-name> -detailed – Example: get-help write-host Note: You can create your own cmdlet using PowerShell Software Developers Kit (SDK). PowerShell for Database Developers 12
  • 13. CMDLET Examples • Get-Help * • Get-Process • Get-Location • Set-Location C: • Write-Host “Hello” • Get-WMIObject Win32_BIOS • Set-ExecutionPolicy RemoteSigned PowerShell for Database Developers 13
  • 14. PowerShell – Common Uses • Use by developers to do pre and post job processing. • Database Support (SQL Server Integration) – Backups, Utilitities, Security • Special database processes like database copy (SMO) • Security Administration (Active Directory) • File System (.NET library and CMDLETS) • Network Administration • Working with MS Products; SharePoint, Office, etc. • Working with XML PowerShell for Database Developers 14
  • 15. PowerShell – Recipes • Say it using SAPI. (scr_say_something, ufn_out_html) • SQL Server. (scr_SQL_Server, scr_winform_sql, scr_job_runner) • Wait for file polling. (ufn_wait_for_file) • Maintaining Data. (scr_gridview_passthru, scr_maintain_data) • Combining files. (ufn_combine_files) • Fun with CSV Files. (scr_load_csv_file_to_table) • Import/Export Excel files. (scr_SQLServer_2_Excel) PowerShell for Database Developers 15
  • 16. PowerShell – Learning Challenges • Have to think differently than with other tools and CLIs due to object orientation, piping, and unusual syntax conventions. • Simple and powerful yet unintuitive. • Most examples are targeted towards system administrators. • Easy to do things the hard way when there is often a command-let for the task or a way to leverage piping. • Can find a work around such as a .bat file to avoid using PowerShell. • With great power comes many options which adds to the learning curve to master this tool. PowerShell for Database Developers 16
  • 17. PowerShell – Periods Matter PowerShell will not default to the current folder. .my_function.ps1 # Refers to a script in the current folder. C:ScriptsTest.ps1 # Refers to a script not in the current folder. Use Dot Sourcing to call the script and keep it in memory. . c:scriptstest.ps1 # Run the script and keep the functions and variables after it is done, i.e. available to the session. . .my_function.ps1 # Run script which is in the current folder and keep the functions and variables after it is done. Space in the path? & "C:My ScriptsTest.ps1“ # Run script with space in the path. PowerShell for Database Developers 17
  • 18. Starting PowerShell PowerShell for Database Developers 18 • Click Start→All Programs→Accessories→Windows PowerShell. • You may have several versions to choose from. • The ISE is suited to script development and testing.
  • 19. PowerShell Environments PowerShell for Database Developers 19 The PowerShell ISE The PowerShell CLI… or …
  • 20. SQL Agent: Built In Support for PowerShell PowerShell for Database Developers 20
  • 21. SSMS: Built In Support for PowerShell PowerShell for Database Developers 21
  • 22. PowerShell: Before You Can Run a Script… • Code Signing and Certificates • You need to set the security policy for PowerShell using the Set-ExecutionPolicy cmdlet • Set-ExecutionPolicy RemoteSigned – Restricts scripts to run from local machine only. PowerShell for Database Developers 22
  • 23. PowerShell: Set-ExecutionPolicy options PowerShell for Database Developers 23 • Set-Executionpolicy unrestricted • Set-ExecutionPolicy Restricted • invoke-command -computername Server01 -scriptblock {get-executionpolicy} | set-executionpolicy –force • set-executionpolicy -scope CurrentUser - executionPolicy AllSigned –force
  • 24. PowerShell – Quick Overview PowerShell for Database Developers 24
  • 25. Running a Script PowerShell for Database Developers 25 Set-Location UsersbcafferkyDocumentsPowershell .Test_CLI.ps1 . tells PowerShell to find the script in the current folder. To include the path to the script just add it before the script name. UsersbcafferkyDocumentsPowershellTest_CLI.ps1
  • 26. Variables PowerShell for Database Developers 26 User Created – These variables are the ones we create in the shell and in scripts. This variables are present only in the current process we are on and are lost when we close the session. We can create variables in scripts with global, script, or local scope. Automatic – These variables keep the state of the PowerShell session and can not be modified directly. The values of this variables change as we execute and use the PowerShell session. This variables will save last run state of cmdlets, commands as well as other objects and information. Preference – These variables store user preferences for PowerShell. These variables are created by PowerShell when a session is started and are populated with default values. We can change the values of these variables. For example, MaximumHistoryCount that sets the maximum number of entries in the session history. Environment – These variables are the variables set by the system for Command and PowerShell environments.
  • 27. Variables Are Objects PowerShell for Database Developers 27 • A variable is an object with methods and properties. • You can set the scope of a variable such as Set-Variable -scope Global -name SqlServerMaximumChildItems -Value 0 • By default a variable is created to be in the current scope and any child scopes. • A variable can hold different types of data unless it was type casted on creation. $myvar = “test” $myvar = 1 [string] $myvar = “test” $myvar = 3 # will produce an error
  • 28. Logical Operators… PowerShell for Database Developers 28
  • 29. Comparison Operators… PowerShell for Database Developers 29 Comparison Operators are NOT Case Sensitive By Default…
  • 30. Variables Methods PowerShell for Database Developers 30 (123).equals(456) will return false (123).CompareTo(150)will return -1 (123).ToString() ($myStringVar1).ToLower() ($myStringVar1).equals($myStringVar2) ($myStringVar1).CompareTo($myStringVar2) ($myStringVar1).PadRight() + "**" ("a b c").split("b") • Note that CompareTo() differs from equals() in that it returns different values if the item is greater than or less than. Finding methods for a string: "The world is everlasting" | get-member will return the methods: Clone, CompareTo, Contains, CopyTo, EndsWith, Equals,GetEnumerator, GetHashCode, GetType, GetTypeCode, GetChars, GetLength, IndexOf, IndexOfAny, Insert, IsNormalised, LastIndexOf, LastIndexOfAny, Normalize, PadLeft, PadRight, Remove, Replace,Split, StartsWith, Substring, ToCharArray, ToLower, ToLowerInvariant, ToString, ToUpper,ToUpperInvariant, Trim,TrimEnd,TrimStart, Chars, Length
  • 31. PowerShell Scripting Best Practices PowerShell for Database Developers 31 • Use naming conventions like ufn_do_something.ps1 where ufn means it’s a user defined function, scr prefix could signify a script, etc. • Have comment header at the tops of the file and consider using the built in reserved comment block tags. • Separate your programs by type like Script and Function. • Keep your code under source control. • Make your code modular and reusable, i.e. functions. • Resist the temptation to use the system delivered naming convention for your own scripts, i.e. don’t call a custom function Invoke-This . It is confusing for users of the code and it may run into a conflict in a later release of PowerShell. Note: Most examples out there do this.
  • 32. Don’t Forget the CLI… PowerShell for Database Developers 32 • The PowerShell CLI is the place to go when you need a command line.
  • 33. And Don’t Forget the Profile… PowerShell for Database Developers 33 • The profile lets you customize the PowerShell environment to your needs. • To see the profile path, enter “$profile”. • Test-Path $profile will tell you if the file exists. • New-Item -path $profile -type file –force • This will create a profile file for you. Taken from: http://technet.microsoft.com/en-us/library/ee692764.aspx notepad $profile • Enter “notepad $profile” to edit your profile settings.
  • 34. PowerShell – A Very Well Supported Tool PowerShell for Database Developers 34 Resources: Downloads and Information http://www.microsoft.com/windowsserver2003/technologies/management/ Training/Documentation http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx Documentation http://technet.microsoft.com/en-us/library/bb978526.aspx Free Training http://pragmaticworks.com/LearningCenter/FreeTrainingWebinars/FutureWebinars.aspx
  • 35. REVISITING THE GOALS OF THIS PRESENTATION PowerShell for Database Developers 35 • Amaze you with capabilities of PowerShell. • Convince you that you need PowerShell. • Provide a basic understanding of PowerShell programming. • Point you to where you can get support.
  • 36. WRAPPING UP: WHY POWERSHELL? PowerShell for Database Developers 36 • Full Support by Microsoft. • Very Powerful. • Integrated into Windows and Windows Based Products. • Lot’s of Free Scripts Available. • Microsoft and Other Vendors Are Including Them More and More With Products.
  • 37. WHAT CAN’T YOU DO WITH POWERSHELL? PowerShell for Database Developers 37 Answer: Not Much!
  • 40. A Sample Script… PowerShell for Database Developers 40 [int]$valA = 2; [int]$valB = 3; [int]$valC = 0; $valC = $valA + $valB; write-host ("The sum is: " + $valC); write-host ("The sum is: $valC ");
  • 41. Variables PowerShell for Database Developers 41 • User assigned variables start with a ‘$’ such as $MyVar = “Bryan” • PowerShell has predefined variables such as $Error that can be viewed in a script. • You can set the scope of a variable such as Set-Variable -scope Global -name SqlServerMaximumChildItems -Value 0 • By default a variable is created to be in the current scope and any child scopes.
  • 42. PowerShell: Pipelining PowerShell for Database Developers 42 PS> ipconfig | findstr "Address" IP Address. . . . . . . . . . . . : 172.28.21.5 IP Address. . . . . . . . . . . . : 172.30.160.225 Pipeline commands, - to pass the output of one command to another command as input Examples: # Pipelining - combine CmdLets for power Get-ChildItem | Where-Object { $_.Length -gt 10kb }
  • 43. Using Arrays… PowerShell for Database Developers 43 ## Create an array named $myArray that contains the ten numeric (int) values:1,2,3,4,5,6,7,8,9,10; $myArray = 1,2,3,4,5,6,7,8,9,10; [int] $sum = 0; foreach ($val in $myArray) { write-host ("Index with value:$val"); $sum = $sum + $val; } write-host ("The sum is: $sum");
  • 44. Using Associative Arrays… PowerShell for Database Developers 44 • A compact data structure for storing a collection of keys and values where each key is paired with a value. • PowerShell uses the hash table data type for storing the contents of an associative array because this data structure provides a fast lookup mechanism. ##Declaration syntax: $<array name> = @{<keyName = Value>;...} $countries = @{'88' = 'Bangladesh'; '44' = 'United Kingdom'; '11' = 'United States'; '1' = 'Canada'}; $countries ;
  • 45. For Loops… PowerShell for Database Developers 45 for (<init>; <condition>; <repeat>) {<command_block>} for ($i=0; $i<10;$i++) {Write-Host $i} $i = 1 for (;;){Write-Host $i} foreach ($<item> in $<collection>){<command_block>} $letterArray = "a","b","c","d" foreach ($letter in $letterArray) { Write-Host $letter }
  • 46. While Loop… PowerShell for Database Developers 46 while (<condition>){<command_block>} while($val -ne 3) { $val++ Write-Host $val }
  • 47. IF Blocks… PowerShell for Database Developers 47 if (<test1>) { <code_block1> } [elseif (<test2) { <code_block2> }] [else <code_block3>}]
  • 48. Functions… PowerShell for Database Developers 48 As in other programming languages, you can define callable functions…
  • 49. IF Examples (Logical AND/OR)… PowerShell for Database Developers 49
  • 50. IF Examples (Logical Not/Not Equal)… PowerShell for Database Developers 50
  • 51. Variables Types PowerShell for Database Developers 51 Shortcut Data Type [datetime] Date or time [string] String of characters [char] Single character [double] Double-precision floating number [single] Single-precision floating number [int] 32-bit integer [wmi] Windows Management Instrumentation (WMI) instance or collection [adsi] Active Directory Services object [wmiclass] WMI class [Boolean] True or False value

Editor's Notes

  1. This template can be used as a starter file for presenting training materials in a group setting. Sections Right-click on a slide to add sections. Sections can help to organize your slides or facilitate collaboration between multiple authors. Notes Use the Notes section for delivery notes or to provide additional details for the audience. View these notes in Presentation View during your presentation. Keep in mind the font size (important for accessibility, visibility, videotaping, and online production) Coordinated colors Pay particular attention to the graphs, charts, and text boxes. Consider that attendees will print in black and white or grayscale. Run a test print to make sure your colors work when printed in pure black and white and grayscale. Graphics, tables, and graphs Keep it simple: If possible, use consistent, non-distracting styles and colors. Label all graphs and tables.
  2. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.
  3. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.
  4. Discuss outcomes of the case study or class simulation. Cover best practices.
  5. Summarize presentation content by restating the important points from the lessons. What do you want the audience to remember when they leave your presentation? Save your presentation to a video for easy distribution (To create a video, click the File tab, and then click Share.  Under File Types, click Create a Video.)
  6. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.
  7. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.
  8. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.
  9. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.
  10. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.
  11. Microsoft Confidential
  12. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.
  13. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.
  14. Microsoft Confidential
  15. Microsoft Confidential
  16. This is another option for an Overview slides using transitions.
  17. Add a case study or class simulation to encourage discussion and apply lessons.
  18. Microsoft Confidential
  19. Microsoft Confidential
  20. Microsoft Confidential
  21. Microsoft Confidential
  22. This is another option for an Overview slides using transitions.