Solved

powershell script structure

Posted on 2011-09-22
1
765 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
A procedure for exporting installed hotfix details of remote computers using powershell
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

717 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