2014-05-05 45 views
0

我有一个脚本,它应该将Description的值复制到Department。 但脚本似乎只能工作50%。我有OU,有3个子OU的。 该脚本只更改第一个OU用户属性,但不更改其他2. 有人可能会检查脚本并告诉我它最新的错误。活动目录复制用户属性

脚本:

Option Explicit 

On Error Resume Next 
Dim objUser, objChild, objConnection, objRootDSE, objItem 
Dim WshShell, objFSO, strRoot, strDNSDomain, strContainer 
Dim strDescription, strsAMAccountName 
Dim strdepartmentAfter, strDirectory, strdepartmentBefore 
Dim i, intLogFlag 'no log exists 

i=1 
intLogFlag = 0 

Set WshShell = CreateObject("WScript.Shell") 

'Set current directory to Desktop & display on page 
strDirectory = WshShell.SpecialFolders("Desktop") & "\" 
Set objRootDSE = GetObject("LDAP://RootDSE") 
strDNSDomain = objRootDSE.Get("DefaultNamingContext") 
strContainer = strContainer & strDNSDomain 
'To do a subcontainer, add it after//ou=OUName, - include comma 
Set strRoot =GetObject("LDAP://OU=Kasutajad," & strDNSDomain) 
'Start Logging 
CreateLog() 
'**************************************************************** 
For each objChild in strRoot 
Select Case objChild.class 
Case "organizationalUnit","container" 
Call DATree 
End Select 
Next 

Sub DATree() 
OU.Filter="objectClass=user" 
For each user in OU 
If user.class="user" Then 
    On Error Resume Next 
    user.Department=user.description 
    If Err.Number = 0 Then 
     user.SetInfo 
     WScript.Echo "Copied:" & user.description & " for " & user.name 
    Else 
     On Error GoTo 0 
     WScript.Echo "No IPPHONE configured for " & user.name 
    End If 
    On Error GoTo 0 
end if 
i=i+1 
next 
End Sub 

i = i -1 
Wscript.Echo "Accounts = " & i 
Wscript.Quit 

'**************************************************************** 

Sub CreateLog() 
On Error Resume Next 
Dim objFile 
Dim strFile, strText 

'Create log file 
strFile = "UserDepartmentLog_" & Month(Date()) & "_" & Day(Date()) & ".txt" 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.CreateTextFile(strDirectory & strFile) 
Set objFile = Nothing 
'Write headers to the log file 
strText = "User Name,Date,Description,Department Before, Department After" 
Set objFile = objFSO.OpenTextFile(strDirectory & strFile, 8, True) 
objFile.WriteLine(strText) 
intLogFlag = 1 
Set objFSO = Nothing 
Set objFile = Nothing 
End Sub 

'**************************************************************** 

'Used to append the log for each computer the script is run against 

Sub WriteLog(strdescription, strAccountName, strDeptBefore, strDeptAfter) 
On Error Resume Next 
Dim objFile, objTextFile 
Dim strFile, strText 

strFile = "UserDepartmentLog_" & Month(Date()) & "_" & Day(Date()) & ".txt" 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
'Check to see if the log exists 
If intLogFlag = 1 Then 
'Write to the log 
strText = strAccountName & "," & Date() & "," & strDescription & "," & strDeptBefore &       "," & strDeptAfter 

Set objFile = objFSO.OpenTextFile(strDirectory & strFile, 8, True) 
objFile.WriteLine(strText) 
objFile.Close 
'Reset strText for later use 
strText = "" 
Else 'If the log doesn't exist, create it 
CreateLog() 
'Reset strText for later use 
strText = "" 
End If 
Set objTextFile = Nothing 
Set objFile = Nothing 
Set objFSO = Nothing 
End Sub 

回答

0

我建议你沟VBScript和切换到PowerShell的。所有这一切都可以用这个来代替(全部在一行上):

获取-ADUser便有-SearchBase 'OU = PowerShell的测试,DC = YOURDOM' -LDAPFilter '(sAMAccountName赋= *)' -Properties描述|的foreach对象{SET-ADUser便有$ _ $ -Department _。说明}

所有你需要做的,这是PowerShell的2.0(最好是3.0,但因为它更容易)和Active Directory Module on your client

希望这个命令足够详细以解决大部分问题。要知道的是,PowerShell命令倾向于返回对象*,而不是文本。 |是一个管道,所以第一个命令(Get-ADUser)的输出对象被发送到第二个命令(ForEach-Object)。 $ _表示管道中的对象。因此,在大括号内,我正在对管道中的对象执行Set-ADUser(Set是更新的PowerShell标准动词),将管道中对象的description属性的值应用于-Department属性。达达!

这被称为PowerShell单线程。我已经写了它作为一个脚本,它可能已经是这样的:

$users = Get-ADUser ` 
    -SearchBase 'OU=PowerShell Testing,DC=yourdom' ` 
    -LDAPFilter '(sAMAccountName=*)' -Properties description 

foreach ($user in $users) 
{ 
    Set-ADUser $user -Department $user.description 
} 

的`是PowerShell的连续字符。

我曾经做过很多VBS脚本 - 我有脚本,有几行代码。现在,如果我有选择的话,我不会用驳船杆来碰它。