{"id":83,"date":"2007-12-19T09:46:44","date_gmt":"2007-12-19T09:46:44","guid":{"rendered":"https:\/\/www.wapshere.com\/missmiis\/?page_id=83"},"modified":"2007-12-19T10:40:35","modified_gmt":"2007-12-19T10:40:35","slug":"import-user-attributes-in-ad-from-excel-using-cns","status":"publish","type":"page","link":"https:\/\/www.wapshere.com\/missmiis\/code-snippets\/import-user-attributes-in-ad-from-excel-using-cns","title":{"rendered":"vbscript: Import User attributes in AD from Excel &#8211; using CNs"},"content":{"rendered":"<p><code><br \/>\n'************************************************************************************<br \/>\n' import2AD.vbs<br \/>\n'<br \/>\n' usage: cscript import2AD.vbs excel_file<br \/>\n'<br \/>\n' This script reads a list of user CNs, attributes and values from an Excel spreadsheet.<br \/>\n' It then modifies the user in Active Directory - setting attribute = value<br \/>\n'<br \/>\n' Written by Carol Wapshere<br \/>\n' 18th December 2007<br \/>\n'<br \/>\n'************************************************************************************<\/p>\n<p>Dim arrCNs()<br \/>\nDim arrAttribs()<br \/>\nDim arrValues()<br \/>\nSet fso = CreateObject(\"Scripting.FileSystemObject\")<br \/>\nset objArgs = WScript.Arguments<\/p>\n<p>'*** Usage ***<br \/>\nIf WScript.Arguments.Count &lt;&gt; 1 Then<br \/>\nUsage<br \/>\nElseIf objArgs(0) = \"\/?\" Then<br \/>\nUsage<br \/>\nElse<br \/>\nExcelFileName = objArgs(0)<br \/>\nEnd If<\/p>\n<p>'*** If no path given assume current folder ***<br \/>\nIf InStr(ExcelFileName, \"\\\") = 0 Then<br \/>\nExcelFileName = fso.GetAbsolutePathName(\"\") &amp; \"\\\" &amp; ExcelFileName<br \/>\nEnd If<\/p>\n<p>'*** Populate arrays from spreadsheet ***<br \/>\nGetColumn arrCNs, ExcelFileName, 1<br \/>\nGetColumn arrAttribs, ExcelFileName, 2<br \/>\nGetColumn arrValues, ExcelFileName, 3<\/p>\n<p>'*** Check column lengths ***<br \/>\nIf Ubound(arrCNs) &lt;&gt; Ubound(arrAttribs) Or Ubound(arrCNs) &lt;&gt; Ubound(arrValues) Then<br \/>\nErrorHandler(\"Column lengths are uneven\")<br \/>\nEnd If<\/p>\n<p>'*** Loop through the CNs, setting attribute values ***<br \/>\nFor i = 0 To Ubound(arrCNs)<br \/>\nIf FindDN(arrCNs(i), userDN) = 1 Then<br \/>\nUpdateAttrib userDN, arrCNs(i), arrAttribs(i), arrValues(i)<br \/>\nEnd If<br \/>\nNext<\/p>\n<p>'************************************************************************************<br \/>\n' SUBROUTINES<br \/>\n'************************************************************************************<\/p>\n<p>Sub GetColumn(arrExcelValues,ExcelFileName,n)<\/p>\n<p>Set objExcel = CreateObject(\"Excel.Application\")<br \/>\nSet objWorkbook = objExcel.Workbooks.Open(ExcelFileName)<br \/>\nobjExcel.Visible = True<\/p>\n<p>i = 1<br \/>\nx = 0<\/p>\n<p>Do Until objExcel.Cells(i, n).Value = \"\"<br \/>\nReDim Preserve arrExcelValues(x)<br \/>\narrExcelValues(x) = objExcel.Cells(i, n).Value<br \/>\ni = i + 1<br \/>\nx = x + 1<br \/>\nLoop<\/p>\n<p>objExcel.Quit<\/p>\n<p>End Sub<\/p>\n<p>Function FindDN(CN, DN)<\/p>\n<p>On Error Resume Next<br \/>\nConst ADS_SCOPE_SUBTREE = 2<\/p>\n<p>Set objConnection = CreateObject(\"ADODB.Connection\")<br \/>\nSet objCommand = CreateObject(\"ADODB.Command\")<br \/>\nobjConnection.Provider = \"ADsDSOObject\"<br \/>\nobjConnection.Open \"Active Directory Provider\"<br \/>\nSet objCommand.ActiveConnection = objConnection<\/p>\n<p>objCommand.Properties(\"Page Size\") = 1000<br \/>\nobjCommand.Properties(\"Searchscope\") = ADS_SCOPE_SUBTREE<\/p>\n<p>objCommand.CommandText = _<br \/>\n\"SELECT AdsPath FROM 'LDAP:\/\/dc=fabrikam,dc=com' WHERE objectClass='user'\"_<br \/>\n&amp; \" AND Name='\" &amp; CN &amp; \"'\"<br \/>\nSet objRecordSet = objCommand.Execute<\/p>\n<p>If objRecordSet.RecordCount = 0 Then<br \/>\nwscript.echo CN &amp; \" not found\"<br \/>\nFindDN = 0<br \/>\nElseIf objRecordSet.RecordCount &gt; 1 Then<br \/>\nwscript.echo \"More than one user object found with the name \" &amp; CN<br \/>\nFindDN = 0<br \/>\nElse<br \/>\nFindDN = 1<br \/>\nobjRecordSet.MoveFirst<br \/>\nDN = objRecordSet.Fields(\"AdsPath\").Value<br \/>\nEnd If<br \/>\nEnd Function<\/p>\n<p>Sub UpdateAttrib(DN, CN, attrib, value)<br \/>\nOn Error Resume Next<\/p>\n<p>Set objUser = GetObject(DN)<\/p>\n<p>If objUser.Name=\"\" Then<br \/>\nwscript.echo DN &amp; \" not found\"<br \/>\nElse<br \/>\nobjUser.Put attrib, value<br \/>\nobjUser.SetInfo<br \/>\nSet objUser = GetObject(DN)<br \/>\nIf objUser.Get(attrib) = value Then<br \/>\nwscript.echo CN &amp; \", \" &amp; attrib &amp; \" successfully updated.\"<br \/>\nEnd If<br \/>\nEnd If<\/p>\n<p>End Sub<\/p>\n<p>Sub Usage<br \/>\nwscript.echo<br \/>\nwscript.echo \"This script imports values from an Excel spreadsheet into AD.\"<br \/>\nwscript.echo<br \/>\nwscript.echo \"It will OVERWRITE the current value, \"<br \/>\nwscript.echo \"and must be used for SINGLE-VALUED attributes only.\"<br \/>\nwscript.echo<br \/>\nwscript.echo \"usage: cscript import2AD spreadsheet\"<br \/>\nwscript.echo<br \/>\nwscript.echo \"The spreadsheet must have THREE columns, in the following configuration:\"<br \/>\nwscript.echo \"Column 1: Object CN\"<br \/>\nwscript.echo \"Column 2: Attribute Name\"<br \/>\nwscript.echo \"Column 3: New Value\"<br \/>\nwscript.echo<br \/>\nwscript.echo \"The spreadsheet should not have blank rows or column headings.\"<br \/>\nWScript.Quit(0)<br \/>\nEnd Sub<\/p>\n<p>Sub ErrorHandler(ErrorMessage)<br \/>\nwscript.echo \"Error: \" &amp; ErrorMessage<br \/>\nWScript.Quit(1)<br \/>\nEnd Sub<\/p>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8216;************************************************************************************ &#8216; import2AD.vbs &#8216; &#8216; usage: cscript import2AD.vbs excel_file &#8216; &#8216; This script reads a list of user CNs, attributes and values from an Excel spreadsheet. &#8216; It then modifies the user in Active Directory &#8211; setting attribute = value &#8216; &#8216; Written by Carol Wapshere &#8216; 18th December 2007 &#8216; &#8216;************************************************************************************ Dim arrCNs() Dim&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":30,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-83","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/Pkp1o-1l","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/pages\/83","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/comments?post=83"}],"version-history":[{"count":0,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/pages\/83\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/pages\/30"}],"wp:attachment":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/media?parent=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}