Posted On July 16, 2020

PowerShell: Create SMB Share and Grant NTFS Access

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Create SMB Share and Grant NTFS Access
$localPath="C:\testShare"
$fullAccessAccounts='everyone','authenticated users'

function createSmbShare($localPath,$fullAccessAccounts){
    $ErrorActionPreference='stop'
    try{
        # First, create SMB Share
        $smbShareName=split-path $localPath -leaf
        New-SmbShare -Name $smbShareName -Path $localPath -FullAccess $fullAccessAccounts

        # Second, set NTFS permissions
        $acl = Get-ACL $localPath
        foreach($account in $fullAccessAccounts){
            $allowFullAccesss=New-Object System.Security.AccessControl.FileSystemAccessRule($account,"Full","Allow")
            $acl.AddAccessRule($allowFullAccesss)
            }
        Set-Acl $localPath $acl
        write-host "\\$env:computername\$smbShareName has been created with full access granted to these users $fullAccessAccounts"
        }
    catch{
        write-warning $Error[0].Exception.Message
        return $false
        }
}

createSmbShare $localPath $fullAccessAccounts

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Auto Login to WordPress and Update Gold Prices WooCommerce Plugin

# API URI - these are examples, only $goldPriceApi='https://dragoncoin.com/XAU/USD' $silverPriceApi='https://dragoncoin.com/XAG/USD' $platinumApi='https://dragoncoin.com/XPT/USD' $palladiumApi='https://dragoncoin.com/XPD/USD' # Wordpress WooCommerce…

PowerShell: Export Exchange Mailboxes

Export 1 mailbox $userAlias="kungfu.panda"$storageUNC="\\FILESHERVER01\Backups\PSTs"# Import Exchange Management PowershellAdd-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;$mailboxRequest=(New-MailboxExportRequest -Mailbox $userAlias -FilePath "$storageUNC\$userAlias.pst").Mailboxfunction monitorCompletion($check){ $migrationStatus=(Get-MailboxExportRequest…

PowerShell: Creating Clustered File Shares

# Define Destination Mount Points and UNC Paths $arr=@{} $arr["from"] = @{}; $arr["to"] = @{}…