KEMBAR78
Modernizing PowerShell Scripts | PDF
M o d e r n i z i n g Po w e r S h e l l S c r i p t s
whoami
• Matthias van den Elsacker
• Axxes since 2014
• System Engineer @ Umicore since 2014
• First Line for 1 year
• Second/Third Line SCCM for 4 years
• Network Engineer @ Umicore since 2019
• Contact: Matthias.vandenElsacker@axxes.com
Introduction
Why this session
My Collection of Scripts
Where did I leave these again?
Problem? Solution!
Talking points
• Best practices
• PowerShell to PowerShell Core
• Cross Platform
• ISE to Visual Studio Code
• Git
• Pester
Best Practices
And style rules
lowercase, PascalCase, UPPERCASE
• lowercase: Powershell Keywords
• foreach, switch, return, -eq, -like,…
• PascalCase: All public identifiers
• Functions, Modules, Cmdlets, Attributes, Classes,…
• Do-Something, $SomeVariable, MyModule.psm1,…
• UPPERCASE: Comment-based help keywords
• .SYNOPSIS, .EXAMPLE, .DESCRIPTION,…
“One True Brace Style”
• Brackets open at the end of a line.
• Brackets close at the beginning of a line.
• Exception: short CommandBlocks.
Verb-Noun
• All PowerShell Modules, Functions and Cmdlets follow this rule.
• Get-Process, New-LocalUser, Clear-Content,…
• Follow this rule when creating functions.
• Repair-Wmi, Add-DomainUserToLocalAdmins, Get-
RootCertificatesFromComputer
• List of approved Verbs:
• Get-Verb
Aliases
• Aliases are cool and all, but…
• … make code hard to understand.
• So try to avoid them.
• (Unless you are playing Code Golf)
Comments
• Comments should be useful.
• Not useful:
• Useful:
Other Best Practices
Other Best Practices
• Re-usability
Other Best Practices
• Re-usability
• Modularity
Other Best Practices
• Re-usability
• Modularity
• Don’t re-invent the wheel
Other Best Practices
• Re-usability
• Modularity
• Don’t re-invent the wheel
• Write-*
PowerShell to PS Core
Moving to an open-source, cross-platform version
What’s broken?
Solution!
What’s broken?
Solution!
What’s broken?
Solution!
Solution!
What’s broken?
Solution?
PowerShell Core keeps evolving…
What about Linux?
Cross-Platform PowerShell
PowerShell comes in a lot of flavors
Remote Connection to Linux
Remote Connection to Linux
Windows vs Linux
ISE vs Code
Out with the old, in with the new
In Memoriam
Welcome Visual Studio Code
Main advantages
• Workspaces, folders, and files
• Add-ons
• Themes
• Cross Platform
• Debugging
• Multiple terminals
• Integrated GIT
• Not just for PowerShell
• It’s just cooler
Git
And GitHub for System Engineers
“A solution for growing pains”
G-I-T
• Random three-letter combination that is pronounceable, and not actually
used by any common UNIX command. The fact that it is a
mispronunciation of "get" may or may not be relevant.
• Stupid. Contemptible and Despicable. Simple. Take your pick from the
dictionary of slang.
• “Global information tracker": you're in a good mood, and it actually works
for you. Angels sing, and a light suddenly fills the room.
• “Goddamn idiotic truckload of sh*t": when it breaks
Pester
Testing PowerShell, using PowerShell
Pester
• Test Framework
• For PowerShell
• Written in PowerShell
• DSL Syntax:
• “Describe”
• “It”
• “Should”
Type of Tests
• Unit Testing
• In isolation (using $TestDrive)
• No external code (using Mocks)
• Integration Testing
• “White/Clear Box Testing”
• Integration with external code
• Acceptance Testing
• “Black Box Testing”
• Third party
T h a n k y o u !
Resources
• https://github.com/PoshCode/PowerShellPracticeAndStyle
• https://poshcode.gitbooks.io/powershell-practice-and-style/
• https://docs.microsoft.com/en-us/powershell/scripting/whats-new/breaking-changes-ps6?view=powershell-6
• https://4sysops.com/archives/make-visual-studio-code-look-and-behave-like-powershell-ise/
• https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/ssh-remoting-in-powershell-
core?view=powershell-6
• https://github.com/PowerShell/psl-omi-provider
• https://lkml.org/lkml/1998/9/30/122
• https://guides.github.com/activities/hello-world/
• https://github.com/pester/Pester
• https://www.red-gate.com/simple-talk/sysadmin/powershell/introduction-to-testing-your-powershell-code-with-
pester/

Modernizing PowerShell Scripts