Deployment: Check AD for computername else generatecomputername with prefix

Hello, I’m a noob to powershell. I am trying to Check AD for computername if there is none based one the UUID then generatecomputername with prefix direct to AD. But i can’t seem to figure it out.
I got the following script which i want to keep building on:

$ComputerModel = (Get-WmiObject -Class Win32_ComputerSystem | Select-Object Model).Model
$SerialNumber = (Get-WmiObject -Class Win32_BIOS | Select-Object SerialNumber).SerialNumber
$UUID = (Get-WmiObject Win32_ComputerSystemProduct | Select-Object -ExpandProperty UUID)UUID

Workstations

if (($ComputerModel -match “Precision”) -or ($ComputerModel -match “Optiplex”))
{
$OSDComputerName = $SerialNumber + “-” + “WS”
$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$TSEnv.Value(“OSDComputerName”) = " $OSDComputerName
}

Laptops

if ($ComputerModel -match “HP Compaq nc6320”)
{
$OSDComputerName = $SerialNumber + “-” + “LAP”
$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$TSEnv.Value(“OSDComputerName”) = " $OSDComputerName
}

You can simplify your WMI calls

$ComputerModel = Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty Model

$SerialNumber = Get-WmiObject -Class Win32_BIOS | Select-Object -ExpandProperty SerialNumber

$UUID = Get-WmiObject Win32_ComputerSystemProduct | Select-Object -ExpandProperty UUID

Not sure what you are trying to achieve after that as Microsoft.SMS.TSEnvironment is for working with Configuration Manager Task Sequences not AD

In any event the code you are using has some potential issues and I think it should look like this

# Workstations if (($ComputerModel -match "Precision") -or ($ComputerModel -match "Optiplex")) { $OSDComputerName = $SerialNumber + "-" + "WS" $TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment $TSEnv.Value("OSDComputerName") = $OSDComputerName } # Laptops if ($ComputerModel -match "HP Compaq nc6320") { $OSDComputerName = $SerialNumber + "-" + "LAP" $TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment $TSEnv.Value("OSDComputerName") = $OSDComputerName }

You could use a switch statement instead or an if-else but lets leave that until we’ve sorted your problem with AD

Can you show the code where you are accessing AD?

Thanks for the reply! That’s another problem i am having. I can’t seem to import the AD module in winpe, do you have any ideas?

Thanks for the reply! That’s another problem i am having. I can’t seem to import the AD module in winpe, do you have any ideas?

I’m not 100% sure but I don’t think you can import extra PowerShell modules into winpe