четверг, 16 января 2020 г.

SCRIPTS BEST PRACTICES

POWERSHELL SCRIPTS

1) Use full cmdlet in your scripts and alias in an interactive console (never use alias in your scripts).

2) Use named parameter in scripts (not positional and partial parameter).
Named ParameterCopy-Item -Path C:\tmp\Demo.txt -Destination C:\tmp\DemoCopy.txtPositional parameterCopy-Item C:\tmp\Demo.txt C:\tmp\DemoCopy.txtPartial parameterCopy-Item -Pat C:\tmp\Demo.txt -De C:\tmp\DemoCopy.txt

3) Avoid Write-Host (unless your goal is to write to the host only).
Write-Host is like a picture sent to the screen, it sends to the host and does not return any objects, it is not possible to export or convert in a specific format. Write-Host has too many limitations.In some rare cases, you could use Write-Host if you want to display text in a specific color (ForegroundColor) or with specific background (BackgroundColor) only on the screen (ex: display in color some specific lines of a log, etc.).But anyway, Write-Host is not needed most of the time.

4) Use CIM cmdlet (not WMI cmdlet). no additional development or improvement is expected.

5) Avoid excessive comments (over-commenting)

6) Use WhatIf and Confirm parameters when working with the commands modifying the system state.
The WhatIf common parameter is used for “Simulation”, “Read-Only”: no action is performed, no write, no impact. Useful for simulation.The Confirm common parameter is used to ask to the user to confirm that the action can be performed. Useful for confirmation.

7) Avoid empty Catch block
try{    Get-ADUser -Identity AccountNotExist}catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{    Write-Warning -Message 'Account not found'}



COMMON PRACTICES

1) Use appropriate naming conventions for variables, scripts and routines. Conform to the conventions of the language, the rest of your site's code base and most importantly, the other variables and functions defined in the current project. Use mixed case or underscores to make long names readable. The naming of the scripts themselves is important, too. In this context, dashes are more common than underscores for simulating spaces

2) Use variable names that reflect the values they store, but keep them short

3) Start every script with a comment block that tells what the script does and what parameters it takes. Include your name and the date. If the script requires nonstandard tools, libraries or modules to be installed on the system, list those as well.

4) Don't store your credentials in shell scripts, people can have access to your script. Use ENV variables or vaults instead.

CLEAN CODE PRINCIPALS

1) KISS. Keep It Simple Stupid. Unnecessary complexity should be avoided. The question to ask when you're writing code is "can this be written in a simpler way?"

2) DRY. Don't Repeat Yourself.  It states that every piece of knowledge (code, in this case) must have a single, unambiguous, authoritative representation within a system (codebase).

3) YAGNI: You Aren't Gonna Need It. A developer should not add functionality unless deemed necessary.

4) Favor readability: Particularly when working with multiple people on a project, always favor readability over conciseness.



Sources:
http://powershell-guru.com/

Комментариев нет: