Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1350
  • Last Modified:

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.
0
CAAInfrastructure
Asked:
CAAInfrastructure
1 Solution
 
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 YeungCommented:
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 YeungCommented:
$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

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Tackle projects and never again get stuck behind a technical roadblock.
Join Now