Page 1 of 2 12>
Topic Options
#168734 - 2006-10-02 04:44 PM Howto share a folder from script
Fredu Offline
Fresh Scripter

Registered: 2006-10-02
Posts: 6
Hi,

I need to get a script working. I want to make a script that creates a specific folder on the users homedrvie when users log on. Then the folder that is created by the script is also shared with "test_%username%".
So I have the folder creation part done, but I really dont know how to make a scritp to share the created folder? Any help?

Thanks,
Fredu

Top
#168735 - 2006-10-02 04:49 PM Re: Howto share a folder from script
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Take a look at the DOS "net share" command:
Code:
C:\Temp>net help share
The syntax of this command is:


NET SHARE
sharename
sharename=drive:path [/USERS:number | /UNLIMITED]
[/REMARK:"text"]
[/CACHE:Manual | Documents| Programs | None ]
sharename [/USERS:number | /UNLIMITED]
[/REMARK:"text"]
[/CACHE:Manual | Documents | Programs | None]
{sharename | devicename | drive:path} /DELETE

NET SHARE makes a server's resources available to network users. When
used without options, it lists information about all resources being
shared on the computer. For each resource, Windows reports the
devicename(s) or pathname(s) and a descriptive comment associated with it.

sharename Is the network name of the shared resource. Type
NET SHARE with a sharename only to display information
about that share.
drive:path Specifies the absolute path of the directory to
be shared.
/USERS:number Sets the maximum number of users who can
simultaneously access the shared resource.
/UNLIMITED Specifies an unlimited number of users can
simultaneously access the shared resource
/REMARK:"text" Adds a descriptive comment about the resource.
Enclose the text in quotation marks.
devicename Is one or more printers (LPT1: through LPT9:)
shared by sharename.
/DELETE Stops sharing the resource.
/CACHE:Manual Enables manual client caching of programs and documents
from this share
/CACHE:Documents Enables automatic caching of documents from this share
/CACHE:Programs Enables automatic caching of documents and programs
from this share
/CACHE:None Disables caching from this share

NET HELP command | MORE displays Help one screen at a time.

C:\Temp>


Top
#168736 - 2006-10-02 04:50 PM Re: Howto share a folder from script
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Maybe CreateShare() can help you.
Top
#168737 - 2006-10-02 04:54 PM Re: Howto share a folder from script
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
BTW, if the users directory is shared, maybe you can use this to map a deep share?
Top
#168738 - 2006-10-02 05:01 PM Re: Howto share a folder from script
Fredu Offline
Fresh Scripter

Registered: 2006-10-02
Posts: 6
Hi,

thanks for fast replys.
But still I dont get it.
I tried with this: net share "test_%username"=\\server\share\%username%\foldertobeshare but this dosn't work. And the folder is created under users homefolders \\server\users\%username%\testfolder. I have a lot of folders to work with, and this is why I would like to get it scripted. I can do it manually but that takes ages to do.

-Fredu

Top
#168739 - 2006-10-02 05:05 PM Re: Howto share a folder from script
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
I am sure Rad's code is better but here is the code I use:
Code:

$ShareName = "TUser"
$ServerName = "FileSVR"
$SharePath = "\\" + $ServerName + "\Users\" + $ShareName
$Discription = "User Share for their My Documents."

CreateShareFolder($ServerName,$ShareName,$SharePath,$Discription)

Function CreateShareFolder($ServerName,$ShareName,$SharePath,$Discription)

$FILE_SHARE = 0
$MAXIMUM_CONNECTIONS = -1 ; -1 allows unlimited connections.
$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $ServerName + "\root\cimv2")
$objNewShare = $objWMIService.Get("Win32_Share")
$FileShare = $objNewShare.Create($SharePath,$ShareName,$FILE_SHARE,$MAXIMUM_CONNECTIONS,$Discription)

EndFunction



Edited by benny69 (2006-10-02 05:08 PM)
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#168740 - 2006-10-02 05:11 PM Re: Howto share a folder from script
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
My fault, I missed the reference to the users home share so ignore the "net share" stuff

I don't think that you are going to be able to do this in the way that you want as creating a (new) share on the server is going to require admin rights on server - I'm assuming that your users don't have admin rights on your file servers!?

Why do you need to create the share? Perhaps we can suggest an alternative.

Top
#168741 - 2006-10-02 05:30 PM Re: Howto share a folder from script
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Quote:


I'm assuming that your users don't have admin rights on your file servers!?




That would be most preferable.
Quote:


Perhaps we can suggest an alternative.




Quote:


If the users directory is shared, maybe you can use this to map a deep share?




Top
#168742 - 2006-10-02 05:31 PM Re: Howto share a folder from script
Fredu Offline
Fresh Scripter

Registered: 2006-10-02
Posts: 6
So I need to make a folder on every users homefolder. Lets call the folder "test". So then we would have \\server\users\username\test folder.

And then I need the folder to be shared with "test_username".

We have a system that checks for a folder and then it dumps automatically files there. It can be any script that I can run from command line. It only need to be run once. When I have the folders there and shared I'm happy. But it would be nice to have it on the login script because when new users come in, then it would be created when she or he logs in.

Here is what I have so far...

$Directory="\\server\users\"+@USERID
$DirectoryTemp="\\server\users\"+@USERID+"\testfolder"
If Not Exist ($Directory)
MD $Directory
Shell "%COMSPEC% /c Echo Y| Cacls \\server\users\"+@USERID+" /e /c /g "+@DOMAIN+\+@USERID+":F"
EndIf

If Not Exist ($DirectoryTemp)
MD $DirectoryTemp

Shell "%COMSPEC% /c Echo Y| Cacls \\server\users\"+@USERID+"\testfolder /e /c /g "+@DOMAIN+\+@USERID+":F"
EndIf

$ShareName = "test"
$ServerName = "server"
$SharePath = "\\" + $ServerName + "\Users\" + "\%username%\" + $ShareName
$Discription = "test folder share."

CreateShareFolder($ServerName,$ShareName,$SharePath,$Discription)

Function CreateShareFolder($ServerName,$ShareName,$SharePath,$Discription)

$FILE_SHARE = 0
$MAXIMUM_CONNECTIONS = -1 ; -1 allows unlimited connections.
$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $ServerName + "\root\cimv2")
$objNewShare = $objWMIService.Get("Win32_Share")
$FileShare = $objNewShare.Create($SharePath,$ShareName,$FILE_SHARE,$MAXIMUM_CONNECTIONS,$Discription)

EndFunction

-Fredu


Edited by Fredu (2006-10-02 05:33 PM)

Top
#168743 - 2006-10-02 05:45 PM Re: Howto share a folder from script
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I have not read through all your code yet, but at first glance, I seem to understand that your users have at least write access to the \\server\users share. I think this is not good. I think user directories should be created by an admin. IMHO \\server\users should be read-only for users on NTFS. From \\server\users\%username%, new NTFS security should be set to allow the user exclusive modify or full access.
Top
#168744 - 2006-10-02 05:59 PM Re: Howto share a folder from script
Fredu Offline
Fresh Scripter

Registered: 2006-10-02
Posts: 6
Users have only list access to users folder. And then they have modify right to their own home folder. But what I need is script to create a folder "test" in all directories and share it with "username_test".

-Fredu

Top
#168745 - 2006-10-02 06:05 PM Re: Howto share a folder from script
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
If I understood this correctly, this is for use during logon in the logon script, isn't it?
If the users have only list access to \\server\users, I do not think this works
Code:

$Directory="\\server\users\"+@USERID
If Not Exist ($Directory)
MD $Directory
Shell "%COMSPEC% /c Echo Y| Cacls \\server\users\"+@USERID+" /e /c /g "+@DOMAIN+\+@USERID+":F"
EndIf


Top
#168746 - 2006-10-02 06:13 PM Re: Howto share a folder from script
Fredu Offline
Fresh Scripter

Registered: 2006-10-02
Posts: 6
Ok. So is there something I can do to make a script for this purpose. Or if I have a script that I can run in the server itself? Skip the login, if it not possibel. Just to get the +1000 folders done. And with new ones I can make the shares manually.

-Fredu

Top
#168747 - 2006-10-02 06:23 PM Re: Howto share a folder from script
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I think you should do this as Admin.
Create an array of $Users
Loop through the array:
Code:

For Each $User in $Users
; Do stuff
Next


Do (almost) the same as you are doing now, as administrator
the @USERID would become $User

Top
#168748 - 2006-10-02 07:05 PM Re: Howto share a folder from script
Fredu Offline
Fresh Scripter

Registered: 2006-10-02
Posts: 6
hmm, I'm lost now. So how would the script look like?
Top
#168749 - 2006-10-03 01:06 AM Re: Howto share a folder from script
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I presume this code will list all your users. I do not know if it is good practice to run your code directly against the list of names, but maybe it can help you to generate shorter lists of users.
Code:

If NOT @LOGONMODE
Break On
Else
Break Off
EndIf
Dim $RC
$RC = SetOption("Explicit","On")
$RC = SetOption("NoVarsInStrings","On")
$RC = SetOption("NoMacrosInStrings","On")
$RC = SetOption("WrapAtEOL","On")

Dim $objDomain, $objUser

$objDomain = GetObject("WinNT://YourDomainName")
$objDomain.Filter = "User",""

For Each $objUser in $objDomain
? "User name: " + $objUser.Name
? "Description: " + $objUser.Description
? "Logon script path: " + $objUser.LoginScript
?
Next


Top
#168750 - 2006-10-03 08:50 AM Re: Howto share a folder from script
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Afaik you cannot create a share from a UNC path. ie \\server\share\foldertobeshared

At any rate, any script able to make shares won't let you "script" share permissions :/ you'd have to either make the share yourself and then script the perms or script the share and set the perms manually.

Top
#168751 - 2006-10-03 09:00 AM Re: Howto share a folder from script
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
Does XCACLS.VBS address this Apronk?

Have not tried myself.

Top
#168752 - 2006-10-03 09:10 AM Re: Howto share a folder from script
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Haven't used XCACLS yet to verify this. Mainly using WMI and other scripts to create a share leave out the registry settings that contain the security data, which is why my SharePerms function won't work on them. When manually creating a Share the shareperms function does work.
Top
#168753 - 2006-10-03 11:23 AM Re: Howto share a folder from script
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
A. Pronk,
You say shares can be created with WMI
I think it is in the script Fredu posted. Don't you think it will work? I presume he should just run (most of his) script as administrator.
This is a quote from his script:
Code:

Function CreateShareFolder($ServerName,$ShareName,$SharePath,$Discription)

$FILE_SHARE = 0
$MAXIMUM_CONNECTIONS = -1 ; -1 allows unlimited connections.
$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $ServerName + "\root\cimv2")
$objNewShare = $objWMIService.Get("Win32_Share")
$FileShare = $objNewShare.Create($SharePath,$ShareName,$FILE_SHARE,$MAXIMUM_CONNECTIONS,$Discription)

EndFunction


Top
Page 1 of 2 12>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 883 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.075 seconds in which 0.025 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org