通过下边脚本可以把当前加入域某OU下的计算机导出到“计算机列表.csv ”文件中,导出的计算机信息包括机器名,dNSHostName,最后登录时间等属性。
'
Define constants
' CONST ForReading = 1
CONST ForWriting = 2
CONST ForAppending = 8
CONST CONST_ERROR = 0
CONST CONST_WSCRIPT = 1
CONST CONST_CSCRIPT = 2
CONST CONST_SHOW_USAGE = 3
CONST CONST_LIST = 4
Const ADS_SCOPE_SUBTREE = 2
Const ADS_UF_DONT_EXPIRE_PASSWD = 65536
Const ADS_UF_PASSWORD_EXPIRED = 8388608
Const E_ADS_PROPERTY_NOT_FOUND = & h8000500D
Const ONE_HUNDRED_NANOSECOND = . 000000100
Const SECONDS_IN_DAY = 86400
Dim lastLogin
' ''''''''''''''''''''''''
Set objShell = CreateObject ( " Wscript.Shell " )
lngBiasKey = objShell.RegRead( " HKLM\System\CurrentControlSet\Control\ " _
& " TimeZoneInformation\ActiveTimeBias " )
If ( UCase ( TypeName (lngBiasKey)) = " LONG " ) Then
lngBias = lngBiasKey
ElseIf ( UCase ( TypeName (lngBiasKey)) = " VARIANT() " ) Then
lngBias = 0
For k = 0 To UBound (lngBiasKey)
lngBias = lngBias + (lngBiasKey(k) * 256 ^ k)
Next
End If
Set objShell = Nothing
' ''''''''''''''''''''''''
inputOU = inputbox ( " 请输入所需查询计算机的OU,该OU必须位于Machines的下一层,不输入为查询所有计算机 " , " 输入信息 " )
Outputfile = strDCName1 & " 计算机列表 "
Set FSO = CreateObject ( " Scripting.FileSystemObject " )
Set fLog = fso.OpenTextFile(Outputfile & inputOU & " .csv " , ForWriting, TRUE )
fLog.WriteLine " 机器名,dNSHostName,最后登录时间 "
Set rootDSE = GetObject ( " LDAP://rootDSE " )
BaseDN = rootDSE.Get( " defaultNamingContext " )
DC = replace (BaseDN, " DC= " , " , " )
DC = replace (DC, " ,, " , " . " )
DC = right (DC, len (DC) - 1 )
TARGET_OU = " Machines "
if inputOU <> "" then
TARGET_OU = inputOU & " ,OU= " & TARGET_OU
end if
inputdate = 0
Set objConnection = CreateObject ( " ADODB.Connection " )
objConnection.Provider = " ADsDSOObject "
objConnection.Open " Active Directory Provider "
Set objCommand = CreateObject ( " ADODB.Command " )
Set objCommand.ActiveConnection = objConnection
Set objCommand1 = CreateObject ( " ADODB.Command " )
Set objCommand1.ActiveConnection = objConnection
' on error resume next
objCommand.CommandText = " <LDAP:// " & DC & " /OU= " & TARGET_OU & " , " & BaseDN & " >;(&(objectclass=Computer));cn,ADsPath;subtree "
Set objRecordSet = objCommand.Execute
if err.number <> 0 then
msgbox " 无法找到: " & TARGET_OU
end if
wscript.echo " Begin: "
If objRecordSet.RecordCount > 0 Then
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
On Error Resume Next
adspath = objRecordSet.Fields( " adspath " )
ShowMSG adspath
Set objComputer = GetObject (adspath)
lastName = objComputer.dNSHostName
computername = objComputer.Name
computername = replace (computername, " CN= " , "" )
Set objDate = objComputer.lastLogonTimeStamp
If (Err.Number <> 0 ) Then
On Error GoTo 0
dtmDate = # 1 / 1 / 1601 #
Else
On Error GoTo 0
lngHigh = objDate.HighPart
lngLow = objDate.LowPart
If (lngLow < 0 ) Then
lngHigh = lngHigh + 1
End If
If (lngHigh = 0 ) And (lngLow = 0 ) Then
dtmDate = # 1 / 1 / 1601 #
Else
dtmDate = # 1 / 1 / 1601 # + (((lngHigh * ( 2 ^ 32 )) _
+ lngLow) / 600000000 - lngBias) / 1440
End If
End If
lastLogin = dtmDate
fLog.WriteLine computername & " , " & lastName & " , " & lastLogin
objRecordSet.MoveNext
Loop
End If
wscript.echo " Count: " & i
Sub ShowMSG(strSubMSG)
Wscript.Echo Time & vbTab & strSubMSG
end sub
' CONST ForReading = 1
CONST ForWriting = 2
CONST ForAppending = 8
CONST CONST_ERROR = 0
CONST CONST_WSCRIPT = 1
CONST CONST_CSCRIPT = 2
CONST CONST_SHOW_USAGE = 3
CONST CONST_LIST = 4
Const ADS_SCOPE_SUBTREE = 2
Const ADS_UF_DONT_EXPIRE_PASSWD = 65536
Const ADS_UF_PASSWORD_EXPIRED = 8388608
Const E_ADS_PROPERTY_NOT_FOUND = & h8000500D
Const ONE_HUNDRED_NANOSECOND = . 000000100
Const SECONDS_IN_DAY = 86400
Dim lastLogin
' ''''''''''''''''''''''''
Set objShell = CreateObject ( " Wscript.Shell " )
lngBiasKey = objShell.RegRead( " HKLM\System\CurrentControlSet\Control\ " _
& " TimeZoneInformation\ActiveTimeBias " )
If ( UCase ( TypeName (lngBiasKey)) = " LONG " ) Then
lngBias = lngBiasKey
ElseIf ( UCase ( TypeName (lngBiasKey)) = " VARIANT() " ) Then
lngBias = 0
For k = 0 To UBound (lngBiasKey)
lngBias = lngBias + (lngBiasKey(k) * 256 ^ k)
Next
End If
Set objShell = Nothing
' ''''''''''''''''''''''''
inputOU = inputbox ( " 请输入所需查询计算机的OU,该OU必须位于Machines的下一层,不输入为查询所有计算机 " , " 输入信息 " )
Outputfile = strDCName1 & " 计算机列表 "
Set FSO = CreateObject ( " Scripting.FileSystemObject " )
Set fLog = fso.OpenTextFile(Outputfile & inputOU & " .csv " , ForWriting, TRUE )
fLog.WriteLine " 机器名,dNSHostName,最后登录时间 "
Set rootDSE = GetObject ( " LDAP://rootDSE " )
BaseDN = rootDSE.Get( " defaultNamingContext " )
DC = replace (BaseDN, " DC= " , " , " )
DC = replace (DC, " ,, " , " . " )
DC = right (DC, len (DC) - 1 )
TARGET_OU = " Machines "
if inputOU <> "" then
TARGET_OU = inputOU & " ,OU= " & TARGET_OU
end if
inputdate = 0
Set objConnection = CreateObject ( " ADODB.Connection " )
objConnection.Provider = " ADsDSOObject "
objConnection.Open " Active Directory Provider "
Set objCommand = CreateObject ( " ADODB.Command " )
Set objCommand.ActiveConnection = objConnection
Set objCommand1 = CreateObject ( " ADODB.Command " )
Set objCommand1.ActiveConnection = objConnection
' on error resume next
objCommand.CommandText = " <LDAP:// " & DC & " /OU= " & TARGET_OU & " , " & BaseDN & " >;(&(objectclass=Computer));cn,ADsPath;subtree "
Set objRecordSet = objCommand.Execute
if err.number <> 0 then
msgbox " 无法找到: " & TARGET_OU
end if
wscript.echo " Begin: "
If objRecordSet.RecordCount > 0 Then
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
On Error Resume Next
adspath = objRecordSet.Fields( " adspath " )
ShowMSG adspath
Set objComputer = GetObject (adspath)
lastName = objComputer.dNSHostName
computername = objComputer.Name
computername = replace (computername, " CN= " , "" )
Set objDate = objComputer.lastLogonTimeStamp
If (Err.Number <> 0 ) Then
On Error GoTo 0
dtmDate = # 1 / 1 / 1601 #
Else
On Error GoTo 0
lngHigh = objDate.HighPart
lngLow = objDate.LowPart
If (lngLow < 0 ) Then
lngHigh = lngHigh + 1
End If
If (lngHigh = 0 ) And (lngLow = 0 ) Then
dtmDate = # 1 / 1 / 1601 #
Else
dtmDate = # 1 / 1 / 1601 # + (((lngHigh * ( 2 ^ 32 )) _
+ lngLow) / 600000000 - lngBias) / 1440
End If
End If
lastLogin = dtmDate
fLog.WriteLine computername & " , " & lastName & " , " & lastLogin
objRecordSet.MoveNext
Loop
End If
wscript.echo " Count: " & i
Sub ShowMSG(strSubMSG)
Wscript.Echo Time & vbTab & strSubMSG
end sub