?
Solved

powershell script structure

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

Bringing Advanced Authentication to the SMB Market

WatchGuard announces the acquisition of advanced authentication provider, Datablink, with one mission – to bring secure authentication to SMB, mid-market, and distributed enterprises with a cloud-based solution, ideal for resale via their established channel & MSSP community.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

752 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