briannz556
(Getting the hang of it)
2005-07-27 01:44 AM
Finding full path to homedirectory

Can anyone help me with a 'simple' scripting issue I have with locating the full path to a user's homedirectory please?

$user = GetObject("LDAP://cn=sillykid,ou=Test users,DC=greyhigh,DC=school,DC=nz")
? $user.homedirectory

returns

\\legolas\sillykid$

What I'd like is to be able to determine the full path to the homedirectory which in this case happens to be: D:\Homedirs\Students\sillykid where D: is on the server \\legolas.

Any thread would be appreciated.

Thank you


NTDOCAdministrator
(KiX Master)
2005-07-27 02:34 AM
Re: Finding full path to homedirectory

Well what are the client types?

You can use ADSI/WMI to gather the locations of the shares I'm sure.

Let me look and see if I can find a snipet of code to do it

What OS is the Sever running NT4/2000/AD is it NT4 or AD Domain?

 


NTDOCAdministrator
(KiX Master)
2005-07-27 02:41 AM
Re: Finding full path to homedirectory

This will list ALL shares. Using this you could do an InStr check for a specific user name if your user folders are named using the users logonID

Code:

Break On
Dim $SO
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
$SO=SetOption('WrapAtEOL','On')
 

Dim $sComputer,$Servers,$objWMIService,$colShares,$objShare,$File,$Report
$sComputer='msef002'
$objWMIService = GetObject("winmgmts:" + "{impersonationLevel=impersonate}!\\" + $sComputer + "\root\cimv2")
$colShares = $objWMIService.ExecQuery("Select * from Win32_Share")
For Each $objShare In $colShares
$sComputer + " " + $objShare.Name + ' Path: ' + $objShare.Path ?
Next


 


LonkeroAdministrator
(KiX Master Guru)
2005-07-27 02:48 AM
Re: Finding full path to homedirectory

that share thing is same you get with @homeshr

now... you can do it with adsi winnt provider or wmi.
here is the wmi way:
Code:

$server = "someServer"
$share = "someShare"

$objWMIService = GetObject("winmgmts:\\" + $server + "\root\cimv2")
For each $objItem in $objWMIService.ExecQuery("Select * from Win32_Share",,48)
if $share = $objItem.name
"there shared folder is: " $objItem.path ?
endif
next



I admit, I didn't write ready code but hope that gives you a pointer to the right direction...


LonkeroAdministrator
(KiX Master Guru)
2005-07-27 02:49 AM
Re: Finding full path to homedirectory

wow! I was slow typing that...

LonkeroAdministrator
(KiX Master Guru)
2005-07-27 02:52 AM
Re: Finding full path to homedirectory

for adsi, try this:
Code:

for each $share in GetObject("WinNT://"+@wksta+"/LanmanServer")
$share.name " " $share.path ?
next
get $



briannz556
(Getting the hang of it)
2005-07-27 04:47 AM
Re: Finding full path to homedirectory

Thanks for that wonderful help.

I've given up worrying about all I don't know and eagerly look forward to what i will learn. Will go and trial it and then put it into use.

Much appreciated.


briannz556
(Getting the hang of it)
2005-07-27 07:46 AM
Re: Finding full path to homedirectory

Thanks to your great thoughts, I found what I wanted and massaged the code to suit my needs. Once again, great service.

Code:

$strComputer = "."
$strNamespace = "\root\cimv2"
$strClassName = "Win32_Share"
$strKeyName = "Name"
$strKeyValue = "sillykid$"

$objSWbemServices = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $strComputer + $strNamespace)
$objInstance = $objSWbemServices.Get($strClassName + "." + $strKeyName + "='" + $strKeyValue + "'")
? $objInstance.path



NTDOCAdministrator
(KiX Master)
2005-07-27 08:20 AM
Re: Finding full path to homedirectory

Question. Why do you want or need the actual path? Why isn't the servername\sillykid$ good enough?

Is this for one user only? For multiple computers reading a list and operating against it would be faster and seemingly more useful than a one machine call. You could have went into the GUI and found that info just as quick for one machine.
 


briannz556
(Getting the hang of it)
2005-07-27 10:36 AM
Re: Finding full path to homedirectory

Why do I want this? Hmmm... Long story.

At various stages in the school year I place AD users who have left (and even whole OU) in a storage OU called "DeleteMeLater". I've found that if I use Active Directory Users and Containers in my Console to delete these users then i have a terrible job finding their homedirs which don't get wiped when they are wiped. Also, occassionally, I find homedirs can't be removed from the server even manually. For example, I've got two such folders that give @error = 9 when I script their deletion.

So, I'm building a Kixform which allows me to:
1. loops through all users in DeleteMeLater and move their homedirs into a special folder so they are in one place.
2. delete each user in DeleteMeLater
3. delete each homedir
4. log any errors
5. Place troublesome homedirs in a folder marker "PigsOfThings" ... or something less polite

If you think there is a much better solution I'd love to know.


NTDOCAdministrator
(KiX Master)
2005-07-27 11:01 AM
Re: Finding full path to homedirectory

Yeah, script the whole move and removal process.

Late here tonight, but I'm sure either as I get time or as other members here pitch in we can help you to automate the whole process better.

1. Disable the accounts of users who have left
2. Unshare their folder
3. Move their folder to some folder like OBSOLETE_ACCOUNTS
4. Run a script that checks the age of those folders and after say 30 days or some date you feel comfortable with, then have the script confirm the account is still there and delete the account including the mailbox, then have it delete the folder and files.


The entire thing should be able to be automated by a script that runs either on schedule and maybe reads a .INI file to determine what action to take against what account.

 


LonkeroAdministrator
(KiX Master Guru)
2005-07-27 02:52 PM
Re: Finding full path to homedirectory

not sure about that fully automated.
if this process is done once a year, I think his approach with a slick KF stuff could be lot cooler


NTDOCAdministrator
(KiX Master)
2005-07-27 07:32 PM
Re: Finding full path to homedirectory

Well not 100% automated, he still has to manually enter in who has left

But after that either a fully automated unseen script, or a KiXform should be quite doable.
 


LonkeroAdministrator
(KiX Master Guru)
2005-07-27 10:26 PM
Re: Finding full path to homedirectory

doable sure, but usefulness should take a rating too, don't you think?

NTDOCAdministrator
(KiX Master)
2005-07-27 11:38 PM
Re: Finding full path to homedirectory

Well we're starting to veer off topic so I'll let briannz556 decide if it's useful or not. I would think so if you cycle through hundreds of users every few months.
 


briannz556
(Getting the hang of it)
2005-07-31 07:40 AM
Re: Finding full path to homedirectory

Hi Guys
Script finished and works. Just testing and improving reporting. Only query now is about deleting mailboxes from Exchange 2000 server.

I've been reading the white paper "H:\My Documents\Kix Library\Server Tasks\research stuff\Automating Exchange 2000 Management with Windows Script Host.htm" and it seems that:

1. I must run my script from the exchange server if i'm to do all planned (remove share, delete mailbox, delete home directory and remove user). Correct?

2. Use CDOEXM to delete mailbox. Correct?

Happy to pursue (2) but just wondering if there are any previous queries on the board regarding mailbox creation/deletion using kix? I've searched but found nothing exactly relevant but maybe my eyes aren't as good as yours or query suitably constructed.

Once all complete I'm happy to post for others to use (some 400 - 500 lines). Is this acceptable?


LonkeroAdministrator
(KiX Master Guru)
2005-07-31 05:26 PM
Re: Finding full path to homedirectory

1 - no, you can connect to exchange LDAP remotely as well.

briannz556
(Getting the hang of it)
2005-08-08 01:31 AM
Re: Finding full path to homedirectory

Back Again
I've completed all my tasks except for one: I need to allow for a corrupted homedirectory being unable to be deleted and which could be on a different server to that where the script is running.

My plan was to move the corrupted directory to a different spot on the same server where it exists. This is where I've been tearing my hair out. What is the best method to do such? Below is some trivial testing code. I've tried two options:

Option 1
Code:

$Computer = '.' ; local computer
$to = 'D:\Accounts\Corrupted Accounts\Rubbish'
$from = 'D:\Accounts\Rubbish'
$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $Computer + "\root\cimv2")
? "Error= " + @ERROR + ' : ' + @SERROR + ' at point ' + '1'
$colFolders = $objWMIService.ExecQuery("SELECT * FROM Win32_Directory WHERE Name = '" + $from + "'")
? "Error= " + @ERROR + ' : ' + @SERROR + ' at point ' + '2'

For Each $objFolder in $colFolders
$result = $objFolder.Rename($to)
? "Error= " + @ERROR + ' : ' + @SERROR + ' at point ' + '3'
Next


but this returns an empty collection as the third error line never appears and the folder doesn't get moved.

Option 2 (which works)
Code:

$objFSO = CreateObject("Scripting.FileSystemObject")
$to = 'D:\Accounts\Corrupted Accounts\Rubbish'
$from = 'D:\Accounts\Rubbish'
$objFSO.MoveFolder($from, $to)



I'd really like to know why Option 1 doesn't work. Also, can FSO be used to bind to a folder on another server? if so, how do you write to path to the folders when it is on another server?


NTDOCAdministrator
(KiX Master)
2005-08-08 07:33 AM
Re: Finding full path to homedirectory

Well if the folder is truely corrupted and can not be deleted then you won't be able to move it either. You should run CHKDSK (VOLUME) /F /R

Which should then repair any corruption and allow you to remove the folder.


briannz556
(Getting the hang of it)
2005-08-08 11:08 AM
Re: Finding full path to homedirectory

Move is not perhaps the best word. Rename visually moves the folder and is what I meant. I have such folders (not many I grant) eg error # 9 in ASE. CHCKDSK hasn't seemed to make any difference when i ran it at the start of the year.

But to get back to my query: is there a simple way to move/rename a folder on another server? If so, a thread would be greatly appreciated. If not, I'll change my design plan. Your expert thoughts would be much appreciated.

Thanks


NTDOCAdministrator
(KiX Master)
2005-08-09 11:32 AM
Re: Finding full path to homedirectory

Sure you could shell out to DOS and use RENAME however if the folder can not be renamed from GUI or DOS then scripting wouldn't work either.

CHKDSK should normally be able to repair the corruption and allow a rename, move, delete, or other similar actions.
Just because you ran a CHKDSK a year or even a few days ago does not mean that something invalid has happened in the file system today.


LonkeroAdministrator
(KiX Master Guru)
2005-08-09 12:51 PM
Re: Finding full path to homedirectory

hmm...
the rename problem really is about does the renaming happen in copy mode when done remotely...

mkey, anyway, this discussion is no more tied to this thread and thus I ask that you brianz start a new thread that honors your script.
this way it will be lot easier to find later.


briannz556
(Getting the hang of it)
2005-08-19 03:48 AM
Re: Finding full path to homedirectory

Everything is now finished and I've posted a working script to the Scripts section of this site. I don't know how the moderators have a life outside of this artificial world but I thank them for their help in the past and also for help I'm sure i'll ask for in the future.

LonkeroAdministrator
(KiX Master Guru)
2005-08-19 03:55 AM
Re: Finding full path to homedirectory

some serious something licking there.
I'm glad I'm not moderator

and thanks for the script.