Adding user in AD using powershell

64 Views Asked by At

I want to create a new computer and want to add that computer in AD domain. Below is the script i want to execute. Can someone tell where should create credential object for SRV1 in Domain Controller or in Computer? If i should create credential object for SRV1 in Domain Controller than how Computer will 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

I want to create a new computer and want to add that computer in AD domain. Below is the script i want to execute. Can someone tell where should create credential object for SRV1 in Domain Controller or in Computer? If i should create credential object for SRV1 in Domain Controller, than how Computer will get value of CredSRV1 which is used in Joining the computer to the domain?

0

There are 0 best solutions below