?
Solved

powershell script structure

Posted on 2011-09-22
1
Medium Priority
?
787 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 2000 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

649 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