Solved

Revise PowerShell Script

Posted on 2013-01-24
3
282 Views
Last Modified: 2013-01-25
I have a script (that came from a previous question). This script creates new computers off of an existing computer template.

As I am still working with PowerShell, I would like to see how it can be approved in efficiency or syntax. The script is currently working but I am sure it can be better.

If it can be improved, can you also explain why an improvement should be made?

Add-PSSnapin Quest.ActiveRoles.ADManagement


$SourceComputer=Read-Host "What will be the computer template?"
$Computer=Get-QADComputer $SourceComputer
$ParentContainer=$Computer.ParentContainer
$Groups=Get-QADComputer $SourceComputer | Get-QADMemberof


$prefix = Read-Host "What is the computer prefix? EX: GAMCN or GAMCLABN"
try { [int]$startnumber = Read-Host "What number would you like to start with? EX: 01 or 13" -ea "Stop"}
catch {"Need to enter a number"; break}
try { [int]$number = Read-Host "How many computer accounts do you want to create?" -ea "Stop"}
catch {"Need to enter a number"; break}
ForEach ($i in ($startnumber..($startnumber + $number -1)))
{
  $compName = $prefix + $i
  New-QADComputer -name $compName -ParentContainer $ParentContainer
  
  foreach ($Group in $Groups) {
  Add-QADGroupMember $Group $compName}

} 

Open in new window

0
Comment
Question by:Joseph Moody
3 Comments
 
LVL 21

Expert Comment

by:mcsween
ID: 38816014
This code looks pretty good to me; I can't really see any improvements to be made.  In VBScript this would be twice as long.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 38816680
The only improvement I can think of is to use less variables. Whenever possible, intermediate objects should not be kept in vars, to allow for lean memory usage. It doesn't matter here, as the vars are "static", but as a rule of thumb, as soon as you are processing a bunch of objects. Your lines 4 to 7 would then look like this:
$SourceComputer = Read-Host "What will be the computer template?"
$ParentContainer = (Get-QADComputer $SourceComputer).ParentContainer
$Groups = Get-QADComputer $SourceComputer | Get-QADMemberof

Open in new window

I don't know if there is any effect, but it might perform better if you collect the generated PC names, and stuff them into a single Add-QADGroupMember call for each group. Starting from line 15:
$comps = @()
ForEach ($i in ($startnumber..($startnumber + $number -1)))
{
  $comps += ($compName = $prefix + $i)
  New-QADComputer -name $compName -ParentContainer $ParentContainer
}

foreach ($Group in $Groups) {
  Add-QADGroupMember $Group $comps
}

Open in new window

0
 
LVL 22

Author Closing Comment

by:Joseph Moody
ID: 38818390
Thank you very much for the feedback!

I had thought that lines 4-7 could be condensed but couldn't figure out how.
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

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This article will help you understand what HashTables are and how to use them in PowerShell.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

22 Experts available now in Live!

Get 1:1 Help Now