Windows Server 跨伺服器匯出與匯入共用資料夾設定 (Shared Fodlers)

2023-10-18

筆記如何使用 PowerShell 跨伺服器匯出與匯入共用資料夾設定,在搬遷伺服器的好幫手。

logo

說明

執行以下 PowerShell Script 將共用資料夾的設定匯出。

Get-SmbShare | Export-Csv -Path C:\SharedFolders.csv -NoTypeInformation

接著 VSCode 過濾無效的資訊後,將 SharedFolders.csv 匯入到新的 Windows Server,完成後需要進行重新啟動 😀

Import-Csv -Path C:\SharedFolders.csv | ForEach-Object {New-SmbShare -Name $_.Name -Path $_.Path -Description $_.Description}

大功告成囉

Net share & icacls

使用指令 net share 的方式確認共用資料夾設定,使用 icacls 確認資料夾的安全性。

REM Show all share folder
net share

REM Show share folder permission
net share sharename

REM Show share folder NTFS permission
icacls folderpath

對應在 PowerShell 可以使用 Get-SmbShare 來確認共用資料夾以及使用 Get-Acl 來確認資料夾安全性。

# Show all share folder
Get-SmbShare

# Show share folder permission
Get-SmbShareAccess sharename | ogv

# Show share folder NTFS permission
Get-Acl folderpath | ogv
# Define folder path and share name
$shareName = "A"
$folderPath = (Get-SmbShare -Name $shareName).Path

# Get ACL permissions for the folder
$aclPermissions = (Get-Acl -Path $folderPath).Access | ForEach-Object { $_.FileSystemRights }

# Get SMB share permissions
$sharePermissions = (Get-SmbShareAccess -Name $shareName).AccessRight

# Calculate minimum permissions by combining both sets of permissions
$minimumPermissions = $aclPermissions + $sharePermissions | Sort-Object -Unique

# Output the minimum permissions
Write-Host "Minimum permissions for the folder:"
$minimumPermissions