Friday, March 21, 2014

Provisioning computers in Active Directory using Powershell

This post will show you how to be able to provisioning/pre-staging computers from a list by using a Powershell script. You need to specify the computer name and the MAC adress in the csv file and after that you can execute the script.

You have to modify the orange text in the script to your AD dn. If you remove this part computers will be created in the "Computers" OU.


Powershell Script

import-module Activedirectory

import-csv "C:\PSScripts\Computer.csv" | foreach-object{

    $name = $_.Name
    $MAC = $_.Mac 
  
    $CheckExists = get-adobject -Filter {(ObjectClass -eq "computer") -and (Name -eq $name)}  #it´s $NULL if the computer doesn´t exist
  
      
    if ($CheckExists -eq $NULL){
        [guid]$nbGUID = "00000000-0000-0000-0000-$MAC"  # transform MAC to netbootGUID
        new-adcomputer -Name $name -SamAccountName $name -Path "OU=Systems,DC=directoryadmin,DC=blogspot,DC=com" -OtherAttributes @{'netbootGUID'=$nbGUID}       
        write-host $name " - " $nbGUID
      }
      else {
        write-host "$name already in use."
        }
      
}


Computer.csv located in C:\PSScripts\

Name,Mac
computer1,A088B44642A8
computer2,A088A44642B8