Link to home
Start Free TrialLog in
Avatar of Joseph Moody
Joseph MoodyFlag for United States of America

asked on

Revise PowerShell Script

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

Avatar of Bradley Fox
Bradley Fox
Flag of United States of America image

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.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Joseph Moody

ASKER

Thank you very much for the feedback!

I had thought that lines 4-7 could be condensed but couldn't figure out how.