Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Revise PowerShell Script

Posted on 2013-01-24
3
Medium Priority
?
293 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
[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
3 Comments
 
LVL 22

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 71

Accepted Solution

by:
Qlemo earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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
A brief introduction to what I consider to be the best editor for 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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 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