Presentation is loading. Please wait.

Presentation is loading. Please wait.

Best Practices for Script Design A PowerShell.org TechSession.

Similar presentations


Presentation on theme: "Best Practices for Script Design A PowerShell.org TechSession."— Presentation transcript:

1 Best Practices for Script Design A PowerShell.org TechSession

2 Remember Find the latest TechSessions at http://powershell.org/wp/techsession-webinars/. http://powershell.org/wp/techsession-webinars/ Advanced registration is required to attend the live events, where you can participate in Q&A Recordings posted to YouTube, usually in 48 hours.

3 Today We’ll be looking at best practices for designing scripts that best leverage PowerShell’s native patterns and capabilities. Please submit questions to the Q&A panel as we go – I’ll address them as you submit them.

4 Basic Information Level: 200-300 Best practices and patterns for using complex technologies Pre-requisites: Solid experience with Windows PowerShell scripting/programming

5 Scripting Goals Ease testing and debugging Maximize reuse Minimize programming effort Conform to existing shell usage patterns Get the job done!

6 Tools vs. Controllers TOOLS Do one thing and one thing only Not tied to a specific context Accept input only via parameters; output only via pipeline Cmdlet-style naming Advanced functions contained in a script module CONTROLLERS Coordinate multiple tools to complete a process Tied to a specific process May read data from files, params, databases, etc. May not use cmdlet-style naming Monolithic script with minimal functionality; mostly logic

7 Tools Create information for use by other tools Get-, Import-, ConvertFrom-, etc. Process information and take action Get-, Set-, New-, Remove-, etc. Put raw data into a particular form Export-, ConvertTo-, Format-, Out-

8 Poor Function Get-Something { [CmdletBinding()] Param( [string[]]$ComputerName, [string]$FileName ) if ($PSBoundParameters.ContainsKey('Filename')) { $ComputerName = Get-Content $FileName }

9 Improved Function Get-Something { [CmdletBinding()] Param( [Parameter(ValueFromPipeline)] [string[]]$ComputerName ) } Get-Content computers.txt | Get-Something Get-ADComputer –filter * | Select –Expand Name | Get-Something

10 Poor... $objects += $object $objects | Format-Table }

11 Improved... Write-Output $object } Do-Whatever | Format-Table

12 General Guidelines If information could ever possibly come from more than one source, make dedicated tools to get it, and feed it to other tools via the pipeline or parameters. If a tool does formatting, puts data someplace (e.g., a database), etc., then that is all the tool should do.

13 General Guidelines If a tool is making any kind of change, then that is all it should do. It should never also worry about where input came from or where output is going. Remember: If a tool makes changes, it should support –confirm and –whatif; those can only affect one operational within the tool, so the tool should only do that one thing.

14 Possible Red Flags One parameter set accepts information (like a computer name), while another accepts a source for that information (like a filename) Any use of Get-Content Any use of Format-, Export-, Out-, or ConvertTo- in a tool that also does other work

15 Controllers Do very little work. Instead, they implement a lot of logic to decide what work to do – and call commands (tools) to do that work. Responsible for the entire cycle of input/work/output. Automate a process, produce a report, display a menu, etc. These control one or more tools to do something useful. Tools may not do a completely useful thing by themselves; the controllers make that happen.

16 Tools vs. Controllers (Redux) New-ADUser Does one thing Doesn't complete an entire process (e.g., for a new employee) Has no idea where new user info is coming from ProvisionEmployee.ps1 Does many things (user account, home dir, add to groups, etc.) Has a specific source for new user info (perhaps an HR database or spreadsheet) Uses New-ADUser internally

17 Goal: Testing and Debugging Tools can be tested independently, using the same input (via parameters) a controller might use Makes debugging simpler and more contained Controllers do nothing, so it's easier to test their logic – especially if you've properly implemented –whatif in your tools.

18 Goal: Reusability Because tools aren't tied to a specific context or use case, they're easier to reuse Because controllers contain little to no functionality, they don't need to offer reusability

19 Goal: Minimize and Conform Tools work consistently with existing shell patterns (e.g., the same as cmdlets, when you do it right) Controllers become the traditional "glue" script that pulls tools together Controllers can be written more quickly because they don't do complex work; they're only coordinating pre- tested, pre-existing tools

20 Let's See an Example

21 Let’s Take Some Questions I know you’ve got ‘em… ask away.

22 Thank You! Find the latest TechSessions at http://powershell.org/wp/techsession-webinars/. Advanced registration is required to attend the live events, where you can participate in Q&A Recordings posted to YouTube, usually in 48 hours. http://powershell.org/wp/techsession-webinars/ Ask follow-up questions in the Forums on PowerShell.org.


Download ppt "Best Practices for Script Design A PowerShell.org TechSession."

Similar presentations


Ads by Google