Active Directory オブジェクトを使用すると、ユーザー、セキュリティ ポリシー、プリンター、分散コンポーネント、その他のリソースなど、コンピューター ネットワーク ドメイン内のリソースを検索できます。 Active Directory オブジェクトは、WMI を使用して作成および更新できます。 WMI イベント通知を使用して、オブジェクトに関する新しい情報が使用可能になったときに Active Directory オブジェクトを更新できます。 たとえば、Active Directory ユーザー オブジェクトが作成されたら、WMI のイベント クエリを使用してその作成を検出し、イベントが受信されたときに、新しい情報でオブジェクトを更新できます。
次のコード例では、Active Directory ユーザー オブジェクトを表すクラスの新しい WMI インスタンスを作成します。 この例では、新しい Active Directory ユーザー インスタンスを作成するために必要なさまざまなプロパティに値を割り当てる方法を示します。
Const cUserID = "WMIUser"
Const cComputerName = "LocalHost"
Const cWMInamespace = "root/directory/LDAP"
Const cWMIclass = "ds_user"
Const ADS_UF_ACCOUNTDISABLE = &h000002
Set objWMILocator = _
CreateObject("WbemScripting.SWbemLocator")
objWMILocator.Security_.AuthenticationLevel = _
wbemAuthenticationLevelDefault
Set objWMIServices = objWMILocator. _
ConnectServer(cComputerName, cWMInamespace, "", "")
Set objWMIClass = objWMIServices.Get(cWMIclass)
Set objWMIInstance = objWMIClass.SpawnInstance_
objWMIInstance.DS_sAMAccountName = userID
objWMIInstance.ADSIPath = "LDAP://CN=" & userID & _
",CN=Users,DC=LissWare,DC=Net"
objWMIInstance.Put_ (wbemChangeFlagCreateOrUpdate Or _
wbemFlagReturnWhenComplete)
WScript.Echo "Active Directory user created."
次のコード例では、Active Directory オブジェクトの WMI インスタンスを更新します。 この例では、displayname 属性が更新されます。
set svc = getObject("Winmgmts:root\directory\ldap")
' A context object is used to tell the provider which
' specific properties are going to be updated.
' In most cases, when you update a WMI object you do not
' need to specify an additional context object.
' However, if a context object is not supplied for a
' directory service provider, the update fails.
set octx = createobject( _
"wbemscripting.swbemnamedvalueset")
octx.add "__PUT_EXT_PROPERTIES", array("ds_displayname")
octx.add "__PUT_EXTENSIONS", true
octx.add "__PUT_EXT_CLIENT_REQUEST", true
set objEnum = svc.execQuery( _
"select * from ds_computer where ds_cn = 'userName'", "WQL", 32)
for each obj in objEnum
obj.ds_DisplayName = "updatedName"
obj.put_ 1, octx
next
WScript.Echo "Active Directory user successfully updated"