在Active Directory脚本的域控制器上获取日期格式(美国或澳大利亚)

时间:2014-10-13 11:12:44

标签: vbscript active-directory

我有一个VBScript来提取某些日期之间的域用户,以便我可以修改他们的帐户。

有时,我注意到当我延长到期日时,它将我设定的日期(澳大利亚格式dd / mm / yyyy)改为美国格式....

如果我要检查域控制器日期格式,我应该怎么做呢?如果是这样,我怎样才能从域控制器获取该信息?

或者他们的代码就像VBA中的FORMAT ...所以我可以设置日期'我的方式'....也许我应该将所有内容转换为VBA ..

这是我的代码的基础......

 Start_Date = 01/08/2014 ' 1st august 2014
 Days       = 28/08/2014 '28th august 2014

 Combined_Date = DateAdd("d", Days, Start_Date)
 Start_Date    = DateAdd("n", lngBias, Start_Date)
 Combined_Date = DateAdd("n", lngBias, Combined_Date)
 lngSeconds1   = DateDiff("s", #1/1/1601#, Start_Date)
 lngSeconds2   = DateDiff("s", #1/1/1601#, Combined_Date)
 str64Bit1     = CStr(lngSeconds1) & "0000000"
 str64Bit2     = CStr(lngSeconds2) & "0000000"

'##############################################################

 Set objRootDSE = GetObject("LDAP://RootDSE")
 strDomain      = objRootDSE.Get("DefaultNamingContext")

' my filter to get users that expiry between dates that are not disabled.
 SearchBy       = "<LDAP://" & strDomain & ">;" & _
 "(&(objectclass=user)(objectcategory=person)(accountExpires>=" & _
  str64Bit1 & ")(accountExpires<=" & str64Bit2 & ")(!(userAccountControl:1.2.840.113556.1.4.803:=2)));" & _
 "distinguishedname;subtree"

 Set objConnection                    = CreateObject("ADODB.Connection")
 objConnection.Provider               = "ADsDSOObject"
 objConnection.Open "Active Directory Provider"
 Set objCommand                       = CreateObject("ADODB.Command")
 Set objCommand.ActiveConnection      = objConnection
 objCommand.Properties("Page Size")   = 100000
 objCommand.Properties("Timeout")     = 300
 objCommand.Properties("Size Limit")  = 750000
 objCommand.CommandText               = SearchBy
 Set objRecordSet                     = objCommand.Execute
 objRecordSet.MoveFirst

 Do Until objRecordSet.EOF
  objUserOU  = objRecordSet.Fields("distinguishedname").Value
  Set myUser = GetObject ("LDAP://" & objUserOU)


   Wscript.Echo myUser.SamAccountName
   Wscript.Echo myUser.DisplayName
   Wscript.Echo myUser.AccountExpirationDate

  Err.clear
  objRecordSet.MoveNext
 Loop

1 个答案:

答案 0 :(得分:1)

我们不需要知道脚本运行机器的文化,语言或区域设置都不是数据保存服务器,因为日期/时间值的内部表示不依赖于那些......

对于日期/时间条目,使用日期文字,即表示哈希符号(#)中包含的日期和/或时间的字符串,DateSerial / TimeSerial函数或(不推荐)串。下一个Start_Date变量条目都是等效的:

Dim Start_Date, Days, Combined_Date
 'If Start_Date should be "1st august 2014"
 'String (not recommended)   
 Start_Date = "1/8/2014"
 'Literal
 Start_Date = #2014-08-01#  ' ISO format
 Start_Date = #01-Aug-2014#
 Start_Date = #Aug/1/2014#
 Start_Date = #8/1/2014#
 Start_Date = #8-1-2014#
 'DateSerial() function
 Start_Date = DateSerial( 2014, 8, 1)

下一行的两行代码片段应如下所示:

 Days          = 27
 Combined_Date = DateAdd("d", Days, Start_Date)  '28th august 2014

对于输出,我们使用FormatDateTime函数和适当的(内置于VBScript)日期格式常量:

  Wscript.Echo FormatDateTime( Start_Date, vbGeneralDate)
  Wscript.Echo FormatDateTime( Start_Date, vbLongDate)
  Wscript.Echo FormatDateTime( Start_Date, vbShortDate)

并且 - 我很害怕 - myUser.AccountExpirationDate不是vbscript日期时间变体,因此您可能不会对其应用FormatDateTime()函数!