How to create multiple shares using a single PowerShell command on Server 2008

        I have 300 users folders that I need to migrate from a 2003 server to a new 2008R2 server.  Server 08 grants read-only by default when creating a share and I need this to be "change" for each user.  Here is what I need:

path is E:\users\ (each subfolder in this directory is a username)

The share names need to match the folder name.  The file permissions are correct; I just need to set the share permissions to "change".  My migration takes place next Saturday night (IT never sleeps), so please HELP!  Thanks

April 25th, 2011 1:57pm

You may use Share module by Vadims Podans - http://en-us.sysadmins.lv/psscripts/ShareUtils.zip.
Free Windows Admin Tool Kit Click here and download it now
April 25th, 2011 2:25pm

why are you creating a share for each user? why not just share out e:\users to everyone (domain users read/write, maybe full control because the profiles might need it, not sure) and then rely on the currently configured NTFS permissions (which should be fine) to enforce the per user security.

April 25th, 2011 2:38pm

jrich, I appreciate the suggestion, but I am stuck with a "steaming pile of legacy".  I work in an evironment that believes, "that's the way we've always done it" is an answer for everything.  I also have to deal with a user base that is resistant to change.  I just have to make it happen. 

Free Windows Admin Tool Kit Click here and download it now
April 25th, 2011 2:43pm

lol, I know how that is... I left that job :)

Kazun's link will likely send you in the right directions. I know there are a lot of links out there for security on shares, although its not straightforward.

if that link doesnt get you off in the right direction let us know.

April 25th, 2011 2:47pm

I am looking at the scripts that Kazun suggested now.  They seem very complex.  The person who wrote those scripts knows so much about PowerShell that now I think I know even less than I did before!  This will take a while to understand, but I will respond as I locate my decoder ring!
Free Windows Admin Tool Kit Click here and download it now
April 25th, 2011 2:51pm

Kazun, have you ever successfully used the scripts you linked to?  I can see "ShareUtils" under "-ListAvailable", but can't see the individual modules or get any help.  Can you possibly walk me through this?
April 25th, 2011 4:26pm

 

Import-Module ShareUtil

Get all functions:

PS > Get-Command -Module ShareUtils

CommandType   Name                        Definition
-----------   ----                        ----------
Filter     Add-SharePermission                 ...
Function    Get-Share                      ...
Function    New-Share                      ...
Filter     Remove-Share                    ...
Filter     Remove-SharePermission               ...
Filter     Set-Share                      ...
Filter     Set-SharePermission                 ...
Function    Test-ElevatedShell                 ...


PS > get-help Set-SharePermission -Examples


Free Windows Admin Tool Kit Click here and download it now
April 25th, 2011 4:31pm

I did the Import-Module ShareUtils and it looked successful, but running Get-Command -Module ShareUtils returns no results.  Trying to run Get-Help for any of the functions says there is no help.

April 25th, 2011 5:17pm

Show output:

Get-Module shareutils | Import-Module -Verbose

April 25th, 2011 5:34pm

Get-Module shareutils | Import-Module -Verbose

This returns nothing!

Free Windows Admin Tool Kit Click here and download it now
April 25th, 2011 5:37pm

Get-Module shareutils | Import-Module -Verbose

This returns no

April 25th, 2011 5:39pm

Here is my output.  Looks like a certificate issue.  I replaced my username with %username%

PS C:\Users\%username%> get-module shareutils -ListAvailable | import-module -verbose


VERBOSE: Loading module from path 'C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.psd1'.
VERBOSE: Loading 'TypesToProcess' from path
'C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml'.
VERBOSE: Loading 'FormatsToProcess' from path
'C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Format.ps1xml'.
Import-Module : The following error occurred while loading the extended type data file:
Microsoft.PowerShell, C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml : File s
kipped because it was already present from "Microsoft.PowerShell".
Microsoft.PowerShell, C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml : File s
kipped because it was already present from "Microsoft.PowerShell".
Microsoft.PowerShell, C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml : File s
kipped because it was already present from "Microsoft.PowerShell".
Microsoft.PowerShell, C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml : File s
kipped because it was already present from "Microsoft.PowerShell".
Microsoft.PowerShell, C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml : File s
kipped because it was already present from "Microsoft.PowerShell".
Microsoft.PowerShell, C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml : File s
kipped because of the following validation exception: File C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareU
tils\ShareUtils.Types.ps1xml cannot be loaded. A certificate chain processed, but terminated in a root certificate whic
h is not trusted by the trust provider..
At line:1 char:53
+ get-module shareutils -ListAvailable | import-module <<<<  -verbose
    + CategoryInfo          : InvalidOperation: (:) [Import-Module], RuntimeException
    + FullyQualifiedErrorId : FormatXmlUpateException,Microsoft.PowerShell.Commands.ImportModuleCommand

Free Windows Admin Tool Kit Click here and download it now
April 25th, 2011 5:53pm

Change your Execution Policy to RemoteSigned.
April 25th, 2011 5:55pm

That took care of cerificate error (see below).  But running Get-Command -Module ShareUtils still returns no content!

PS C:\Users\%username%> get-module shareutils -ListAvailable | import-module -verbose
VERBOSE: Loading module from path 'C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.psd1'.
VERBOSE: Loading 'TypesToProcess' from path
'C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml'.
VERBOSE: Loading 'FormatsToProcess' from path
'C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Format.ps1xml'.
Import-Module : The following error occurred while loading the extended type data file:
Microsoft.PowerShell, C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml : File s
kipped because it was already present from "Microsoft.PowerShell".
Microsoft.PowerShell, C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml : File s
kipped because it was already present from "Microsoft.PowerShell".
Microsoft.PowerShell, C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml : File s
kipped because it was already present from "Microsoft.PowerShell".
Microsoft.PowerShell, C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml : File s
kipped because it was already present from "Microsoft.PowerShell".
Microsoft.PowerShell, C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml : File s
kipped because it was already present from "Microsoft.PowerShell".
Microsoft.PowerShell, C:\Users\%username%\Documents\WindowsPowerShell\Modules\ShareUtils\ShareUtils.Types.ps1xml : File s
kipped because it was already present from "Microsoft.PowerShell".
At line:1 char:53
+ get-module shareutils -ListAvailable | import-module <<<<  -verbose
    + CategoryInfo          : InvalidOperation: (:) [Import-Module], RuntimeException
    + FullyQualifiedErrorId : FormatXmlUpateException,Microsoft.PowerShell.Commands.ImportModuleCommand

Free Windows Admin Tool Kit Click here and download it now
April 25th, 2011 5:59pm

Restart powershell and show Get-ExecutionPolicy.
April 25th, 2011 6:03pm

Returns "RemoteSigned".

 

By the way, that you very much for all your time on this!

Free Windows Admin Tool Kit Click here and download it now
April 25th, 2011 6:07pm

I did the import again after renaming the old directory and I can now see the new modules!  The problem was that I set the execution policy AFTER running the import.  This is an important first step. 

1) Extract the zip file to C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ or C:\Users\%username%\Documents\WindowsPowerShell\Modules\

2) Set-ExecutionPolicy RemoteSigned

3) Import-Module ShareUtils

PS C:\Users\%username%> get-command -module shareutils

CommandType     Name                                                Definition
-----------              ----                                                ----------
Filter             Add-SharePermission                                 ...
Function        Get-Share                                                 ...
Function        New-Share                                                 ...
Filter             Remove-Share                                          ...
Filter             Remove-SharePermission                           ...
Filter             Set-Share                                                 ...
Filter             Set-SharePermission                                  ...
Function        Test-ElevatedShell                                     ...

OK.  Now I have this module installed correctly.  How do make a script to share 300 users folders at once?!!

April 25th, 2011 7:33pm

Why you don't want to read the documentation?
dir C:\folders | foreach {
		New-Share -Name $_.Name -Path $_.FullName | Set-Share
		Get-Share -Name $_.Name | Set-SharePermission "$($_.Name)" Allow Change | Set-Share
		}
Free Windows Admin Tool Kit Click here and download it now
April 25th, 2011 7:41pm

I've just now had time to read through some of the help files.  I looked for the syntax you have listed (specifically the variables) and I don't see them.  Creating a simple share seems straight-forward, but when you have to incorporate a complicated variable like making the sharename equal to each folder name, it gets a lot tougher.  Since my time was limited for this project, I was hoping to find a finished script that does what I needed and then just change the path to my own.  I wasn't prepared to try and learn PowerShell in the next few days!  I'm not trying to be lazy here, just short on time.  Making these shares is only one small part of my project.  I have to migrate a file server that hosts 2.5 million files and serves over 2000 users...oh, and not break anything!  Any help I can get is appreciated.
April 26th, 2011 2:35am

Hi,

 

 

Get-Acl and Set-Acl

 

$Acl = Get-Acl "C:\Folder_B"

Set-Acl "C:\Folder_A" $Acl

 

 

Changing the ACL

 

New-Item -type directory -path C:\MyFolder

$Acl = Get-Acl "C:\MyFolder"

$Ar = New-Object  system.security.accesscontrol.filesystemaccessrule("username","FullControl","Allow")

$Acl.SetAccessRule($Ar)

Set-Acl "C:\MyFolder" $Acl

 

For more information:

 

PowerShell - Editing permissions on a file or folder

http://blogs.msdn.com/b/johan/archive/2008/10/01/powershell-editing-permissions-on-a-file-or-folder.aspx

 

 

Furthermore, a regular shell (not power shell), type xcacls /? and see if the options available work for you.

 

How to use Xcacls.exe to modify NTFS permissions

http://support.microsoft.com/kb/318754

 

 

Thanks.

Free Windows Admin Tool Kit Click here and download it now
April 26th, 2011 3:11am

I did something similar recently - did it before finding this page - used this code:

$shares=[WMICLASS]WIN32_Share
Get-ChildItem -path 'c:\test1' | ForEach-Object -Process {
$Shares.Create("c:\test1\$_",$_,0) }

July 14th, 2015 3:19am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics