Posted On September 8, 2022

PowerShell: Grant SMB Access

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Grant SMB Access
# grantSmbAccess.ps1

$computername='fileserver'
$shareName='TEST'
$account='domain\groupOrUsername'
$accessType='read' # also change or full

invoke-command -computername $computername -scriptblock{
    param($shareName,$account,$accessType)
    try{
        Grant-SmbShareAccess -Name $shareName -AccountName $account -AccessRight $accessType -force
        # Get-SmbShareAccess $shareName
        $object=(Get-SmbShare $shareName).Path
        # Translating SMB to NTFS equivalent: Full, Modify, ReadAndExecute
        $permission=switch($accessType.tolower()){
            'full' {'full'}
            'change' {'modify'}
            'read' {'ReadAndExecute'}
            Default {'ReadAndExecute'}
        }
        $acl=Get-Acl $object
        $grantAccess=New-Object System.Security.AccessControl.FileSystemAccessRule($account,$permission,'Allow')
        $acl.AddAccessRule($grantAccess)
        Set-Acl $object $acl
        return $true
    }catch{
        write-warning $_
        return $false
    }

} -Args $shareName,$account,$accessType

Leave a Reply

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

Related Post

Some Batch Files

REM Date.batset fscfg=m:\mfgsys\fs.cfgm:\fs_apps\setcfg N0 DT_TODAY={TODAY} REM lexp.batfsguiapp.exe -s -cm:\mfgsys\fs.cfg -iAAA{TAB}y2k99{CR}bexe{ESC}exp{CR}exit Rem Invoice_Prints_ZZ1_2_3_.batfsguiapp.exe -cm:\mfgsys\fs.cfg -iZZ1{TAB}pass1{CR}bexe{ESC}zz1{CR}M:\cabsauto\timeout 15fsguiapp.exe…

JavaScript: Build a Tribute Page

Demo: https:// codepen.io/dragoncoin/pen/EZJxYY HTML Code: <html><head> <body> <img src="http://www.animationxpress.com/wp-content/uploads/2015/10/Mahatma-Gandhi.jpg"> <h1>Mahatma Gandhi</h1> <div class="hidden"> <h2>"You must…

PowerShell: Purge OneDrive

# purgeOneDrive_v0.0.1.ps1function purgeOneDrive{ write-host "Halting OneDrive and explorer processes..." taskkill.exe /F /IM "OneDrive.exe" taskkill.exe /F…