SOLVED

Verify if the Computer Name in AD during OSD

Copper Contributor

Hi

 

I am trying to use this in my task sequence to verify if the computer object name already exists in AD. This script works absolutely fine from a machine already joined to the domain. I am trying to execute the below script during the WinPE stage, but end up with error in below step in the script.

 

 

# Create an object "DirectoryEntry" and specify the domain, username and password
$Domain = New-Object -TypeName System.DirectoryServices.DirectoryEntry
-ArgumentList $DomainDN,$($Credential.UserName),$($Credential.GetNetworkCredential().password)

 

 

 

Error encountered is:

Format-default : The following exception occurred while retrieving member distinguishedName: Unknown error 0x80005000

 

Complete Script:

 

#Computer hostname
$varCompName = "W1022H2"

#Define the Credential
$Credential = Get-Credential -Credential Tucson\Administrator

# Create an ADSI Search
$Searcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher

# Searchh fiter to computers only
$searcher.filter = “(&(objectClass=computer)(sAMAccountName= $($varCompName)$))”

# Get the current domain
#$DomainDN = $(([adsisearcher]"").Searchroot.path)
#$DomainDN = $(([adsisearcher]"sAMAccountName=$($varCompName)$").FindOne().path) 
$DomainDN = 'LDAP://DC=Tucson,DC=com'

# Create an object "DirectoryEntry" and specify the domain, username and password
$Domain = New-Object `
 -TypeName System.DirectoryServices.DirectoryEntry `
 -ArgumentList $DomainDN,$($Credential.UserName),$($Credential.GetNetworkCredential().password)

# Add the Domain to the search
$Searcher.SearchRoot = $Domain

# Execute the Search
$Searcher.FindAll()

 

 

 

Kindly advice how can i correct/tweak the script to work during WinPE. Thank you 

 

 

 

 

 

2 Replies

Hello @RameshRK, did you manage to get this working? I'm facing a similar issue. Thanks 

best response confirmed by RameshRK (Copper Contributor)
Solution
Hi

Get following dlls for AD module:
Microsoft.ActiveDirectory.Management.dll
Microsoft.ActiveDirectory.Management.resources.dll

Import AD module in the beginning of the script:
Import-module Microsoft.ActiveDirectory.Management.dll
Import-module Microsoft.ActiveDirectory.Management.resources.dll

Then got some ideas from the below good articles to complete my required script:
https://gist.github.com/davegreen/453cee5ef2db1063a007
https://msendpointmgr.com/2013/10/02/prompt-for-computer-name-during-osd-with-powershell/

Hope this helps.
1 best response

Accepted Solutions
best response confirmed by RameshRK (Copper Contributor)
Solution
Hi

Get following dlls for AD module:
Microsoft.ActiveDirectory.Management.dll
Microsoft.ActiveDirectory.Management.resources.dll

Import AD module in the beginning of the script:
Import-module Microsoft.ActiveDirectory.Management.dll
Import-module Microsoft.ActiveDirectory.Management.resources.dll

Then got some ideas from the below good articles to complete my required script:
https://gist.github.com/davegreen/453cee5ef2db1063a007
https://msendpointmgr.com/2013/10/02/prompt-for-computer-name-during-osd-with-powershell/

Hope this helps.

View solution in original post