How can I share multiple folders and apply share permissions using powershell?

I have folder called userdata.
Within the userdata folder are the users home drives. The folder name is the same as their user account name:

asmith
jsmith
ataylor and so forth. (1000's)

We are robocopying user data from one server to another. I need to recreate the share once it has moved and apply the permissions.
I'd really like to use powershell to do this.

I have this thus far (taken from http://social.technet.microsoft.com/Forums/windowsserver/en-US/3edcabac-f1a8-4c4a-850c-8ba4697930a2/using-net-share-within-powershell)
but its not working, please help
(test environment)

#Get a list of the user folders and the path
$users=get-childitem C:\ain\userdata\ -name ; $SrvPath = "C:\ain\Userdata\"


#Create a scrptblock with the Net SHARE COMMAND
$sb = `
 {
 param($User,$SrvPath)
 NET SHARE $User=$SrvPath+$user "/GRANT:Domain\Domain Admins,FULL" "/GRANT:DOMAIN\+$User,CHANGE" /REMARK:"Home folder for $SrvPath"
 }
 
foreach-Object `
{
 Invoke-Command -ScriptBlock $sb -ArgumentList $user,$SrvPath
}

 Error Reads:

NET : System error 1332 has occurred.
At C:\Scripts\share-test.ps1:10 char:2
+  NET SHARE $User=$SrvPath+$user "/GRANT:Domain\Domain Admins,FULL" "/GRANT:DOMAI ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (System error 1332 has occurred.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
No mapping between account names and security IDs was done.
CAAInfrastructureAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan McFaddenSystems EngineerCommented:
I would recommend using XCOPY instead of robocopy.  It will make the procedure much easier.

References:

1. http://support.microsoft.com/kb/323007/en-us
2. http://support.microsoft.com/kb/240268/en-us
3. http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true

This would make the relocation a single step process.... as opposed to the robocopy and the powershell steps.

Dan
0
Justin YeungSenior Systems EngineerCommented:
questions

1. are users' folders created under the "C:\ain\Userdata\"
2. what type of permission needs to be on each folder? inherited? not inherited? user need modify, full or so?
any other network account needs to have access?
3. you want to share the folder Userdata or share each of the users folders?
4. if so how would you like the shared name? and what type of shared permission? everyone? full or ?
0
David Johnson, CD, MVPOwnerCommented:
robocopy with the /sec option already does the ntfs file permissions
now you simply need to create the share and give everyone full access (share level)
0
CAAInfrastructureAuthor Commented:
Thanks  for the  responses.
I obviously need to clarify a few things.
1. We are performing an initial restore of the data using DPM 2010 to c:\ain\Userdata
2. We are then preforming a robocopy to from the current production data folder to  c:\ain\Userdata to catch up on any data differences.
3. Once the data is up to date, I need to reshare the folders.

To answer Justin's questions:
1. Yes , the users folders are created under c:\ain\userdata
2. Permissions are to be
/GRANT:Domain Admins,FULL  /GRANT:caa\SSimpson ,CHANGE" /REMARK:"Home folder for SSimpson"
3. The folder userdata is a hidden share. The subfolders are shared but not hidden
4. For each of the userdata folders, the sharename is to be the same as the foldername (which incidentally is the same as the username.
I'm trying a slightly different approach: (But still getting an error. added to the bottom)

#Declare a user variable and populate it with the users from the target OU
$users.Clear()
$users=Get-ADUser -filter * -SearchBase "OU=AAA,OU=BBB,OU=BDD Domain Users,DC=CCC,DC=DDD,DC=FFF,DC=GG" | Select sAMAccountName | ft -hidetableheaders
$users | foreach-object -process $sb
$sb = {new-smbshare -name $users -path e:\userdata\$users -description "Homedrive share for" '$users' -FULLACCESS "CCC\domain admins" -changeaccess CCC\$users }

New-SmbShare : Cannot process argument transformation on parameter 'Name'. Cannot convert value to type System.String.
At line:2 char:27
+ $sb = {new-smbshare -name $users -path e:\userdata\$users -description 'Homedriv ...
+                           ~~~~~~
    + CategoryInfo          : InvalidData: (:) [New-SmbShare], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,New-SmbShare
0
Justin YeungSenior Systems EngineerCommented:
$sb = {new-smbshare -name $users -path e:\userdata\$users -description "Homedrive share for" '$users' -FULLACCESS "CCC\domain admins" -changeaccess CCC\$users }

The $users is not a string, it is all the users of "Get-ADUser -filter * -SearchBase "OU=AAA,OU=BBB,OU=BDD Domain "

you need to change that.....

$users | % {new-smbshare -name $_.samaccountname -path e:\userdata\$($_.Samaccountname) -description "Homedrive share for "$($_.Name) '' -FULLACCESS "CCC\domain admins" -changeaccess CCC\$($_.Name)}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.