Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PDX-PSUG-2018-02-21-PowerShell-Classes

 PDX-PSUG-2018-02-21-PowerShell-Classes

Classes are a powerful capability in PowerShell and are meant to provide a familiar development experience to more traditional programmers. You will not experience calm skies and quiet seas though. This talk will show you where the dragons lurk and help you navigate the stormy seas.

C44fc6d582b47ebc3da0e059730dddfd?s=128

Brandon Olin

February 28, 2018
Tweet

Transcript

  1. PowerShell Classes Here Be Dragons Brandon Olin @devblackops

  2. Slides & Code Deck: http://bit.ly/2BGfXWz Code: http://bit.ly/2BFDVRP

  3. Agenda • What are PowerShell Classes? • Why would you

    use them? • Gotchas • Importing • Verbose/Debug • Sharing • More… • Demos • Q&A
  4. What is a PowerShell Class? class Car { [string]$Manufacturer [string]$Model

    [int]$Year [string]Drive() { return 'zoom zoom’ } } $c = [Car]::new() class Tesla : Car { [int]$MaxSpeed = 250 [int]$Range = 620 Tesla () { $this.Manufacturer = 'Tesla’ $this.Model = 'Roadster’ $this.Year = 2020 } } $t = [Tesla]::new()
  5. Why Use A Class? • Separation of concerns • Encapsulation

    of entities • Inheritance – DRY • Overload methods Classes define a structure and allow restricting the methods that work with that structure. (Classes are for rigid structures, custom objects are for fluid structures) - Missy Januszko
  6. DEMO

  7. Takeaways • Import-Module does NOT import classes but using module

    does • “using module” must be first command in script • Cannot dot source class files in psm1 if you wish to share the class • Order matters • Loading classes with dependencies requires classes be defined in order • No secrets • Class properties are not private, only hidden. • Access them with Get-Member -Force • #requires does not import classes. You shouldn’t use it anyway. • They’re still cool, if you like dragon slaying
  8. More From Me devblackops.io @devblackops github.com/devblackops github.com/poshbotio http://bit.ly/2BFDVRP http://bit.ly/2BGfXWz psake

    POSHOrigin Watchmen PoshBot NetScaler PasswordState PSHealthZ Operation Validation Framework