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

PowerShell Classes - Here be Dragons

PowerShell Classes - Here be Dragons

Classes are a powerful capability in PowerShell and are meant to provide a more familiar developing 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

April 09, 2018
Tweet

Transcript

  1. Developing with PowerShell Classes Here Be Dragons Brandon Olin @devblackops

  2. Slides & Code https://bit.ly/2GbLxe4 https://bit.ly/2mO9oIt

  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
  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. • No version support with using module • I think they’re still cool, if you like dragon slaying
  8. More From Me devblackops.io @devblackops github.com/devblackops github.com/poshbotio https://bit.ly/2mO9oIt https://bit.ly/2GbLxe4 psake

    POSHOrigin Watchmen PoshBot NetScaler PasswordState PSHealthZ Operation Validation Framework
  9. THANK YOU! Please use the event app or Sched.com to

    submit a session rating!