Solved

powershell script structure

Posted on 2011-09-22
1
744 Views
Last Modified: 2012-05-12
Are there any recommended "best practices" for structuring powershell scripts to avoid very lengthy, spaghetti-like code.
0
Comment
Question by:AlStoughton
1 Comment
 
LVL 16

Accepted Solution

by:
Dale Harris earned 500 total points
ID: 36581712
Create functions whenever possible.  You'll find that functions help clean up your code into blocks of manageable data.

Stick with a structure that is easy to read.  One liners are cool, but you should save those really cool one liners for console stuff.  You should always space out your code to help other people see it clearly for what it does.

Comment before huge blocks of code.  Sometimes the comments you see are every other line like #I'm about to write to the host.  That's a little too much I think and creates more clutter.

Define variables at the top of your script.  That way if anyone comes in after you, they can just modify one section and be able to use your script.

Always code like you think someone else will have to read it and you'll have to explain it to them with comments, not talking with them personally.

And that's not just so you can have other people look at your code.  Have you ever tried looking at some abstract script you wrote while on a caffeine high during the wee hours of a night 6 months after you wrote it?  Sometimes you can confuse yourself, so always code clearly and define anything murky that might cause someone to stumble or ask questions.

That's all I think think of right now.  Have you already checked out the articles in EE for Powershell?  There's some good ones in there.

HTH,

Dale Harris
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article will help you understand what HashTables are and how to use them in PowerShell.
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now