Solved

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

Posted on 2014-09-17
5
1,106 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
5 Comments
 
LVL 26

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 78

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now