工作站允许在活动目录中使用属性

时间:2013-03-16 17:39:06

标签: c# active-directory

通过执行命令行net user "userid" /domain,我会看到workstations allowed值。

活动目录中此属性的名称是什么?

我正在使用c#来检索数据。

1 个答案:

答案 0 :(得分:1)

简短回答:userWorkstations

答案很长:我使用以下脚本从我需要学习的AD对象中读取属性。

    'Script to Walk Through the Property Cache for an Object and Display All Values

Option Explicit
On Error Resume Next

Const ADSTYPE_INVALID = 0
Const ADSTYPE_DN_STRING = 1
Const ADSTYPE_CASE_EXACT_STRING = 2
Const ADSTYPE_CASE_IGNORE_STRING = 3
Const ADSTYPE_PRINTABLE_STRING = 4
Const ADSTYPE_NUMERIC_STRING = 5
Const ADSTYPE_BOOLEAN = 6
Const ADSTYPE_INTEGER = 7
Const ADSTYPE_OCTET_STRING = 8
Const ADSTYPE_UTC_TIME = 9
Const ADSTYPE_LARGE_INTEGER = 10
Const ADSTYPE_PROV_SPECIFIC = 11
Const ADSTYPE_OBJECT_CLASS = 12
Const ADSTYPE_CASEIGNORE_LIST = 13
Const ADSTYPE_OCTET_LIST = 14
Const ADSTYPE_PATH = 15
Const ADSTYPE_POSTALADDRESS = 16
Const ADSTYPE_TIMESTAMP = 17
Const ADSTYPE_BACKLINK = 18
Const ADSTYPE_TYPEDNAME = 19
Const ADSTYPE_HOLD = 20
Const ADSTYPE_NETADDRESS = 21
Const ADSTYPE_REPLICAPOINTER = 22
Const ADSTYPE_FAXNUMBER = 23
Const ADSTYPE_EMAIL = 24
Const ADSTYPE_NT_SECURITY_DESCRIPTOR = 25
Const ADSTYPE_UNKNOWN = 26

Const ADS_PROPERTY_CLEAR = 1
Const ADS_PROPERTY_UPDATE = 2
Const ADS_PROPERTY_APPEND = 3
Const ADS_PROPERTY_DELETE = 4

Dim adsPropValue  'An individual property value within a loop
Dim adsPropEntry  'An ADSI PropertyEntry object
Dim adsObject     'The object whose property list we wish to investigate
Dim txtStr        'A text string used to display results in one go
Dim intPropCount  'The number of properties in 
Dim intIndex      'The index used while looping through the property list
Dim intCount      'Used to display property values in a numbered sequence
Dim ArgObjName    'Used to hold name of object to be enumerated
Dim objAttr
Dim sidBin, sidHex, sidDec

'Uncomment one of these lines and modify it to your own environment
'Set adsObject = GetObject("LDAP://cn=Managers,ou=Sales,dc=windows,dc=mycorp,dc=com")
'Set adsObject = GetObject("WinNT://WINDOWS/Managers,Group")


if WScript.Arguments.Count > 0 then
    ArgObjName = WScript.Arguments(0)
else
    ArgObjName = InputBox("Please enter the machine you wish to query or leave blank for local Profile.", _
            "Walk1.vbs","LDAP://CN=CND7352YMY-A,OU=Kingsport,OU=NAR,OU=Workstations,DC=emn,DC=com")
    if ArgObjName = "" then
        ArgObjName = "LDAP://CN=ZWW8R-A,OU=Longview,OU=NAR,OU=Desktops,DC=emn,DC=com"
    end if
end if

if instr(ArgObjName,":") < 1 then ArgObjName = "LDAP://" & ArgObjName

wscript.echo "Connecting to " & ArgObjName
Set adsObject = GetObject(ArgObjName)
adsObject.GetInfo

intPropCount = adsObject.PropertyCount
txtStr = wscript.scriptname & " For " & ArgObjName & vbcrlf 
txtStr = txtStr & "There are " & intPropCount & " values in the property cache." & vbCrLf
wscript.echo "GUID: " & adsobject.GUID

sidBin = adsObject.objectSid
sidHex = OctetToHexStr(sidBin)
sidDec = HexStrToDecStr(sidHex)
WScript.Echo "SID: " & sidDec & " (hex: " & sidHex & ")"

'The extra vbTab's used in the first loop are to space the results so that they are nicely
'formatted with the list of values in the second loop
For intIndex = 0 To (intPropCount-1)
  Set adsPropEntry = adsObject.Item(CInt(intIndex))
  txtStr = txtStr & adsPropEntry.Name & vbCrLf
  If (adsPropEntry.ADsType = ADSTYPE_INVALID) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "INVALID" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_DN_STRING) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "DN_STRING" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_CASE_EXACT_STRING) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "CASE_EXACT_STRING" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_CASE_IGNORE_STRING) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "CASE_IGNORE_STRING" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_PRINTABLE_STRING) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "PRINTABLE_STRING" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_NUMERIC_STRING) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "NUMERIC_STRING" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_BOOLEAN) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "BOOLEAN" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_INTEGER) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "INTEGER" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_OCTET_STRING) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "OCTET_STRING" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_UTC_TIME) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "UTC_TIME" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_LARGE_INTEGER) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "LARGE_INTEGER" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_PROV_SPECIFIC) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "PROV_SPECIFIC" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_OBJECT_CLASS) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "OBJECT_CLASS" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_CASEIGNORE_LIST) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "CASEIGNORE_LIST" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_OCTET_LIST) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "OCTET_LIST" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_PATH) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "PATH" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_POSTALADDRESS) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "POSTALADDRESS" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_TIMESTAMP) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "TIMESTAMP" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_BACKLINK) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "BACKLINK" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_TYPEDNAME) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "TYPEDNAME" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_HOLD) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "HOLD" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_NETADDRESS) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "NETADDRESS" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_REPLICAPOINTER) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "REPLICAPOINTER" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_FAXNUMBER) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "FAXNUMBER" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_EMAIL) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "EMAIL" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_NT_SECURITY_DESCRIPTOR) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "NT_SECURITY_DESCRIPTOR" & vbCrLf
  ElseIf (adsPropEntry.ADsType = ADSTYPE_UNKNOWN) Then
    txtStr = txtStr & vbTab & "Type:" & vbTab & vbTab & "UNKNOWN" & vbCrLf
  End If

  intCount = 1
  For Each adsPropValue In adsPropEntry.Values  
    If (adsPropValue.ADsType = ADSTYPE_DN_STRING) Then
      txtStr = txtStr & vbTab & "Value #" & intCount & ":" & vbTab & adsPropValue.DNString & vbCrLf
    ElseIf (adsPropValue.ADsType = ADSTYPE_CASE_EXACT_STRING) Then
      txtStr = txtStr & vbTab & "Value #" & intCount & ":" & vbTab & adsPropValue.CaseExactString & vbCrLf
    ElseIf (adsPropValue.ADsType = ADSTYPE_CASE_IGNORE_STRING) Then
      txtStr = txtStr & vbTab & "Value #" & intCount & ":" & vbTab & adsPropValue.CaseIgnoreString & vbCrLf
    ElseIf (adsPropValue.ADsType = ADSTYPE_PRINTABLE_STRING) Then
      txtStr = txtStr & vbTab & "Value #" & intCount & ":" & vbTab & adsPropValue.PrintableString & vbCrLf
    ElseIf (adsPropValue.ADsType = ADSTYPE_NUMERIC_STRING) Then
      txtStr = txtStr & vbTab & "Value #" & intCount & ":" & vbTab & adsPropValue.NumericString & vbCrLf
    ElseIf (adsPropValue.ADsType = ADSTYPE_BOOLEAN) Then
      txtStr = txtStr & vbTab & "Value #" & intCount & ":" & vbTab & CStr(adsPropValue.Boolean) & vbCrLf
    ElseIf (adsPropValue.ADsType = ADSTYPE_INTEGER) Then
      txtStr = txtStr & vbTab & "Value #" & intCount & ":" & vbTab & adsPropValue.Integer & vbCrLf
    End If
    intCount=intCount+1
  Next
  If (adsPropEntry.ADsType = ADSTYPE_UTC_TIME) Then
      objattr = ""
      objAttr = adsObject.get(adsPropEntry.Name)
      if isarray(objAttr) then
    txtStr = txtStr & vbTab & "Value   :" & vbTab & join(objAttr,";") & vbCrLf
      else
    txtStr = txtStr & vbTab & "Value   :" & vbTab & objAttr & vbCrLf
      end if
  End If
  If (adsPropEntry.ADsType = ADSTYPE_LARGE_INTEGER) Then
      wscript.echo adsPropEntry.Name
      objattr = ""
      objAttr = adsObject.get(adsPropEntry.Name)
      if isarray(objAttr) then
    txtStr = txtStr & vbTab & "Value   :" & vbTab & join(objAttr,";") & vbCrLf
      else
    txtStr = txtStr & vbTab & "Value   :" & vbTab & objAttr & vbCrLf
      end if
  End If
  'If (adsPropEntry.ADsType = ADSTYPE_LARGE_INTEGER) Then
  '    txtStr = txtStr & vbTab & "Value   :" & vbTab & FormatNumber(adsPropEntry,0) & vbCrLf
  'End If
Next
WScript.Echo txtStr

'RefreshTime = FormatNumber((Account.get("PasswordAge"))/86400,0)

' The following functions are attributable to <A Target="NewWindow" Href="http://www.rlmueller.net/"><FONT SIZE="+1"><B>Richard Mueller [MVP]</B></FONT></A>

Function OctetToHexStr(arrbytOctet) 
    Dim k 
    OctetToHexStr = "" 
    For k = 1 To Lenb(arrbytOctet) 
        OctetToHexStr = OctetToHexStr _ 
            & Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2) 
    Next 
End Function 

Function HexStrToDecStr(strSid) 
    Dim arrbytSid, lngTemp, j 
    ReDim arrbytSid(Len(strSid)/2 - 1) 
    For j = 0 To UBound(arrbytSid) 
        arrbytSid(j) = CInt("&H" & Mid(strSid, 2*j + 1, 2)) 
    Next 
    HexStrToDecStr = "S-" & arrbytSid(0) & "-" _ 
             & arrbytSid(1) & "-" & arrbytSid(8) 

    lngTemp = arrbytSid(15) 
    lngTemp = lngTemp * 256 + arrbytSid(14) 
    lngTemp = lngTemp * 256 + arrbytSid(13) 
    lngTemp = lngTemp * 256 + arrbytSid(12) 

    HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp) 

    lngTemp = arrbytSid(19) 
    lngTemp = lngTemp * 256 + arrbytSid(18) 
    lngTemp = lngTemp * 256 + arrbytSid(17) 
    lngTemp = lngTemp * 256 + arrbytSid(16) 

    HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp) 

    lngTemp = arrbytSid(23) 
    lngTemp = lngTemp * 256 + arrbytSid(22) 
    lngTemp = lngTemp * 256 + arrbytSid(21) 
    lngTemp = lngTemp * 256 + arrbytSid(20) 

    HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp) 

    lngTemp = arrbytSid(25) 
    lngTemp = lngTemp * 256 + arrbytSid(24) 

    HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp) 

End Function