VMware Cloud Community
BrianGordon84
Contributor
Contributor
Jump to solution

Add newly created vm to domain

I've been searching the net for days trying to find out if it's possible to add a newly created vm to our domain but I'm running into a few problems. I'm not even sure if this is possible. I have an admin account that I'm running this script under but that doesn't really matter since the template vm's aren't actually a member of the domain so the account doesn't have any privledges. Here's the scrip that I'm using:

$domain = "xxxx"

$domainpw = "xxxxxx"

$username = "xxxxxxx"

function JoinDomain {

$domain = "domain.net"

$domainpw = "localadminpassword"

$username = "localadminusername"

$servers = (Get-Content servers.txt)

$servers | ForEach-Object $_ {

$DomainUser = $Domain + "\" + $Username

$OU = $null

$ComputerSystem = gwmi Win32_ComputerSystem -ComputerName 10.2.4.205 -Credential localadminusername

Write-Host $ComputerSystem

$ComputerSystem.JoinDomainOrWorkGroup($Domain,$domainpw,$username)

}

}

JoinDomain

So, like I said, I tried running this with my domain admin account and run into an access denied error. So I changed the -Credential to a local username on the newly created vm and entered the password for the account when prompted. However, I still run into access denied error. Any ideas on this? Is it even possible or am I wasting my time?

0 Kudos
21 Replies
raghavendrats
Contributor
Contributor
Jump to solution

It's Working for me. Thank you very much.

0 Kudos
Kiala
Contributor
Contributor
Jump to solution

Hi LucD,

I am uing folloing script to add the VM to domain and it works great.

function Join-DomainOrWorkGroup
{

$username = "administrator"
$Domain = "test.com"
$DomainPassword = "xxxxxxx"
$Domainusername = "administrator"
$FJoinOptions = "3"

$ComputerSystem = gwmi Win32_ComputerSystem -ComputerName 10.x.x.x -Credential $username

-Authentication 6 -Impersonation Impersonate

$ComputerSystem.JoinDomainOrWorkGroup($Domain,$DomainPassword,$Domain + "\" +

$Domainusername,$null,$FJoinOptions)


}
Join-DomainOrWorkGroup

But I have two more criterias which I need to fullfill,

1. I need to restart each VM automatically after joining it to domain. Which command I should and where should I add in the script?

2. I have a list of 50 VMs on the same host. How can I implement on all of them in a single shot. Can the script take the input from an excel or csv file or any other alternative?

Thanks for your help!

0 Kudos