How do I use WMI to get the current OU of a computer and list all other computers in that OU?
问题 I'm using WMI and am trying to find a powershell script that will allow me get the OU of the local computer and then get a full list of computers in that OU. 回答1: Here you go: $ComputerName = '<Name of Computer>'; $Computer = Get-WmiObject -Namespace 'root\directory\ldap' -Query "Select DS_distinguishedName from DS_computer where DS_cn = '$ComputerName'"; $OU = $Computer.DS_distinguishedName.Substring($Computer.DS_distinguishedName.IndexOf('OU=')); $ComputersInOU = Get-WmiObject -Namespace