Solved

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

Posted on 2014-09-17
5
1,261 Views
Last Modified: 2014-10-20
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
Comment
Question by:CAAInfrastructure
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 28

Expert Comment

by:Dan McFadden
ID: 40329811
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
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 40334643
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
 
LVL 82

Expert Comment

by:David Johnson, CD, MVP
ID: 40335686
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
 

Author Comment

by:CAAInfrastructure
ID: 40338039
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
 
LVL 14

Accepted Solution

by:
Justin Yeung earned 500 total points
ID: 40344128
$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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question