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.

Brandon Olin

April 09, 2018
Tweet

More Decks by Brandon Olin

Other Decks in Technology

Transcript

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

    use them? • Gotchas • Importing • Verbose/Debug • Sharing • More… • Demos • Q&A
  2. 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()
  3. Why Use A Class? • Separation of concerns • Encapsulation

    of entities • Inheritance – DRY • Overload methods
  4. 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
  5. 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