Re: Verifying a user without knowing his precise OU by Randy
Randy
Thu Sep 14 09:13:51 CDT 2006
Try this script - may need to modify a bit, run from a DOS prompt or command
line as "CScript orphan.vbs" After inputting the server name and location
of home folders on the server, it creates a csv file that tells you what
folders exist, who owns them with the full OU or that the folder is bad.
The first folder always seems to say the user is not found, some type of
error. This is assumming that the folder name and share name are the user's
pre-Win2000 user ID. This is not what you had for a request, but I believe
it fits your need much better.
Randy
--------Script Orphan.vbs-------------------------------
'=================
'Name: orphanV2.vbs
'Author: Tim Sullivan
'Date: 04.01.2005
'Info: This is a script to check for orphaned home folders. It reads the
name of the folder, and
' looks for a corresponding AD user account. The original concept for this
script was done by
' Alan Kaplan (alan@akaplan.com), and a lot has been borrowed from it.
'
' I made this one to look at the folder names, rather than the SID of the
folder owner. This
' worked better for me, as our home folder names are the same as our domain
usernames.
'=================
Dim AppendOut
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTrans = CreateObject("NameTranslate")
Set wshShell = WScript.CreateObject ("WScript.Shell")
strDomain = "Corp"
strComputer = "ServerName"
strDNSDomain = "DC=corp,DC=inet"
objTrans.Init 3, strDNSDomain
objTrans.Set 1, strDNSDomain
'=================
'Pre script running checks.
'===
syscheck
'===
'If Wscript is being used, script is restarted with Cscript instead.
'===
If (Not IsCScript()) Then
WshShell.Run "CScript.exe " & quote & WScript.ScriptFullName & quote, 1,
true
WScript.Quit
End If
'=================
'User input area and error checking.
'=================
strTargetServer = Inputbox("Enter the target server name.", "Target Server
Name", strComputer)
If strTargetServer = "" Then
quitmessage = "Target server cannot be blank. Exiting!"
Abort
End If
strTargetServer = UCase(strTargetServer)
strStartFolder = InputBox("Enter the starting folder path. Be sure not to
put any leading or trailing slashes. Follow the example in the box.",
"Search Start Path", "D:\Users")
If strStartFolder = "" Then
quitmessage = "Starting folder cannot be blank. Exiting!"
Abort
End If
'=====
'Convert inputs to UNC
'=====
strUNCStart = "\\" & strTargetServer &"\"& Replace(strStartFolder,":","$")
WScript.Echo strUNCStart
If not objFSO.FolderExists(strUNCStart) Then
quitmessage = strUNCStart & " Not Found"
abort
End If
'====
'Prepare our output log file
'====
logsetup
'=================
'=================
'Here we get our list of folders, and call our function to actually find
'our little lost orphans.
'====
Set objFolder = objFSO.GetFolder(strUNCStart)
Set colSubfolders = objFolder.Subfolders
For Each objSubFolder in colSubfolders
strCheckFolder = objSubFolder.name
Validate(strCheckFolder)
GetADName(strCheckFolder)
If strADPres = "Present" Then
EchoAndLog strUNCStart & "\" & objSubFolder.Name & ", Folder/User is
ok, User is: " & strUserDN
Else
EchoAndLog strUNCStart & "\" & objSubFolder.Name & ", Folder/User is
bad. User Not Found!"
End If
Next
'=================
'Functions and sub-routines
'=================
'System check functions. Checks VBScript version, and verifies WMI is
installed.
Sub syscheck()
Dim major,minor, ver, key, key2
major = (ScriptEngineMinorVersion())
Minor = (ScriptEngineMinorVersion())/10
Ver = major + minor
If err.number or ver < 5.5 then
quitmessage = "You have WScript Version " & ver & ". Please load
Version 5.5"
End If
err.clear
key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed
Components\{E92B03AB-B707-11d2-9CBD-0000F87A369E}\version"
key2 = WshShell.RegRead (key)
if err <> 0 then
quitmessage = quitmessage & "ADSI must be installed on local
workstation to continue" & vbCrLf
abort
End if
End Sub
'===
'Function to handle aborting the script.
'===
Function Abort() 'error message handler
wshShell.Popup quitmessage,0,"Abort",vbCritical
WScript.Quit
End Function
'====
'Sub routine for preparing the output log file.
'====
Sub logsetup()
On Error goto 0
Dim arTemp
logpath= wshShell.SpecialFolders("Desktop") & "\"
arTemp= Split(WScript.ScriptName,".") 'Script Name found"
logfile = logpath & strTargetServer & artemp(0)& ".csv" 'append .CSV
'setup Log
WriteType = 2 ' forwriting 'presume for writing. Usually
done as constant...
If objFSO.FileExists(logfile) Then
retval = MsgBox("Logfile Exists, do you want to append?",vbyesno +
vbdefaultbutton1,"Old Log File")
If retval = vbyes Then
writeType = 8 'change type to append
End If
End If
On Error Resume next
Set AppendOut = objFSO.OpenTextFile(logfile, WriteType, True)
If Err <> 0 Then
MsgBox "You must close the log file!",vbcritical +
vbinformation,"Fatal Error"
WScript.Quit
End If
On Error goto 0
If WriteType = 2 Then 'only write header if new file
Appendout.writeline "Folder UNC,User"
End If
End sub
'=====
'Output subroutine
'=====
Sub EchoAndLog (message)
Wscript.Echo message
AppendOut.WriteLine message
End Sub
'===
'Function for determining if CScript is being used.
'===
Function IsCScript()
If (InStr(UCase(WScript.FullName), "CSCRIPT") <> 0) Then
IsCScript = True
Else
IsCScript = False
End If
End Function
'===
'Function for comparing foldername against AD.
'===
Function Validate(strCheckFldr)
On Error Resume Next
Dim objSAMUser
Err.Clear
Set objSAMUser = GetObject("WinNT://" & strDomain & "/" & strCheckFolder &
",user")
If Err.Number = 0 Then
strADPres = "Present"
Validate = True
Else
strADPres = "NotPresent"
Validate = False
End If
Set objSAMUser = Nothing
End Function
'===
'Function to get Distinguished Name
'===
Function GetADName(strCheckFolder)
' Use the NameTranslate object to convert the NT user name to the
' Distinguished Name required for the LDAP provider.
objTrans.Init 1, strDomain
' Trap error if sAMAccountName does not exist.
On Error Resume Next
Err.Clear
objTrans.Set 3, strDomain & "\" & strCheckFolder
strUserDN = objTrans.Get(2)
End Function
'================
'End Of Script
'================
--------Script Orphan.vbs-------------------------------
"Mathieu Paquette" <leprince@hotmail.com> wrote in message
news:ep%23l7zp1GHA.1288@TK2MSFTNGP03.phx.gbl...
> Hello,
>
> Here's the task at hand. I have a user drive which has twice as many
> folders as we have employees. Therefore, someone hasn't done its job of
> deleting the users. Now, I'm not talking about having 20 employees and 40
> folders; it's in the thousands.
>
> I did a dir /b and outputted the result in a text file, which is easily
> parsable in vbscript. Folders are name through the pre-Win2000 username,
> which is cool.
>
> However, the thing is, the users can be located in various OUs, for policy
> and site reasons. Therefore, I don't want to go through EACH OU; I'd like
> to be able to input to the script the name of the user and search AD
> automatically through a function of some sort.
>
> Is it even doable ?
>
> Thank you !
>
> Mathieu
>