Sign in to follow this  
Followers 0
lostangel556

Remotely creating Home drive

4 posts in this topic

Hi there,

I've been trying to create an script to remotely create an users home drive (taken from Active Directory)

Below is what i currently have, which does kinda do it in an roundabout way. But preferably im looking for something less script intensive that doesnt use Psexec to execute the command.

I'm hoping that someone may be able to help me recreate the script so that it uses WMI to create, share and set permissions on an server-side folder

e.g.

Folder created = e:\users\username

Share = username

permissions would be RW

Note that below im using an mirror account to get the install location of where to create the folder on the server.

If its possible to change this so that i can use WMI to run cmd on the remote computer with an given string(e.g. net share ....., md, cacls)

Any help would be most appreciated as this is starting to give me an headache. :)

CODE
#NoTrayIcon

;Program to Create an home drive remotely via the command line using switches

;Ran Via: homedrivecreate.exe <username to create> <username Mirrored>

;Uses Pstools Psexec to run commands on home drive server

;Includes

#Include <File.au3>

#include <Array.au3>

#Include <NetShare.au3>

;PsExec Install Commands

FileInstall("psexec.exe", @TempDir & "\psexec.exe",1)

RegWrite("HKCU\Software\Sysinternals\PsExec", "EulaAccepted", "REG_DWORD", "00000001")

$psexec = @TempDir & "\psexec.exe"

;Check to see if ran from command line and exit if not

If $CmdLine[0] = 0 Then

MsgBox(0, "Error", "Program can only be ran from command line. If running from command line, please ensure details are correct.")

;Delete psexec files and reg entries

FileDelete($psexec)

RegDelete("HKCU\Software\Sysinternals\PsExec")

Exit

EndIf

;Static Assignments

$Username = $CmdLine[1] ;Username from command line

$Mirror = $CmdLine[2] ;Mirror account from command line

$WmicTempFile = @TempDir & "\wmic.txt"

;Ldap Query basics

Const $ADS_NAME_INITTYPE_GC = 3

Const $ADS_NAME_TYPE_NT4 = 3

Const $ADS_NAME_TYPE_1779 = 1

$objRootDSE=ObjGet("LDAP://RootDSE")

$strDNSDomain = $objRootDSE.Get("defaultNamingContext")

$objTrans=ObjCreate("NameTranslate")

$objTrans.Init($ADS_NAME_INITTYPE_GC, "" )

$objTrans.Set($ADS_NAME_TYPE_1779, $strDNSDomain)

$strNetBIOSDomain = $objTrans.Get($ADS_NAME_TYPE_NT4)

$strNetBIOSDomain =StringLeft($strNetBIOSDomain,StringLen($strNetBIOSDomain)-1)

$objTrans.Set($ADS_NAME_TYPE_NT4, $strNetBIOSDomain & "\" & $Username)

$strUserDN = $objTrans.Get($ADS_NAME_TYPE_1779)

$objUser = ObjGet("LDAP://" & $strUserDN)

;Variables

$HomeDriveServer = GetHomeDriveServerName($objuser.Get("homeDirectory")) ;Uses Ldap query to get the servername to logon with

Func GetHomeDriveServerName($LHomeDrive) ;Gets the name of the server which the home drive resides on. Returns Server name

Dim $LHomeDriveArray

$LHomeDriveArray = StringSplit($LHomeDrive, "\")

MsgBox(0,"GetHomeDriveServerName", $LHomeDriveArray[3])

Return $LHomeDriveArray[3]

EndFunc

Func FindLocalHomeDrivePath() ;This Runs an Search on the selected Dc for the mirror accounts home folder and returns its location.

;RunWait("cmd.exe /c wmic /NODE:" & $HomeDriveServer & " /output:" & $WmicTempFile & " share get path", @SystemDir, @SW_HIDE)

Dim $ShareArray

$ShareArray = _Net_Share_ShareGetInfo($HomeDriveServer, $Mirror)

$strLen = StringLen($Mirror)

$LhomePath = StringTrimRight($ShareArray[6], $strLen)

$LhomePath = $LhomePath & $Username

MsgBox(0,"FindLocalHomeDrivePath", $LhomePath)

Return $LhomePath

EndFunc

Func CreateDrive() ;Creates the remote drive and set permissions

RunWait($psexec & " \\" & $HomeDriveServer & " " & @SystemDir & "cmd.exe /c" & " md " & FindLocalHomeDrivePath(), @ScriptDir, @SW_HIDE) ;Create folder

;RunWait($psexec & " \\" & $HomeDriveServer & "NET SHARE " & $Username & "=" & FindLocalHomeDrivePath() & "/GRANT:Everyone,FULL", @ScriptDir, @SW_HIDE) ;Share drive

;RunWait($psexec & " \\" & $HomeDriveServer & "CACLS " & FindLocalHomeDrivePath() & " /G " & $Username & ":C", @ScriptDir, @SW_HIDE) ;Grant Folder permissions

EndFunc

Func CleanupFiles() ;Cleans up the files used in this program

;Delete psexec files and reg entries

FileDelete($psexec)

RegDelete("HKCU\Software\Sysinternals\PsExec")

EndFunc

CreateDrive()

CleanupFiles()

Share this post


Link to post
Share on other sites



In an Active Directory environment it would be absurd to "push" a logon script using psexec, instead you use Group Policy.

1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

In an Active Directory environment it would be absurd to "push" a logon script using psexec, instead you use Group Policy.

i know, basically we have an 100+ server environment than spans over an uk wide Wan. Each individual server stores the users home folders for that location. what im making atm is an toolkit to make the lives on the service desk an bit easier by automating some tasks. e.g. creating an home drive and setting permissions for an new user in Edinburgh when the server we use to access the network is in london i.e. our current process:

1. logon to london jump off box,

2. get the home drive location from AD,

3. goto that locations Domain Controller via RDP

4. create Folder in its Users folder and share it

5. Set permissions

What im looking to do is make an script that gets the home directory from AD, creates the drive on the server and shares it, sets the permissions, All from passing it the username to create for and an mirror account.

Edited by lostangel556

Share this post


Link to post
Share on other sites

If anyones interested, i've figured out how to do it. ldap mixed in with wmi to share the drive

CODE
Func _CreateRemoteDrive($usr, $clone)

$HomeDriveServer = GetHomeDriveServerName(Ldap($clone, "homeDirectory")) ;Uses Ldap query to create drive on

$ShareArray = _Net_Share_ShareGetInfo($HomeDriveServer, $clone)

$DriveString = StringReplace($ShareArray[6], ":\", "$\")

$DriveString = StringReplace($DriveString, $clone, $usr)

$DriveString = "\\" & $HomeDriveServer & "\" & $DriveString

;Create Folder remotely

RunWait(@ComSpec & " /c md " & $DriveString, "", @SW_HIDE)

_CreateRemoteShare($usr, StringReplace($ShareArray[6], $clone, $usr), $HomeDriveServer)

;need to add part in here to set permissions (cacls remotely)

EndFunc

Func _CreateRemoteShare($ShareName, $ShareLoc, $Server)

Const $FILE_SHARE = 0

Const $MAXIMUM_CONNECTIONS = 25

$strComputer = $Server

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

$objNewShare = $objWMIService.Get("Win32_Share")

$errReturn = $objNewShare.Create($ShareLoc, $ShareName, $FILE_SHARE, $MAXIMUM_CONNECTIONS, "")

Return $errReturn

EndFunc

Func GetHomeDriveServerName($LHomeDrive) ;Gets the name of the server which the home drive resides on. Returns Server name

Local $LHomeDriveArray

$LHomeDriveArray = StringSplit($LHomeDrive, "\")

Return $LHomeDriveArray[3]

EndFunc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0