Solved

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

Posted on 2014-09-17
5
1,090 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article will help you understand what HashTables are and how to use them in PowerShell.
In this Micro Tutorial viewers will learn how to use Windows Server Backup to create full image of their system. Tutorial shows how to install Windows Server Backup Feature on Windows 2012R2 and how to configure scheduled Bare Metal Recovery backup.…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

746 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

17 Experts available now in Live!

Get 1:1 Help Now