vincikb
Contributor
Contributor

Organize VM's into Folders based on Active Directory Info?

Here is what we are looking to do and I wanted to throw it out to see if anyone had any PowerCLI scripts or snipets that would get me on the right direction.

We want our VMs and Templates view to have folders and to then place VM's into those folders based on the users Division and Department information that is store in Active Directory. So here is how I would envison the script would run.

It would connect to vCenter, find the first VM's name, search AD to find which user that VDI belongs to, then read the Division/Department and move that VM into the matching folder that is already created in Virtual Center.

So in vCenter you would see:

VDI Farm

\IT

\VDIMachine1

\SALES

VDIMachine2

0 Kudos
8 Replies
LucD
Leadership
Leadership

Do you use the QAD cmdlets for AD access ?

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vincikb
Contributor
Contributor

I have the free version downloaded but haven't used them yet.

0 Kudos
LucD
Leadership
Leadership

How do you make the link between the guest's name and the AD account which is the owner ?

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vincikb
Contributor
Contributor

We created a custom field in AD which records the users machine name. So the PS script would just need to search for machine27, then once it finds the user, retrieve that users Dept/Div information, then send the command to vCenter to move the VM into the correct folder.

0 Kudos
vincikb
Contributor
Contributor

Anyone have any hints or code snipets for me that I could start with? Especially the AD searching. Thanks

0 Kudos
LucD
Leadership
Leadership

The AD searching is not too difficult.

$domain = [adsi] "LDAP://<DC-fqdn>/dc=your,dc=domain,dc=com"
$searcher = [adsisearcher] $domain
foreach($vm in Get-VM){
   $searcher.Filter = '(&(objectClass=User)(extensionAttribute1=' + $vm.Name + '))'
   $userResult = $searcher.FindOne()
   $user = $userResult.GetDirectoryEntry()
   $user.name
   $user.Division
   $user.Departement
}

Update the first line with one of your AD DCs and correct the DNname of your AD domain.

You will need to put the correct number in the 'extensionAttribute1' field.

Most probably you will also have to correct the Division and Department fields.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
BlueMike
Contributor
Contributor

I work with the original poster.  I used the Quest Active Directory plugin to grab the relevant information out of Active Directory.  Since our VMs are named based on the user name (with a 2 character prefix), I was able to determine the related user, and cross reference the organizational information from that.  Finally, I created any folders that didn't already exist in the structure and moved the machines that were not in the correct folder already.  First run took a couple hours for around 1600 VMs, but subsequent runs took around 5 minutes.  Later, wrote a SQL query that I used from PowerShell to pull all the information from the VMware vSphere database much more quickly, and cross reference the differences via a SQL map of our Active Directory structure, and the actual data crunching was sped up significantly.

Unfortunately, I can't share the code as it is very specific to our environment, but I can confirm that it can be done with only PowerCLI and ActiveDirectory queries only.

0 Kudos
LucD
Leadership
Leadership

Thanks for sharing that.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos