Specify Credentials to run Powershell Script to Query AD
- by Ben
I want to run a powershell script to query AD from a machine that is NOT on the domain.
Basically I want to query to see if there is computer account already on the domain for this machine and create it if there is not. Because this has to happen before the machine joins the domain I assume I will need to specify some credentials to enable it to run. (I'm pretty new to Powershell, so apologies if this is a newbie question!)
The script I am using to check the account is below, and then once this has run it will join the domain using the computername specified.
Can you tell me how to specify some domain credentials to run this section of the script as?
Cheers,
Ben
$found=$false
$thisComputer = <SERVICE TAG FROM BIOS>
$ou = [ADSI]"LDAP://OU=My Computer OU,DC=myDomain,DC=com"
foreach ($child in $ou.psbase.Children ) {
if ($child.ObjectCategory -like '*computer*') {
If ($child.Name -eq $thisComputer) {
$found=$true
}
}
}
If ($found) { <DELETE THE EXISTING ACCOUNT> }