Slide 1

Slide 1 text

PowerShell Classes Here Be Dragons Brandon Olin @devblackops

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Agenda • What are PowerShell Classes? • Why would you use them? • Gotchas • Importing • Verbose/Debug • Sharing • More… • Demos • Q&A

Slide 4

Slide 4 text

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()

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

DEMO

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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