Migrate shared folder configuration to Windows Server 2012
Migrate shared folder configuration to Windows Server 2012

You need to migrate a file server shared folder from a Windows Server 2008 to a Windows Server 2012. Windows Server 2012 now contains Powershell cmdlets to interact with SMB Shared folders, but Windows 2008 not. The purpose of the following script is to generate the new-smbshare commands to create shared folder based on the current Windows 2008 file server configuration.

The script performs the following steps :

  • list shared folders located in the Windows 2008 server
  • get the shared folder permissions
  • create the command using the new-smbshare cmdlet

For information, the script bypass the default administrative shared folders (from A$ to Z$).

When it is done, just copy paste the script output to your new file server running on Windows 2012 and… that’s all 🙂

Source

Function Get-SharePerm($Share,$comp) {
	$tmp = @()
	$share = $share -replace "\\","\\"
	$wmi_shared = gwmi -Class Win32_LogicalShareSecuritySetting -Filter "name='$Share'" -ComputerName $comp 
	$wmi_shared.GetSecurityDescriptor().Descriptor.DACL | % {
		$UserName = $_.Trustee.Name 
		If ($_.Trustee.Domain -ne $Null) {$UserName = "$($_.Trustee.Domain)\$UserName"}    
        If ($_.Trustee.Name -eq $Null) {$UserName = $_.Trustee.SIDString }     
		[Array]$tmp += New-Object Security.AccessControl.FileSystemAccessRule($UserName, $_.AccessMask, $_.AceType)
	}
	return $tmp
}

$computer = "your_Windows_2008_Server_Hostname"
 
if ($shares = Get-WmiObject Win32_Share -ComputerName $computer | Where {$_.Path -and $_.ServerName -and ($_.Caption -notmatch "^[A-Z]\$")}){
	$shares | Foreach { 
		Write-Progress -Status "Get share information on $($_.__Server)" $_.Name
		$result = Get-SharePerm $_.Name $_.__Server
		$scope = ($_.Name -split "\\")[2]
		$name = '"'+($_.Name -split "\\")[3]+'"'
		$pathname = '"'+$_.Path+'"'
		Write-Host -NoNewline "new-smbshare -Name"$name" -ScopeName "$scope" -Path "$pathname
			if ($result | ? {$_.FileSystemRights  -match "Full"}) { 
				Write-Host -NoNewline " -FullAccess" ((($result | ? {$_.FileSystemRights  -match "Full"}) | `
				select -ExpandProperty IdentityReference | % { '"' + $_ + '"'} ) -join ", ")
			}
			if ($result | ? {$_.FileSystemRights  -match "Modify"}) { 
				Write-Host -NoNewline " -ChangeAccess" ((($result | ? {$_.FileSystemRights  -match "Modify"}) | `
				select -ExpandProperty IdentityReference | % { '"' + $_ + '"'} ) -join ", ")
			}
			if ($result | ? {$_.FileSystemRights  -match "Read"}) { 
				Write-Host -NoNewline " -ReadAccess" ((($result | ? {$_.FileSystemRights  -match "Read"}) | `
				select -ExpandProperty IdentityReference | % { '"' + $_ + '"'} ) -join ", ")
			}
			write-host " "
	}
}
else {"Failed to get share information from {0}." -f $($_.ToUpper())}

<>

My Powershell script categories

Migrate shared folder configuration to Windows Server 2012

Leave a Reply

Your email address will not be published.