Solved

Revise PowerShell Script

Posted on 2013-01-24
3
281 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
Comment Utility
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
Comment Utility
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 21

Author Closing Comment

by:Joseph Moody
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

17 Experts available now in Live!

Get 1:1 Help Now