-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshareDump.ps1
126 lines (92 loc) · 4.18 KB
/
shareDump.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<#
.SYNOPSIS
Dump/Restore share permissions to $sharesXmlFile file.
.DESCRIPTION
The function for share permissions migration.
.PARAMETER $srvName
Specifies the server name for export share permissions to $sharesXmlFile.
.PARAMETER $sharesXmlFile
Specifies the file name for export share permissions, xml format.
.INPUTS
None. You cannot pipe objects for function.
.OUTPUTS
Dump-Shares function is generate $sharesXmlFile.
.EXAMPLE
C:\PS> Dump-Shares -srvName RemoteServerName -sharesXmlFile "c:\shareXmlInfo.xml"
.EXAMPLE
C:\PS> Restore-Shares -srvName RemoteServerName -sharesXmlFile "c:\shareXmlInfo.xml"
.LINK
https://t.me/id58162774
#>
function Dump-Shares{
Param (
[parameter(Mandatory = $true)]
[string]$srvName,
[parameter(Mandatory = $true)]
[string]$sharesXmlFile
)
Process{
$cim = New-CimSession -ComputerName $srvName
$shares = Get-SmbShare -CimSession $cim
Remove-Item -Path $sharesXmlFile -EA SilentlyContinue
$GeneralStatsXML = "<?xml version=""1.0"" encoding=""utf-8""?>`n"
$GeneralStatsXML += "<Result>`n"
if ($shares){
foreach($share in $shares){
$shareName = $share.Name
$sharePath = $share.Path
$GeneralStatsXML += "`t<OperationResult>`n"
$GeneralStatsXML += "`t`t<ShareName>$shareName</ShareName>`n"
$GeneralStatsXML += "`t`t`t<Path>$sharePath</Path>`n"
$sharePerms = Get-SmbShareAccess -CimSession $cim -Name $shareName
if ($sharePerms){
foreach($sharePerm in $sharePerms){
$shareUser = $sharePerm.AccountName
$shareRight = $sharePerm.AccessRight
$GeneralStatsXML += "`t`t`t`t<User>`n"
$GeneralStatsXML += "`t`t`t`t`t<Name>$shareUser</Name>`n"
$GeneralStatsXML += "`t`t`t`t`t<Right>$shareRight</Right>`n"
$GeneralStatsXML += "`t`t`t`t</User>`n"
}
}
$GeneralStatsXML += "`t</OperationResult>`n"
}
$GeneralStatsXML += "</Result>`n"
}
Add-Content -Encoding UTF8 -Value $GeneralStatsXML -Path $sharesXmlFile
Write-Host "Save XML file to $sharesXmlFile"
}
}
function Restore-Shares{
Param (
[parameter(Mandatory = $true)]
[string]$srvName,
[parameter(Mandatory = $true)]
[string]$sharesXmlFile
)
Process{
$cim = New-CimSession -ComputerName $srvName
[xml]$shareSettings = Get-Content $sharesXmlFile
foreach( $setShare in $shareSettings.Result.OperationResult){
$shareName = $setShare.ShareName
$sharePath = $setShare.Path
$shareIsPreset = Get-SmbShare -Name $shareName -ErrorAction SilentlyContinue -CimSession $cim
foreach( $user in $setShare.User){
$userName = $user.Name
$userRight = $user.Right
if($shareIsPreset){
Grant-SmbShareAccess -Name $shareName -AccountName $userName -AccessRight $userRight -Confirm:$false -CimSession $cim
}else{
if($userRight -eq "Full"){
New-SmbShare -Name $shareName -Path $sharePath -FullAccess $userName -Confirm:$false -CimSession $cim
}elseif($userRight -eq "Change") {
New-SmbShare -Name $shareName -Path $sharePath -ChangeAccess $userName -Confirm:$false -CimSession $cim
}else{
New-SmbShare -Name $shareName -Path $sharePath -ReadAccess $userName -Confirm:$false -CimSession $cim
}
$shareIsPreset = $true
}
}
}
}
}