Using Powershell to Create Windows SMB Shares
I have been knee deep in migrations, so after clicking through a few times to create clustered file shares, I finally decided to script them. Here is that script I thought I would share. Thanksfully there was the NTFSSecurity module published by microsoft that makes permissions a breeze. You can get more details by viewing the module page.
Pre-Requsites
Link to Script
Preparing to Execute the Script
Fill in the appropriate variables, and execute the script.
1Import-Module NTFSSecurity
2
3#variables
4$sharename = "share"
5$cachingmode = "none"
6$continuouslyavailable = $true
7$encryptdata = $false
8$folderenumerationmode = "AccessBased"
9$path = "D:\Shares\"
10$scopename = "ClusterORResourceName"
11$sharepath = $path+$sharename
12$fullaccess = "Everyone"
13
14
15#check if path exists
16$PathCheck = Test-Path $sharepath
17if ($PathCheck -eq $true) {
18 Write-Host "$sharepath exists."
19}
20else {
21 Write-Host "$sharepath does not exist"
22 New-Item $sharepath -type directory
23}
24
25
26#create share
27New-SmbShare -Name $sharename -CachingMode $cachingmode -ContinuouslyAvailable $continuouslyavailable -EncryptData $encryptdata -FolderEnumerationMode $folderenumerationmode -Path $sharepath -ScopeName $scopename -FullAccess $fullaccess
28
29#disable inheritance and add full access
30Disable-NTFSAccessInheritance $sharepath -RemoveInheritedAccessRules
31Add-NTFSAccess $sharepath -Account BUILTIN\Administrators -AccessRights FullControl
32Get-NTFSAccess $sharepath
Execute the Script
- Run .\Create-SMBShare.ps1