Presentation is loading. Please wait.

Presentation is loading. Please wait.

Don Jones Co-Founder Concentrated Technology, LLC Session Code: WSV325.

Similar presentations


Presentation on theme: "Don Jones Co-Founder Concentrated Technology, LLC Session Code: WSV325."— Presentation transcript:

1

2 Don Jones Co-Founder Concentrated Technology, LLC Session Code: WSV325

3 Introductions Don Jones Windows PowerShell MVP Aware Recipient Co-author, Windows PowerShell: TFM (SAPIEN Press) Monthly columnist, Microsoft TechNet Magazine Blogger at ConcentratedTech.com (with daily tips and other content) You A Windows administrator who has perhaps dabbled in Windows PowerShell… …and are looking to do more and go further

4 About the Code I don’t like “canned” demos – I’ll be doing everything live (It shows how easy this is!) I will run Start-Transcript to begin with, and capture everything I type in the shell I’ll be happy to you the transcript and any scripts I write – just drop off a business card or your address I’ll include a copy of this deck as well I’ll send shortly after the conference is over

5 Tip #1: From Command to Tool PSH is great in that it lets you accomplish an awful lot with a single command-line… …but what about turning that into a tool for less- experienced administrators? …but what about turning that into a more flexible tool that can be used in multiple situation? …but what about turning that into a reusable thing that can be made a part of other, larger processes? …but what about giving it the ability to produce output that can go to multiple forms more easily?

6 The Answer: Objects and Funtions Wrapping your one-liner into a parameterized function makes it easier for others to re-use Having it accept pipeline input and emit objects to the pipeline give it amazing flexibility with very little work on your part

7 Tip #1: From Command to Tool

8 Tip #2: Formatting Control Freak The shell’s built-in formatting system is pretty smart…...but it’s not psychic. Let’s look at how it works, and how we can exploit it to get better output for any particular situation

9 Formatting and the Pipeline The Pipeline CmdletCmdlet ObjectsObjects

10 Formatting and the Pipeline The Pipeline CmdletCmdlet ObjectsObjects Out- Default

11 Formatting and the Pipeline The Pipeline CmdletCmdlet ObjectsObjects Out- Default Out-Host Out-File Out-Printer

12 Formatting and the Pipeline The Pipeline CmdletCmdlet ObjectsObjects Out- Default Out-Host Out-File Out-Printer Is this an object or a formatting object?

13 Formatting and the Pipeline The Pipeline CmdletCmdlet ObjectsObjects Out- Default Out-Host Out-File Out-Printer FormattingSystemFormattingSystem

14 Formatting and the Pipeline The Pipeline CmdletCmdlet ObjectsObjects Out- Default Out-Host Out-File Out-Printer FormattingSystemFormattingSystem Formatting Objects Follow the formatting rules!

15 Formatting and the Pipeline The Pipeline CmdletCmdlet Out- Default Out-Host Out-File Out-Printer FormattingSystemFormattingSystem Formatting Objects

16 Formatting and the Pipeline The Pipeline CmdletCmdlet Out- Default Out-Host Out-File Out-Printer Formatting Objects Is this an object or a formatting object?

17 Formatting and the Pipeline The Pipeline CmdletCmdlet Out- Default Out-Host Out-File Out-Printer OutputDisplayOutputDisplay

18 Formatting Rules Is there a pre-defined view for this object type? YES: Use that view. No further questions. NO: Move on. Is there a pre-defined DefaultDisplayProperties PropertySet for this object type? YES: Consider only those properties in the next decision. NO: Consider all properties in the next decision.

19 Formatting Rules (con't) Are we working with >5 properties? YES: Use a list NO: Use a table Using the selected type of layout, and the selected properties, construct the formatting objects to create the display Format-Table (ft) Format-List (fl) Format-Wide (fw) Format-Custom (rarely)

20 The Formatting Rules

21 Formatting Control You can use the three main Format-* cmdlets on their own to achieve finer control over the results Format-Table in particulate is a ton of fun

22 Tip #2: Formatting Control

23 Tip #3: Security The shell has four security modes with regard to script execution: None (Restricted; the default) RemoteSigned (limited security) AllSigned (awesome, but potentially a hassle) Unrestricted (for the very brave) Examine/change using Get- or Set- ExecutionPolicy Can be controlled locally (must be Admin) or via Group Policy (download the ADM template)

24 Security and Profiles The shell is hardcoded to look for up to four profile scripts and execute whatever is inside them each time it loads These don’t exist by default – the common (per-user) ones go in your Documents folder Even the folder doesn’t exist by default! But once created, these are local scripts – not remote scripts They’re in you Documents folder – think about how easy it is for you (or any process running as you) to create/modify these

25 Fun with Profiles

26 Um… ExeuctionPolicy = RemoteSigned Malware = Modifies profile.ps1 in your Documents folder (or creates it) Your day = ruined

27 Safety first! Rely on the AllSigned execution policy – it requires scripts to be signs, and will detect changes to a signed script Secures your profiles (even if they don’t currently exist) Obtain a Class 3 Authenticode code-signing certificate From your internal PKI, or commercially Use Set-AuthenticodeSignature to sign a script Once signed, any script – including your profile – can’t be altered without breaking the signature

28 Tip #3: Security with Profiles

29 Tip #4: Dealing with Errors Hey, errors happen Sometimes you expect it Missing file Can’t connect to a computer via WMI Etc How can you make the shell stop bleeding red text on the screen and handle the error more gracefully?

30 Background Info Error Messages vs. Exceptions $ErrorActionPreference -ErrorActionPreference (-EA) common parameter

31 Errors vs. Exceptions

32 Trapping Errors Create a Trap construct in advance of the error Anticipate the error with –EA “Stop” Don’t set $ErrorActionPreference to “Stop” globally – hides useful errors Do something in the trap $error[0] will have the most recent error Exit with continue or break Picks up on the line of code following the exception Exits the scope as passes the exception up

33 Tip #4: Error Trapping

34 Tricky! Errors Work in Scope GLOBALGLOBAL SCRIPTSCRIPT FUNCTIONFUNCTION Trap { # do something continue } Write “Hello” Gwmi –ea “Stop” Write “World” Trap { # do something continue } Write “Windows” Function Write “PowerShell”

35 Tricky! Errors Work in Scope GLOBALGLOBAL SCRIPTSCRIPT FUNCTIONFUNCTION Write “Hello” Gwmi –ea “Stop” Write “World” Trap { # do something continue } Write “Windows” Function Write “PowerShell”

36 Tip #5: WMI Tricks All WMI objects come with a __SERVER property that tells you what computer the object was born on The Get-WMIObject cmdlet has a – computerName property which can accept a collection of String objects These two pieces of information are not unrelated

37 Tip #5: WMI Multiquery

38 Summary Powerful tools from one-liners Take control of formatting Respond to errors Make WMI work harder for you Secure the shell Always keep pushing forward and learning new tidbits!

39 Last Chance for Questions Please complete a session evaluation online – and please feel free to hunt me down if you have other questions or just want to “talk shell” for a bit. I blog constantly (tech tips) at ConcentratedTech.com – I hope you’ll find something useful there Remember: Drop off a business card or write down your address if you want me to mail you the code and shell transcript You can also visit ConcentratedTech.com and click on “Newsletter” to add your address I’ll get this stuff sent to you ASAP, so don’t wait to get me your info

40

41 Sessions On-Demand & Community Resources for IT Professionals Resources for Developers Microsoft Certification and Training Resources Microsoft Certification & Training Resources Resources

42 Related Content Breakout Sessions WSV402 – Advanced Windows PowerShell Scripting WSV319 – PowerShell and Windows Server 2008 R2

43 Windows Server Resources Make sure you pick up your copy of Windows Server 2008 R2 RC from the Materials Distribution Counter Learn More about Windows Server 2008 R2: Technical Learning Center (Orange Section): Highlighting Windows Server 2008 and R2 technologies Over 15 booths and experts from Microsoft and our partners Over 15 booths and experts from Microsoft and our partners

44 Complete an evaluation on CommNet and enter to win!

45 © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.


Download ppt "Don Jones Co-Founder Concentrated Technology, LLC Session Code: WSV325."

Similar presentations


Ads by Google