I want to create a new computer object in an AD domain.
Below is the script I am using. Can someone tell where should create credential object for SRV1? In domain controller or in computer? If credential object for SRV1 is created in domain controller, how will computer get value of CredSRV1 which is used in joining the computer to the domain?
#Creating a new computer in the domain
$NCHT = @{
Name = 'User'
DNSHostName = 'user.domain.com'
Description = ''
Path = 'OU=xyz,DC=domain,DC=com'
}
New-ADComputer @NCHT
#Creating a credential object for SRV1
$ASRV1 = 'SRV1/Administrator'
$PSRV1 = 'P@SSW0RD'
$PSSRV1 = ConvertTo-SecureString -String $PSRV1 -AsPlainText -Force
$CredSRV1 = [pscredential]::New($ASRV1,$PSSRV1)
#Creating a script to join SRV1
$SB = {
$ARK = 'domain/Administrator'
$PRK = 'P@SSW0RD'
$PSRK = ConvertTo-SecureString - String $PRK -AsPlainText -Force
$CredRK = [pscredential::New($ARK,$PSRK)
$DJHT = @{
DomainName = 'domain.com'
OUPath = 'OU=xyz,DC=domain,DC=com'
Credential = $CredRK
Restart = $false
}
Add-Computer @DJHT
}
#Joining the computer to the domain
Set-Item -Path WSMan:\localhost\Client\TrustedHosts -Value '*'
Invoke-Command -ComputerName SRV1 -Credential $CredSRV1 -Force
#Restarting SRV1
Restart-Computer -ComputerName SRV1 -Credential $CredSRV1 -Force
#Viewing the resulting computer accounts for domain
Get-ADComputer -Filter * -Properties DNSHostName, LastLogonDate | Format-Table -Property Name, DNSHostname, Enabled