I need a script to detect if a computer is in an active directory user group

﹥>﹥吖頭↗ 提交于 2019-12-13 04:38:32

问题


I found online a VBscript that joins a computer to a group in the Users OU for Direct Access to work. It works perfectly in the task sequence for our OSD using SCCM 2012 but we've come to a road block where we only want laptops to be added rather then all computers.

If someone would be so kind, I need a script (Powershell, VB, JScript) that will detect that the computer was added to the group in AD.


回答1:


Set the task sequence step to run based on a condition.

The above conditions detects if the computer is a laptop. The first one will only work when you have MDT integrated and the second uses a simple WMI query to detect the system type.

The WMI query might not be as reliable, so I would advice you integrate MDT into your environment.




回答2:


You could try something like this in PowerShell to check that the named computer is in the OU or not:

Script:

import-module activedirectory

$OU = @() 
$CheckOU = "LaptopOU"
$computerName = "Laptop12345"

$user = get-adcomputer $computerName -Properties *
$user.DistinguishedName -split "," | %{If($_ -match "OU="){$OU += $_ -replace "OU=",""}}

If($OU -match $CheckOU){
    "Computer:$computerName is in the OU:$CheckOU"
    # Do something...
}
Else{
    "Computer:$computerName is not in the OU:$CheckOU"
    # Do something else..
}

This will take a $computerName and get all the OU's that it's in from Active Directory and stores them in an $OU array.

Then you can use that array to simply check if the computer is in the given OU ($CheckOU) or not by using the -match operator.

Note: You need to make sure that you import the activedirectory module. If you do not have this to import follow this link for how to get it: activedirectory module

For more Cmdlet's and syntax on the Powershell Active Directory module follow this Link



来源:https://stackoverflow.com/questions/17752248/i-need-a-script-to-detect-if-a-computer-is-in-an-active-directory-user-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!