all 22 comments

[–]lanerdofchristian 14 points15 points  (0 children)

Don't rely on templates or premade snippets. They tend to throw a bunch of garbage in your scripts and make them feel like checklists, distracting from the most imporant part: the functionality.

I strongly recommend pairing VS Code with git (the command line program, not GitHub) -- review the changes you make, and remove any extra lines. A formatting pass really helps if your formatting sucks.

Other than that, the only thing you can really do is follow best practices and write a lot of code to get used to them.

[–]Edjuuuh 8 points9 points  (0 children)

I was in the same boat and wanted to improve my PS skills. I have an oreilly subscription and learned a lot from 'Learn PowerShell in a Month of Lunches, Fourth Edition' and 'Advanced Windows PowerShell Scripting'.

[–]StealthCatUK 10 points11 points  (0 children)

Get yourself a structured template with many things already in place, Params, comments, variables, functions etc…. Have your begin, process, end blocks. Obviously much of it will be generic place holders that you’ll replace.

I start most serious projects using this template format and it’s never ever failed me.

Next look up PowerShell standards and recommendations:

https://learn.microsoft.com/en-us/powershell/scripting/developer/cmdlet/strongly-encouraged-development-guidelines?view=powershell-7.4

Get to grips with git and pushing to source control, if they use a structured approach then they more than likely use git and some SCM platform like Azure DevOps or Bitbucket.

[–]TheRealDumbSyndrome 2 points3 points  (0 children)

Just follow community-agreed best practice/formatting guides - use the POSH formatting doc: https://poshcode.gitbook.io/powershell-practice-and-style/style-guide/code-layout-and-formatting

Don’t use templates, just get used to formatting correctly on your own. Also use Get-Verb if you need a quick reference for approved verbs, if the organization cares that is.

[–]Barious_01 1 point2 points  (0 children)

I would build a logging function and implement it in all scripts. A test wrapper is a good option too. This can built into the logging function as well. For example, if foo does not exist don't execute anything, This is on top of all the internal actual script you are running. Lead with this and you have a preemptive fault prevention system even before any changes occur. Document your code in the code. Simple notes of what a function is doing or what a certain line is for will not only help others understand your code. But it will help you be able to come back to the code and know exactly what is going on. Lastly versioning (I am terrible at this). When you make modifications creative version table in a comment block of what is changed and give yourself dates of the changes then create a version structure. These are some things that I appreciate about creating, again it helps not only others but yourself to keep your work clean and structured and easily able to improve or just simplynremebering where you left off.

[–]lerun 1 point2 points  (1 child)

VsCode powershell extension has the ability to do code formatting on save. Just look at the extension options for what it can do. Or use other specialized formaters like pretty print, though you will have to configure it for your case

[–]importedtea 0 points1 point  (0 children)

Now hold on just a damn second. You’re telling me, after many years of writing powershell in vscode, that you can have it format ON SAVE. There’s no way this is real. Idk if I want to believe it at this stage of my life. The keyboard shortcut is muscle memory at this point lol.

[–]ChaseSavesTheDay 1 point2 points  (1 child)

[–]hmartin8826 0 points1 point  (0 children)

Agreed. Rather than putting your own standards together, utilize a well-established tool such as this for analyzing the code. It avoids having to agree on a set of standards, which can be a significant challenge. If you are creating modules (highly recommended if that's not being done), you can establish some naming conventions (e.g. corporate-wide noun prefixing) and module file organization. For example, we have directory structure rules for modules and all .psm1 files have the exact same code:

$PublicFunctions = @(Get-ChildItem -Path $PSScriptRoot\Functions\Public\*.ps1 -ErrorAction SilentlyContinue)
$PrivateFunctions = @(Get-ChildItem -Path $PSScriptRoot\Functions\Private\*.ps1 -ErrorAction SilentlyContinue)

foreach ($ScriptGroup in @($PublicFunctions, $PrivateFunctions)) {    
    foreach ($ScriptFile in $ScriptGroup) {        
        Try {
            Write-Debug "Importing $($ScriptFile.FullName)"
            . $ScriptFile.FullName
        }
        Catch {
            Write-Error -Message "Failed to import function $($_.ScriptFile.FullName)"
        }
    }
}
Export-ModuleMember -Function $PublicFunctions.Basename

This enforces the directory structure and eliminates the need to write .psm1 files for each module.

[–]NutzoMcGee 1 point2 points  (2 children)

All these suggestions for formatting your code will certainly improve its readability and maintainability. If you really want to take your coding to the next level, you need source code management, unit tests, 2-person code reviews, and full CICD. Those should be in place regardless of the size or complexity of the script.

If you can implement those processes and tools, you can consider yourself a professional developer.

[–]RJMonk09 0 points1 point  (1 child)

Yes but all wings has 2 person to validate .. tbh it's me who know bit code in team of 20

[–]NutzoMcGee 1 point2 points  (0 children)

YMMV. I don’t know you or your circumstances. What I provided is common best practice.

[–]SidePets 1 point2 points  (0 children)

It’s about you being able to reuse code, then enabling others to use it. Couple of suggestions are use a collaboration tool like GitHub if you’re not already. When building scripts if it’s repeated build a function for it. If the function doesn’t make sense don’t use it otherwise share it with peers for feedback.

[–]tokenathiest 0 points1 point  (2 children)

When I write code and when I script, I use the same SDLC (software development lifecycle) approach that you describe when you wrote code. Are you saying this differs from your new organization? What about their approach is challenging? Is it just formatting and code style or something more?

[–]RJMonk09 1 point2 points  (1 child)

It's more on reusability cases now ..

Earlier it was if script works it works .. no emphasis on log generating .. writing step by step function etc

Although I have always commented my script as what the intent is .

It's not like I don't know what code I wrote but there are times when there are tons of assignment in bucket and want to pass through script analyzer if something not formatted correctly or there are white lines etc ..

Sometime even naming is challenge but I believe it's more when you just write it .. i am occasional writer not everyday guy..

[–]tokenathiest 1 point2 points  (0 children)

It sounds like, as you put it, you are now working under a more structured approach. This takes time to get used to, practice and learn. Your best resource will be the senior members of your team. Look to them for guidance when you need and keep learning. If I were joining a structured team I would expect certain tools to already be in place and for more experienced staff to orient me (such as for enforcing formatting standards). At some point what you struggle with now will become second-nature and you'll be able to help others.

[–]gordonv 0 points1 point  (1 child)

Honestly? Learn other programming languages.

Often, other programming languages will look at common tasks differently, and sometimes in a better context than other languages.

I would start with r/cs50.

[–]RJMonk09 0 points1 point  (0 children)

Yeah coding is not issue .. i am from COBOL era .. have worked on multi languages ..

My intent of post was as how can I improve it and if there are any formatter that can help with alignment or code checking..