I’m using PowerShell v. 4.0 to try to inventory computers in some child OU’s by the Operating system and I’m getting inconsistent results. Here’s the code I’m using:
The problem, for example, is that I get “0” Windows 2008 machines for a given OU in the csv file, but I use an LDAP browser with the same LDAP filter and it finds several machines. I’ve tried modifying the filter by putting in the EXACT operating system name (like “Windows Server 2012 R2 Standard”), but it still doesn’t work.
In other cases, I get a strange output message in the .csv: “Microsoft.ActiveDirectory.Management.ADPropertyValueCollection”. Research indicates that is an indication of a multi-valued attribute. Is that because there are multiple flavors of a given OS (Standard vs. Enterprise vs. DataCenter)? I would think putting in “**” in the filter would resolve that.
I don’t really want to use WMI for this, since I don’t need to get that far down in the weeds. I just want to pull the OS attribute from the computer object in AD and have it work consistently.
I found that it works, except in cases when only 1 object is found that meets the search filter. If it finds 0 or 2 or more objects it works, but not if it finds 1. If it finds 1, then I get the “Microsoft.ActiveDirectory.Management.ADPropertyValueCollection” output.
When you find only one object, you get the object and not an array of objects. Try $windows2012.gettype() and you´ll see that the object types are different. I suggest you use Measure-Object to get the count.