Batch modification of SQL Server database disk file permissions

Company security policy adjustments have been made. Due to irregular activation methods, it seems fine without activating for personal use.

Activation through unconventional means triggered Microsoft’s detection. The server ran for half a month before automatically shutting down after one hour of startup. Examining the system logs revealed it was due to piracy.

There’s no way around it; another system reinstall is needed, INLINE_CODE_0 faces a reinstall too. Each time is quite troublesome due to strict file permission controls, preventing normal database appending.

Error message

After system reinstallation, 5120 errors and operating system access denials may occur in the supplemental database

Process script

The previous link: 批量更新本地Git仓库. It’s this familiar script again, modified to traverse folders while also changing file permissions – currently granting full editing access.

Most online tutorials require manual modification – do they only need to change a few files each time? I have to handle batches of files, all manually. It’s enough to make you feel depressed.

$currentUserName = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$rootDirectory = "D:\data\2013_RujiaInfo"

Get-ChildItem -Path $rootDirectory -Recurse | ForEach-Object {
    $itemPath = $_.FullName

    if ($_ -is [System.IO.DirectoryInfo]) {
        $icaclsResult = icacls $itemPath /setowner "$currentUserName" 2>&1
        if ($LASTEXITCODE -eq 0) {
            Write-Host "已更改文件夹 $itemPath 的所有者为 $currentUserName"
            # 授予当前用户写入权限
            Invoke-Expression "icacls `"$itemPath`" /grant `"$($currentUserName):(OI)(CI)F`""
            Write-Host "已授予 $currentUserName 编辑文件夹的权限"
        } else {
            Write-Host "无法更改文件夹 $itemPath 的所有者。错误信息: $icaclsResult"
        }
    } else {
        $takeownResult = icacls $itemPath /setowner "$currentUserName" 2>&1
        if ($LASTEXITCODE -eq 0) {
            # 授予当前用户写入权限
            Invoke-Expression "icacls `"$itemPath`" /grant `"$($currentUserName):(F)`""
            Write-Host "已授予 $currentUserName 编辑文件的权限"
        } else {
            Write-Host "无法更改文件 $itemPath 的所有者。错误信息: $takeownResult"
        }
    }
}

Licensed under CC BY-NC-SA 4.0
Last updated on May 28, 2025 09:47
A financial IT programmer's tinkering and daily life musings
Built with Hugo
Theme Stack designed by Jimmy