Presentation is loading. Please wait.

Presentation is loading. Please wait.

PowerShell Best Practices for SQL DBA’s

Similar presentations


Presentation on theme: "PowerShell Best Practices for SQL DBA’s"— Presentation transcript:

1 PowerShell Best Practices for SQL DBA’s
Ed Wilson, MCDBA Microsoft Scripting Guy 1/10/2019

2 Ed Wilson Microsoft Scripting Guy
“Windows PowerShell 2.0 Best Practices” @ScriptingGuys Mr.Ed.Wilson on Facebook Ed Wilson on Linked In

3 Objectives and Take-Aways
!($local:BestPractice -eq $global:BestPractice) Script Progression TechNet Script Center FaceBook – Friend Me Mr.Ed.Wilson

4 What is Windows PowerShell?
Console Interactive commands Query and configure Run jobs Scripting language Automate everything Sharable and reusable Windows PowerShell 2.0 comes in two flavors: the command line version, and the script version. You can think of combining the DOS prompt with VBScript. This is something that is foreign to Windows people, but has been around in other computing environments for a long time. The console gives you the ability to quickly run cmdlets, external executables, and even code in an interactive fashion. In Windows PowerShell 2.0 you can work on the local host, or run commands against remote computers. Windows PowerShell scripts can use the same cmdlets that are used in the PowerShell console, but can also use COM objects, .NET Framework classes, ADSI, ADO, WMI, and even WIN32 API’s. In the Windows PowerShell console, there is a Start-Transcript cmdlet that records your commands and associated output. I have written a Start-Transcript function for the Windows PowerShell ISE.

5 Progression Modules Advanced Functions Functions Quick Script Console
Complexity and Development Time Console

6 Using Console Aliases Positional Arguments
Partial parameter completion Variables Profile functions and aliases Include files (dot sourcing) Import-history

7 Using Windows PowerShell Console
Demo Using Windows PowerShell Console

8 Quick Script Basically collection of Windows PowerShell commands
Add variables Error handling Comments Straight line execution of script If use parameters, add some kind of help #Requires –Version 2.0 Quick scripts range in usefullness and complexity from a collection of commands – and therefore help you to remember that cool sequence you were not sure would work – but did, to scripts that use variables, error handling, and comments. If the script accepts arguments, or parameters, make sure you add some kind of help with an example syntax.

9 Demo DemoScriptCommands.ps1

10 Functions Use Verb-Noun Include closing #end function
Use Param, not ($myparameter) Use type constraint on parameters Supply default values for params Consider partial param completion in names Choose names that do not conflict Create unique alias for function Use Test-Path to check resource Use Join-Path when building path to resource Often that quick script you dashed off, proved to be rather useful. The next stage in the evolution is to migrate the code and put it into a function. This facilitates re-use – either by copy / paste method, or by dot sourcing the function.

11 Demo Function DemoScriptFunction.ps1

12 Advanced Function Help tags Avoid Throw unless actually causes error
.Synopsis .Example Avoid Throw unless actually causes error Parameter Validation attributes Avoid Out-Host, Out-Default Return Objects Do not make permanent modifications Env Advanced functions in the Windows PowerShell 2.0 beta were called script cmdlets – a name I still like. Because advanced functions can be used to act like cmdlets: integrate with Get-Help, accept pipeline input, implement parameter validation and do some really sophisticated stuff. You can do all this, and leave the function in a script if you wish, or you may want to migrate it to a separate file to facilitate importing (dot sourcing) or begin thinking about creating a module.

13 Practical Example Evolution of a function … the conversion module demo  First 6 installments of Weekend Scripter on Hey Scripting Guy blog.

14 TechNet Script Center Hey Scripting Guy Blog – 99% PowerShell for last 24 months Hey Scripting Guy is 7 days a week! Script Repository – Over 5,000 scripts –some cool, others rather rudimentary. Official Scripting Guys Forum – sweet! TechNet Library content – good stuff Downloads page = nice Coming = more videos, etc.

15 Social media www.ScriptingGuys.Com http://bit.ly/hsgblog
Scripting Guys group on Facebook Scripting Guys fan page on Facebook Mr.Ed.Wilson on Facebook Ed Wilson on Linked In


Download ppt "PowerShell Best Practices for SQL DBA’s"

Similar presentations


Ads by Google