Thanks a lot for your time in advance of reading this (it is going to be a long question).
I'm a newbie with a little knowledge of Powershell. I have a situation in which I'm creating an installation wrapper script.
This script will be executed on many user machines, and they may/may not have Active directory access.
But the script should be in such a way that, if the Machine object is part of a specific OU in active directory, then it should choose the configuration file accordingly.
Will the OU information be stored somewhere in the WMI? What is ADSI searcher, will it need any special access or pre-requisites to work properly?
I have googled and got the below script using ADSI and it seems to be working on my virtual machine, but not on a few other virtual machines (for reasons unknown to me).
$ComputerName = $env:ComputerName
$ADSISearcher = New-Object System.DirectoryServices.DirectorySearcher
$ADSISearcher.Filter = '(&(name=' + $ComputerName + ')(objectClass=computer))'
$ADSISearcher.SearchScope = 'Subtree'
$Computer = $ADSISearcher.FindAll()
$OU = $($Computer.Properties.Item('distinguishedName'))
I've tried running the script on various other user machines (mixed AD access levels), and $OU = $($Computer.Properties.Item('distinguishedName'))
is giving error.
You cannot call a method on a null-valued expression. At line:1 char:42 + $OU = $($Computer.Properties.Item < <<< ('distinguishedName')) + CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
But $computer
is not a null value. It has the result below (I have modified the LDAP info..)
Path Properties
---- ----------
LDAP://CN=,OU=< ........>,OU=V< ...>,OU=N... {primarygroupid, iscriticalsystemobject, msds-supportede...
Now the question is: Am I in the right direction, will ADSI searcher query this information from locally cached information?
If yes, can you help me fix this issue? If I'm all wrong, please correct me. I've been sitting with this for days now and help would be much appreciated.