Create Device Collections From Active Directory OUs with PowerShell

I was setting up a Config Manager environment for a client who is situated in roughly 40 locations. Each location had an Organizational Unit (OU) in Active Directory (AD) and within that OU was… even more OUs! This was understandable as in each location there were many different rooms for many different purposes and so these were organised via separate OUs.

Baring in mind 40 locations had many OUs and I needed to create a device collection which houses all corresponding devices for each of these, I had to turn to my good friend PowerShell as there is no chance I was creating 400+ device collections manually! As a result I create this script.

Although fairly well documented within the script (If i do say so myself), I will run you through it in a bit more detail. Below you can see an example usage of this script.

For example, let’s pretend we have an AD OU structure a little something like this:

JACKO.local > JACKO > Internal IT > Workstations

Under workstations, imagine we had the following OUs

  • Meeting Room 1
  • Meeting Room 2
  • Office Ground
  • Office Second
  • Main Office
  • IT Office

Now picture each of these OUs having multiple devices and we need to create a device collection for each of them and add the devices within them to that collection. If there are only 6 like above then it’s an easy job and you might consider doing this manually at first, however if there are 50… you might not.

This is where the script comes into play, it is simple to use although it may look daunting at first. There are a couple of question you should ask first:

  1. What do I want the limiting collection of all of these new collections to be?
  2. Are there any OUs I do NOT want to create a collection for?
  3. Do I want to only add the machines that are currently in the OUs to the collection or do I want to also add any devices that are added in the future to the OU?
  4. Do I want these device collections to be in a folder within Config Manager?
  5. Finally, do I want to identify these collections with a tag?

Question #1 corresponds to the “LimitingCollection” parameter, simply pass the name of the desired limiting collection.

Question #2 corresponds to the “ExcludedOUs” parameter, pass a comma-separated list of any OU names you do NOT wish to make a collection for.

Question #3 corresponds to the “MembershipRule” parameter, pass “Query” if you want to add all current AND future devices. Pass “Direct” if you only want to add current devices, but avoid adding any new devices added to the AD OU.

Question #4 is based on the “CollectionFolder” param, just pass the path of that folder. E.G. “Operational” or “Operational\Workstations”

Question #5 related to the “Tag” param. By default all these collections will be created with a name like:

OU Based | $CollectionName

as we believe that you should differentiate your collections and label the ones built from OUs. If you use a tag then the name will change slightly to:

OU Based | $Tag | $CollectionName

Tags are useful for separate sites and also avoid duplicate collection names across sites.

Last but not least, the most important parameter is the “OUSearchBase” param which is going to be the name of the OU which parents all of the other OUs you wish to make device collections based off. In our example we would have to the value

“OU=Workstations,OU=Internal IT,OU=JACKO,DC=JACKO,DC=LOCAL”

This would loop all OUs that are children of “Workstations” and create a device collection for each and automate all that hard work so you can sit back, relax, and have a coffee.

Set static IP on VM Pre LiteTouch in WinPE – MDT VBScript

I was working on a client site last year and setting them up a reference image task sequence for Windows 10. The VMs that I was working with were hosted in a subnet that did not have DHCP configured. This gave me the issue of configuring the IP statically on the VM that was used to build and capture the reference image BEFORE light touch is launched. So I wrote this little script (with google’s help) to grab the MAC address of the virtual NIC that had been initialised on the VM, match it to a variable and set the IP accordingly to the adaptor by referencing its name.

To implement this script into your MDT boot image there are a couple of steps that have to be performed. The first is to tell the boot image to run it beofre litetouch is called! To do this we have to place an updated Unattend.xml into the boot image.

Thankfully MDT has a proccess for adding extrafiles to your bootimage.
But firtly, we need to get sort out the script and xml.
So…

  • Create a folder called “Static IP”.

  • Then copy and paste the XML code from below into a text document and save it in the Static IP folder called “Unattend.xml”.

NOTE: This XML has been built to work with x64 boot images and x86.)

  • Now open notepad again, copy and paste the VBscript from below and then save it as “Set-WinPE-StaticIP.vbs” into the “Static IP” Folder.

  • Now identify the MAC address of the VM(s) you will be targeting as this is what the script identifies the machine with, and place them into the MAC address variable fields (strRefVM1 & strRefVM2).

  • Now populate the IPs as required.

  • At this point is up to you to find the description of the network adaptor as this is used to grab the MAC address. I have supplied the two that I usually see for HyperV and VMware VMs but it is always best to check!

  • So boot up your current MDT Boot ISO and grab the description from the network adaptor that has been initialised by running an “Ipconfig /all”.

  • Now copy and paste it into the variable value for “strNetworkAdapter” or just uncomment the one you require.

  • And finally comment out the Ethernet name (strEthName) that does NOT apply to your environment. For example if you are running Hyper V VMs comment out the VMware line and vice versa.

  • Now open MDT and right click your deployment share and click “properties”.

  • Now select the “Windows PE” tab.

  • For each boot image you require the static IPs to be set click browse on the “Extra directory to add” field and import the “Static IP” folder.

  • Next click Apply.

  • Now right click the deployment share and update your boot images.

  • And thats it! Mount the ISO on your VM and hey presto…. no more IP issues! 🙂

 

Remotely change Site Code WMIC SCCM CMD

Below is a quick WMIC CMD line that will change the site assignment of a client remotely. Bare in mind that account you use must have Administrative access to the machine you are targeting.

This version will read from a text file the machines that require the Site Code change. Again though, bare in mind that the account running the command must have Administrative access to the machines in question.

PowerShell add Computers to Collection from CSV – SCCM ConfigMgr

This is a quick and dirty PowerShell script to import from CSV using the name of the machine to find the resource ID. It will only work for machines that are already a member of the Site you are working on.

For example you could use one of my other scripts to export from one collection and then add to a new collection.

As always this is provided as is, usage is in the header and please use the modded by field 😉

Copyright 2016 SCCMOG | All Rights Reserved