Posted On October 27, 2020

SQL Error Msg 3201, Level 16, State 2

kimconnect 0 comments
blog.KimConnect.com >> Codes , Database >> SQL Error Msg 3201, Level 16, State 2

Sample Error Message:

Msg 3201, Level 16, State 2, Line 2
Cannot open backup device '\\1.1.1.1\d$\backup\TEST_MSCRM.bak'. Operating system error 5(Access is denied.).
Msg 3013, Level 16, State 1, Line 2
RESTORE DATABASE is terminating abnormally.

Resolution:

  1. Get the SQL Service run-as account
    $sqlServiceRunas=(Get-WMIObject win32_service |?{$_ -like "*MSSQLSERVER*"}).StartName
  2. Grant SQL Service account Full access to parent folder of backup directory
    $backupFile='\\1.1.1.1\d$\backup\TEST_MSCRM.bak'
    $parentDirectory=split-path $backupFile -parent
    $acl = Get-ACL $parentDirectory
    $allowFullAccesss=New-Object System.Security.AccessControl.FileSystemAccessRule($sqlServiceRunas,"Full","Allow")
    $acl.AddAccessRule($allowFullAccesss)
    Set-Acl $parentDirectory $acl
  3. Since the referenced path is an ‘Administrative Share’, the SQL account must be added to the local ‘Administrators’ group of the File Server
    $backupFile='\\1.1.1.1\d$\Backup\TEST_MSCRM.bak'
    [regex]$regexFileServer='^\\\\([\d\w\.\-]+)\\'
    $fileServer=.{$address=$regexFileServer.Match($backupFile).Groups[1].Value
    [System.Net.Dns]::GetHostByAddress($address).HostName
    }
    $session=new-pssession $fileServer
    if($session){
    invoke-command -session $session -scriptblock{
    param($principleName,$groupName)
    write-host "Adding $principleName into $groupName";
    Add-LocalGroupMember -Group $groupName -Member $principleName;
    write-host "$env:computername group $groupName now has these members:`r`n";
    get-localgroupmember $groupName|ft -autosize
    } -args $sqlServiceRunas,'Administrators'
    remove-pssession $session
    }else{
    write-warning "Unable to connect to $fileServer"
    }

Leave a Reply

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

Related Post

How to Install Secured Shell SSH on Windows

# Windows 10 & Server 2019 # Install the OpenSSH Server Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0…

PowerShell: Running Commands on Remote Computers

# runCommandsOnRemoteComputers.ps1 # User defined variables $computernames=@( 'SERVER001', 'SERVER002' ) $expectedExecutable='racadm.exe' $expectedInstallPath='C:\program files\Dell\SysMgt\iDRACTools\racadm' # Execution…

CentOS: Java & Tomcat Installation

Install Java 1.6.0_20:   The following instructions assume that there is no root access to…