bakersfield
bakersfield

Reputation: 13

Create New SMB Share for every folder in a directory using powershell

I want to create a new SMB Share for every folder in a directory under windows, have the smb share name be the same as the folder name and set the account "Everyone" with "Full Acess" for the share permission (so not the NTFS permissions)

So for example I have the following folders

And the share names should then be named adequately, so Folder1, Folder2, Folder3

I know how to create a single smb share and set a local user with full access with the following:

New-SmbShare -name "Test" -path "D:\Test" -FullAccess "TestServer\TestAccount"

Where I currently fail is to somehow get all the folder names and create a share accordingly. Also, I don't know how to tell PowerShell the account "Everyone".

EDIT:

When I try it as you mentioned, I get the following error

New-SmbShare: The trust relationship between this workstation and the primary domain failed.

At line:2 char:1
+ New-SmbShare -Name $_.Name -Path $._FullName -FullAccess Everyone
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (MSFT_SMBShare:ROOT/Microsoft/Windows/SMB/MSFT_SMBShare) [New-SmbShare], CimException
+ FullyQualifiedErrorId : Windows System Error 1789,New-SmbShare

Since I can set the permission for this group manually, I dont know why I here would need access to the domain. I know that the Well-Known SID "World" or "Everyone" has the String Value S-1-1-0, maybe you have to replace "-FullAccess Everyone" with "FullAccess S-1-1-0"? But that didn't work for me..

Source: https://learn.microsoft.com/en-us/windows/win32/secauthz/well-known-sids

EDIT2
OS is in german, so I have to change "Everyone" to the german counterpart (="Jeder")

Upvotes: 1

Views: 5981

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174815

Use Get-ChildItem -Directory to enumerate all the folders:

Get-ChildItem path\to\root\directory -Directory |ForEach-Object {
  New-SmbShare -Name $_.Name -Path $_.FullName -FullAccess Everyone
}

To generate the correct translation of Everyone regardless of OS language, use its well-known SID:

$everyoneSID = [System.Security.Principal.SecurityIdentifier]::new('S-1-1-0')
$everyoneName = $everyoneSID.Translate([System.Security.Principal.NTAccount]).Value

Get-ChildItem path\to\root\directory -Directory |ForEach-Object {
  New-SmbShare -Name $_.Name -Path $_.FullName -FullAccess $everyoneName
}

Upvotes: 2

Related Questions