lunedì 7 settembre 2015

Simpliest Script to share folders

Hi everybody,
for me this is the simpliest script to share folders with poweshell 4, works well in w2012:

#Username
$User = ""Write-Host $User
$DomainUser = "pacorini\"+$User


#User home folder base path
$BasePath = 'E:\HomeFolders\'




#Select the name of the share$ShareName=$User
#Combine sharebase folder and user to construct the full path
$ShareFolder = $BasePath+$User



#Create new share - this work only with windows 2012

New-SmbShare -Name $Sharename -Path $ShareFolder -FullAccess $User


#Alternatively you could use this (on previous version):
#$Shares=[WMICLASS]'WIN32_Share'
#$Shares.Create($ShareFolder,$ShareName,0)



#Set NTFS Permission

$Acl = Get-Acl $ShareFolder

$Ar = New-Object system.security.accesscontrol.filesystemaccessrule($DomainUser,'FullControl','ContainerInherit, ObjectInherit', 'None', 'Allow')

$Acl.AddAccessRule($Ar)

Set-Acl $ShareFolder $Acl



#Set Smb share permission.
Revoke-SmbShareAccess -Name $ShareName -AccountName "Everyone" -Force
Grant-SmbShareAccess -Name $ShareName -AccountName $DomainUser -AccessRight Full -Force




If you like this post, please leave a comment.

Nessun commento:

Posta un commento