© Adam Bertram 2020
A. BertramBuilding Better PowerShell Codehttps://doi.org/10.1007/978-1-4842-6388-4_13

13. Stick to PowerShell

Adam Bertram1  
(1)
Evansville, IN, USA
 

PowerShell is a forgiving scripting language, and that’s part of its appeal. When you’re working with PowerShell, you can borrow code from C#, .NET, or even COM objects and integrate them seamlessly into your code. This sounds great in theory, but when it comes to working on your code in a team of PowerShell developers, it’s best to stick to PowerShell. By straying from it, you could end up leaving your team confused, making your code less understandable, and really giving the next person who needs to edit your scripts a hard time.

Use Native PowerShell Where Possible

With PowerShell, you have a nearly limitless toolset. If you need to accomplish something outside of what you can find PowerShell commands for, you have the option to use COM objects, .NET classes, and so on. That being said, don’t immediately jump to using these “non-PowerShell” methods. When there’s an existing PowerShell way of doing something, use it! There’s a strong chance that it’ll process better, faster, or give you an output that’s easier to use in the end.

Let’s say you’re a seasoned C# developer in charge of writing a script for your team. Your team consists of system administrators and perhaps a junior dev here and there. No one quite understands C# as you do. You come across a need to read a file. Easy enough, you think. You know how you’d do this in C# and you know you can do a lot of “C sharpy” things in PowerShell so you write a line like this:
$text = [System.IO.File]::ReadAllText('C: extfile.txt')
## Do some stuff with the string in $text here

You test the script, it reads the file as expected and you move on. However, a few days go by and maybe someone else on your team opens up the script or you decide to share it with the PowerShell community. Soon someone finds a bug and begins to attempt to troubleshoot the problem and see this line. Not having any programming knowledge at all, they immediately are stuck and much dive into Google figuring out what this means.

By simply replacing the preceding line Get-Content, this problem wouldn’t have happened. You are now reading this file the “PowerShell way” making it much easier to understand for a programming layperson.
$text = Get-Content -Path 'C: extfile.txt' -Raw
## Do some stuff with the string in $text here

Only revert to calling .NET methods rather than PowerShell commands if you require the highest performance possible or there is no PowerShell command available to do what you need to.

Tip Source: https://twitter.com/JimMoyle

Further Learning

Use Approved, Standard Function Names

PowerShell allows you to assign any name to a function you’d like. You can call a function foo, do-thing, or Get-Thing. PowerShell doesn’t care as long as you call that function by the assigned name. Even though you can assign whatever name you’d like doesn’t mean you should.

PowerShell has preferred naming standard you should follow for all of your functions. This standard specifies that all commands should start with a verb, a dash, and followed by a singular noun such as Get-Thing, Start-Service, Invoke-Command, and so on. You are free to use whatever noun you’d like that makes sense, but you should only use “approved verbs.”

PowerShell strongly encourages you to use an “approved verb” or any verb you see when you run the Get-Verb cmdlet. Use any of these verbs that make sense for your situation.

Once you find a verb that fits, assign a singular noun. Don’t assign a plural noun like Get-Things. Although your function may, in fact, get things (plural), does it always? When you use the pipeline, your function isn’t technically getting multiple things; it’s processing them one at a time. Always use a singular noun to provide an accurate name of what your function does.

Further Learning

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.154.41