Prestage-PC

Let’s have a look on how we can easily prestage computer accounts with Powershell. Prestaging computer account can be useful if you’re using WDS in your organization to deploy computers. That one script will create a computer account in AD, add Mac address, WDS server name, and unattend information in it which will make your deployment zero touch (apart from turning on the PC and PXE boot of course… ).

Let’s start with the Computer function. Here, we’re first looking if the $computername variable is null or empty, and if it is, ask user to enter a computername.

Then, try/catch is used to verify if a computername with that name exists already or not. If it does exist, we’ll go to our Choice function, else to our ComputerOption function


Function computer ($computername)
{if (!$Computername){$Computername=Read-Host "- Please enter Computername (Capital letter)"}
try{$test=get-adcomputer $Computername
 write-host -ForegroundColor white -backgroundcolor red "$computername already exists in AD, following options are available:"
 Choice -computername $computername
 }
catch {computeroption}
}

Our choice function below will ask you to either enter another computername, delete/create a computer account with the name you provided earlier or exit..

We’ll use a switch here to determine your answer, and perform action accordingly.

function choice ($computername)
{ write-host "1. Select another computer name"
 write-host "2. Delete $computername from AD and continue ($Computername will be deleted and created again)"
 write-host "3. Exit script"
 $Choice=read-host "Choose a number to continue"

switch ($Choice)
 {
 "1" {computer}
 "2" {Remove-ADComputer -identity $Computername ;ComputerOption}
 "3" {Write-Host "Au revoir..."}
 default {exit}
 }
}

Finally, our last function here is ComputerOption. We’ll look into this one in details


switch -wildcard ($computername)
 {
 "AU*" { $server= "AU-SYD-WDS01.ad.Contoso.com";$Country= "AT";$OU ="Australia" }
 "BE*" { $server= "BE-BRU-WDS01.ad.Contoso.com";$Country= "BE";$OU="Belgium"}
 "FR*" { $server= "FR-PAR-WDS01.ad.Contoso.com";$Country= "BE";$OU="France" }
 "UK*" { $server= "UK-LON-WDS01.ad.Contoso.com";$Country= "UK";$OU="United Kingdom" }
 "US*" { $server= "US-WAH-WDS01.ad.Contoso.com";$Country= "UK";$OU="United States" }
 Default {$server= read-host "- can't find WDS server to assign, please enter WDS server FQDN name to use"
 $country = read-host "- Please enter keyboard layout code (eg: BE, FR, US...)"
 $OU = Read-Host "- Please enter full country name" 
 }
 }

Our switch here allow us to set the $server & $OU variable depending on our computername. This assume of course that our naming convention is a computer in a specific location will start with the country code of that location (ex: FRComputer01 will be a computer based in France). This allow us to know which WDS server will be use to deploy that PC, in which OU it will be located, and which Keyboard Layout/Windows language will be set.


$Mac= read-host "- Please type in MAC address (no space, no -)"
 do{if ($Mac.Length -ne 12){$mac=read-host "Incorrect Mac Address, Mac must be 12 digits exactly, try again" } 
 }
 until ($Mac.Length -eq 12 )

so here, we’ll simply ask user to enter the 12 digits of the MAC address of the computer .


$model = read-host "- Please type in PC Model (eg:Dell E7440, HP X360, Atari ST2600,...)"

switch -wildcard ($model)
{
"*E7440*" {$WDSunattend= "WDSunattend_"+"$Country"+"_E7440.xml"}
"*X360*" {$WDSunattend= "WDSunattend_"+"$Country"+"_X360.xml"}
"*E7470**" {$WDSunattend= "WDSunattend_"+"$Country"+"_E7470.xml"}
"*ST2600*" {$WDSunattend= "WDSunattend_"+"$Country"+"_ST2600.xml"}
Default {write-host -Foregroundcolor yellow "Standard model unattend will be used"
$WDSunattend= "WDSunattend_"+"$Country"+".xml"
}
}

We know need to know the model of the computer so we can assign a specific unattend file to the computer account that will be use to deploy the computer. So we now have all information we need to prestage that computer.


$macaddr= "00000000-0000-0000-0000-"+"$mac"
$computername = $computername.ToUpper()
Write-Host -ForegroundColor yellow "creating $Computername account in AD..."
do{try {$createPC=New-ADComputer -Enabled $true -Name $Computername -Path "OU=$OU,OU=ContosoComputers,DC=ad,DC=Contoso,DC=com" -SamAccountName $Computername -ErrorAction $ErrorActionPreference ;$Result="yes"}
catch {write-host -ForegroundColor white -BackgroundColor Red "Issue creating computer account, will try again in 5sec..." ; sleep 5}}
until ($Result -like "*yes*")
write-host -foregroundcolor green "$Computername successfully created"
write-host -ForegroundColor yellow "Configuring WDS option..."
do{try {$setPC=Set-ADComputer $Computername -Add @{netbootGUID=([GUID]"$macaddr").tobytearray();netbootMachineFilePath="$server";netbootMirrorDataFile="BootImagePath=Boot\x64\Images\64bit_boot.wim;WdsUnattendFilePath=WdsClientUnattend\"+"$WDSunattend"+";Group=;JoinDomain=1;PxePrompt=0;"} -ErrorAction $ErrorActionPreference ;$Result="yes"}
catch {write-host -ForegroundColor white -BackgroundColor Red "Issue setting up WDS option on $Computername, will try again in 5sec..." ; sleep 5}}
until ( $Result -like "*yes*" )
write-host -ForegroundColor Green "$Computername has been successfully configured"
if (!(Test-Path $file)){Out-File -FilePath $file -Encoding ascii -Force ; Add-Content -Path $file -value "Computername,Mac,Model,FileServer,Unattend,Keyboard" }
add-content -Path $file -value "$Computername,$Mac,$Model,$server,$WDSunattend,$Country"

In that last part, we first create the computer account. We use a try/catch in case there is an error, and use a do/until to retry creating the computer account in case of error. We then add the mac address, our bootimage / unattend information and WDS server name that will be use for deployment. We also use try/catch and do/until to handle error here. Finally, we populate a text file with the different value to keep a track of our created computer.

We could add a foreach loop in case we have a large number of computers to create, if they are all in a CSV file. To avoid changing too much in the original script, I will only take the last bit of the script, and add a foreach like this :


$importfile = Import-Csv -Delimiter "," -Path "C:\MyCSVFile.csv"
foreach ($Computer in $importfile)
{
$Computername = $Computer.name
$mac = $computer.mac
$server = $Computer.server
$country= $computer.country
$ou = $Computer.ou
$model = $computer.model
}

Below is the full script, hope this can help some prestaging computer account  faster.

param ([string]$Computername)
$file= "$env:USERPROFILE\Documents\PrestagePCLog.csv"
$ErrorActionPreference= "SilentlyContinue"
Import-Module activedirectory

 

Function computer ($computername)
{if (!$Computername){$Computername=Read-Host "- Please enter Computername (Capital letter)"}
try{$test=get-adcomputer $Computername
write-host -ForegroundColor white -backgroundcolor red "$computername already exists in AD, following options are available:"
Choice -computername $computername
}
catch {computeroption}
}

Function ComputerOption
{
switch -wildcard ($computername)
{
"AU*" { $server= "AU-SYD-WDS01.ad.Contoso.com";$Country= "AT";$OU ="Australia" }
"BE*" { $server= "BE-BRU-WDS01.ad.Contoso.com";$Country= "BE";$OU="Belgium"}
"FR*" { $server= "FR-PAR-WDS01.ad.Contoso.com";$Country= "BE";$OU="France" }
"UK*" { $server= "UK-LON-WDS01.ad.Contoso.com";$Country= "UK";$OU="United Kingdom" }
"US*" { $server= "US-WAH-WDS01.ad.Contoso.com";$Country= "UK";$OU="United States" }
Default {$server= read-host "- can't find WDS server to assign, please enter WDS server FQDN name to use"
$country = read-host "- Please enter keyboard layout code (eg: BE, FR, US...)"
$OU = Read-Host "- Please enter full country name"
}
}

$Mac= read-host "- Please type in MAC address (no space, no -)"
do{if ($Mac.Length -ne 12){$mac=read-host "Incorrect Mac Address, Mac must be 12 digits exactly, try again" }
}
until ($Mac.Length -eq 12 )

$model = read-host "- Please type in PC Model (eg:Dell E7440, HP X360, Atari ST2600,...)"
switch -wildcard ($model)
{
"*E7440*" {$WDSunattend= "WDSunattend_"+"$Country"+"_E7440.xml"}
"*X360*" {$WDSunattend= "WDSunattend_"+"$Country"+"_X360.xml"}
"*E7470**" {$WDSunattend= "WDSunattend_"+"$Country"+"_E7470.xml"}
"*ST2600*" {$WDSunattend= "WDSunattend_"+"$Country"+"_ST2600.xml"}
Default {write-host -Foregroundcolor yellow "Standard model unattend will be used"
$WDSunattend= "WDSunattend_"+"$Country"+".xml"
}
}
$macaddr= "00000000-0000-0000-0000-"+"$mac"
$computername = $computername.ToUpper()
Write-Host -ForegroundColor yellow "creating $Computername account in AD..."
do{try {$createPC=New-ADComputer -Enabled $true -Name $Computername -Path "OU=$OU,OU=ContosoComputers,DC=ad,DC=Contoso,DC=com" -SamAccountName $Computername -ErrorAction $ErrorActionPreference ;$Result="yes"}
catch {write-host -ForegroundColor white -BackgroundColor Red "Issue creating computer account, will try again in 5sec..." ; sleep 5}}
until ($Result -like "*yes*")
write-host -foregroundcolor green "$Computername successfully created"
write-host -ForegroundColor yellow "Configuring WDS option..."
do{try {$setPC=Set-ADComputer $Computername -Add @{netbootGUID=([GUID]"$macaddr").tobytearray();netbootMachineFilePath="$server";netbootMirrorDataFile="BootImagePath=Boot\x64\Images\64bit_boot.wim;WdsUnattendFilePath=WdsClientUnattend\"+"$WDSunattend"+";Group=;JoinDomain=1;PxePrompt=0;"} -ErrorAction $ErrorActionPreference ;$Result="yes"}
catch {write-host -ForegroundColor white -BackgroundColor Red "Issue setting up WDS option on $Computername, will try again in 5sec..." ; sleep 5}}
until ( $Result -like "*yes*" )
write-host -ForegroundColor Green "$Computername has been successfully configured"
if (!(Test-Path $file)){Out-File -FilePath $file -Encoding ascii -Force ; Add-Content -Path $file -value "Computername,Mac,Model,FileServer,Unattend,Keyboard" }
add-content -Path $file -value "$Computername,$Mac,$Model,$server,$WDSunattend,$Country"
pause
}

function choice ($computername)
{ write-host "1. Select another computer name"
write-host "2. Delete $computername from AD and continue ($Computername will be deleted and created again)"
write-host "3. Exit script"
$Choice=read-host "Choose a number to continue"

switch ($Choice)
{
"1" {computer}
"2" {Remove-ADComputer -identity $Computername ;ComputerOption}
"3" {Write-Host "Au revoir..."}
default {exit}
}
}

computer -computername $computername

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s