Another simple task using windows PowerShell.
Problem Statement –
Problem Statement –
How to make a computer join a domain using powershell script?
Firstly, we will get the Domain class object using powershell with required domain values. Then we will retrieve the current computer domain name.
If the current computer domain name matches with Domain class object values; we can say that computer is joined to the required domain. If matching condition fails then, powershell to join the domain will be executed.
#set execution policy
set-executionpolicy remotesigned
#Get required domain object based on domain information
$domtype = [System.DirectoryServices.ActiveDirectory.DirectoryContexttype]"Domain"
$domain = "A3.gmd.lab"
$domuser = "A3\Administrator"
$dompass = "Acc1234#"
$context = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext($domtype, $domain, $domuser, $dompass)
$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($context)
write-host "Domain: $dom `r`n"
#get computer's domain
$Computer = Get-WmiObject -Class Win32_ComputerSystem
# Print Computer Name
"Computer Name is: {0}" -f $Computer.Domain
#compare the values of domains retrieved above
if($dom.Name -eq $Computer.Domain)
{
write-host "Domain joined: $domain `r`n"
}
else
{
write-host "!! ERROR: No Domain connected `r`n"
$comp = get-wmiobject -class win32_computersystem
$comp.joindomainorworkgroup("A3.gmd.lab","Acc1234#","a3\administrator",$null,3)
$reboot = (gwmi -class win32_operatingsystem)
$reboot.psbase.scope.options.enableprivileges = $true
$reboot.reboot()
}
Cheers…
Please give food to all my fishes swimming at the bottom. It's fun!! Try it!!
Thanks for reading!!
Happy Coding!!Please give food to all my fishes swimming at the bottom. It's fun!! Try it!!
Thanks for reading!!
No comments:
Post a Comment