人力検索はてな
モバイル版を表示しています。PC版はこちら
i-mobile

AD上のコンピュータオブジェクトが所属するOUを調べるVBScriptの作成を考えております。

メッセージボックスを表示して検索対象のコンピュータを記入するような
方式を考えております。

スクリプトは「VBScript」を考えておりますが、作成のヒント
出来ましたらサンプルスクリプトの作成をお願いいたします。

※内容がいい方にポイントを一括してお渡しします。

■ドメイン:「test.local」

●質問者: gocnia3
●カテゴリ:コンピュータ インターネット
○ 状態 :終了
└ 回答数 : 2/2件

▽最新の回答へ

1 ● パパトモ
●250ポイント

過去ログが役に立ちそうですが。

http://q.hatena.ne.jp/1189158411
http://nakagami.blog.so-net.ne.jp/2005-05-23

ループする部分を書き換えれば、できるんじゃないでしょうか。


2 ● gizmo5
●250ポイント

http://www.experts-exchange.com/Software/Server_Software/File_Servers/Active_Directory/Q_27736201.html
ここにあるスクリプトが求めているものじゃないでしょうか。

コードでは、テキストファイルからコンピュータ名を取得していますが、そこを画面から入力するように変えれば良いと思います。
先のページは、先頭に余計な情報が多いので、ちょっと長いですが以下にコードを引用します。

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
strInput = "computers.txt"
Set objInput = objFSO.OpenTextFile(strInput, ForReading, False)
While Not objInput.AtEndOfStream
 strComputer = objInput.ReadLine
 strADsPath = Get_LDAP_User_Properties("computer", "name", strComputer, "adsPath")
 If strADsPath <> "" Then
 Set objComputer = GetObject(strADsPath)
 WScript.Echo strComputer & ": " & objComputer.parent
 Set objComputer = Nothing
 Else
 WScript.Echo strComputer & " not found"
 End If
Wend
objInput.Close

Public Function Get_LDAP_User_Properties(strObjectType, strSearchField, strObjectToGet, strCommaDelimProps)
 
  ' This is a custom function that connects to the Active Directory, and returns the specific
  ' Active Directory attribute value, of a specific Object.
  ' strObjectType: usually "User" or "Computer"
  ' strSearchField: the field by which to seach the AD by. This acts like an SQL Query's WHERE clause.
  ' It filters the results by the value of strObjectToGet
  ' strObjectToGet: the value by which the results are filtered by, according the strSearchField.
  ' For example, if you are searching based on the user account name, strSearchField
  ' would be "samAccountName", and strObjectToGet would be that speicific account name,
  ' such as "jsmith". This equates to "WHERE 'samAccountName' = 'jsmith'"
  ' strCommaDelimProps: the field from the object to actually return. For example, if you wanted
  ' the home folder path, as defined by the AD, for a specific user, this would be
  ' "homeDirectory". If you want to return the ADsPath so that you can bind to that
  ' user and get your own parameters from them, then use "ADsPath" as a return string,
  ' then bind to the user: Set objUser = GetObject("LDAP://" & strReturnADsPath)
 
  ' Now we're checking if the user account passed may have a domain already specified,
  ' in which case we connect to that domain in AD, instead of the default one.
 If InStr(strObjectToGet, "\") > 0 Then
 arrGroupBits = Split(strObjectToGet, "\")
 strDC = arrGroupBits(0)
 strDNSDomain = strDC & "/" & "DC=" & Replace(Mid(strDC, InStr(strDC, ".") + 1), ".", ",DC=")
 strObjectToGet = arrGroupBits(1)
 Else
  ' Otherwise we just connect to the default domain
 Set objRootDSE = GetObject("LDAP://RootDSE")
 strDNSDomain = objRootDSE.Get("defaultNamingContext")
 End If

 strBase = "<LDAP://" & strDNSDomain & ">"
  ' Setup ADO objects.
 Set adoCommand = CreateObject("ADODB.Command")
 Set ADOConnection = CreateObject("ADODB.Connection")
 ADOConnection.Provider = "ADsDSOObject"
 ADOConnection.Open "Active Directory Provider"
 adoCommand.ActiveConnection = ADOConnection


  ' Filter on user objects.
  'strFilter = "(&(objectCategory=person)(objectClass=user))"
 strFilter = "(&(objectClass=" & strObjectType & ")(" & strSearchField & "=" & strObjectToGet & "))"

  ' Comma delimited list of attribute values to retrieve.
 strAttributes = strCommaDelimProps
 arrProperties = Split(strCommaDelimProps, ",")

  ' Construct the LDAP syntax query.
 strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
 adoCommand.CommandText = strQuery
  ' Define the maximum records to return
 adoCommand.Properties("Page Size") = 100
 adoCommand.Properties("Timeout") = 30
 adoCommand.Properties("Cache Results") = False

  ' Run the query.
 Set adoRecordset = adoCommand.Execute
  ' Enumerate the resulting recordset.
 strReturnVal = ""
 Do Until adoRecordset.EOF
  ' Retrieve values and display.
 For intCount = LBound(arrProperties) To UBound(arrProperties)
 If strReturnVal = "" Then
 If IsArray(adoRecordset.Fields(intCount).Value) Then
 For Each strValue In adoRecordset.Fields(intCount).Value
 If strReturnVal = "" Then
 strReturnVal = strValue
 Else
 strReturnVal = strReturnVal & ", " & strValue
 End If
 Next
 Else
 strReturnVal = adoRecordset.Fields(intCount).Value
 End If
 Else
 If IsArray(adoRecordset.Fields(intCount).Value) Then
 For Each strValue In adoRecordset.Fields(intCount).Value
 strReturnVal = strReturnVal & ", " & strValue
 Next
 Else
 strReturnVal = strReturnVal & ", " & adoRecordset.Fields(intCount).Value
 End If
 End If
 Next
  ' Move to the next record in the recordset.
 adoRecordset.MoveNext
 Loop
 
  ' Clean up.
 adoRecordset.Close
 ADOConnection.Close
 Get_LDAP_User_Properties = strReturnVal
 
End Function
関連質問

●質問をもっと探す●



0.人力検索はてなトップ
8.このページを友達に紹介
9.このページの先頭へ
対応機種一覧
お問い合わせ
ヘルプ/お知らせ
ログイン
無料ユーザー登録
はてなトップ