Specify Credentials to run Powershell Script to Query AD

Posted by Ben on Server Fault See other posts from Server Fault or by Ben
Published on 2010-05-25T10:19:08Z Indexed on 2010/05/25 10:21 UTC
Read the original article Hit count: 391

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> }

© Server Fault or respective owner

Related posts about active-directory

Related posts about powershell