-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSSA-MP-Export-Import.ps1
More file actions
165 lines (136 loc) · 7.96 KB
/
SSA-MP-Export-Import.ps1
File metadata and controls
165 lines (136 loc) · 7.96 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# from https://gallery.technet.microsoft.com/office/Powershell-script-to-09ffa974
#-----------------------------------------------------------------------------
# Name: export-import-mp.ps1
# Description: This script has three switch to:
# - export a list of managed properties (optionally filtered)
# - import a list of managed properties with crawled property mapping
# - delete a list of managed properties
#
# Usage: Run the script passing paramters ServiceApp, Import|Export|Delete and Filter
# By: Riccardo Celesti blog.riccardocelesti.it
#-----------------------------------------------------------------------------
Param([Parameter(Mandatory=$true)]
[String]$serviceapp,
[Parameter(Mandatory=$false,ParameterSetName='Export')]
[Parameter(Mandatory=$true,ParameterSetName ="Delete")]
[String]$filter,
[Parameter(ParameterSetName='Import')]
[switch]$import,
[Parameter(ParameterSetName='Export')]
[switch]$export,
[Parameter(ParameterSetName='Delete')]
[switch]$delete
)
if ((gsnp MIcrosoft.SharePoint.Powershell -ea SilentlyContinue) -eq $null){
asnp MIcrosoft.SharePoint.Powershell -ea Stop
}
$logfile = ".\export-managed-properties.csv"
$importlog = ".\import-managed-properties.log"
$importlogerr = ".\import-managed-properties-errors.log"
function ManagedPropertyTypes($type){
switch ($type){
"text" {$type = 1}
"integer" {$type = 2}
"decimal" {$type = 3}
"DateTime" {$type = 4}
"YesNo" {$type = 5}
"Binary" {$type = 6}
"Double" {$type = 7}
default {$type = 1}
}
return $type
}
if ((Get-SPEnterpriseSearchServiceApplication $serviceapp -ea SilentlyContinue) -eq $null){
Write-Host "Enterprise Search Service Application $serviceapp has not been found" -ForegroundColor Red
exit
} else {
$ssa = Get-SPEnterpriseSearchServiceApplication $serviceapp
Write-Host "Enterprise Search Service Application $serviceapp has been found" -ForegroundColor Green
}
if ($export){
if ((Get-ChildItem -Name $logfile -ea SilentlyContinue) -ne $null){
Clear-Content $logfile
ac $logfile "Name,Description,ManagedType,Searchable,FullTextQueriable,Queryable,Retrievable,Refinable,Sortable,HasMultipleValues,SafeForAnonymous,Mapping";
} else {
ac $logfile "Name,Description,ManagedType,Searchable,FullTextQueriable,Queryable,Retrievable,Refinable,Sortable,HasMultipleValues,SafeForAnonymous,Mapping";
}
if ($filter -ne $null){
Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Limit All | ?{$_.Name -like "$($filter)*"} | %{
$mp = $_;
$mpmap = @();
Get-SPEnterpriseSearchMetadataMapping -SearchApplication $ssa -ManagedProperty $mp.Name | %{
$mpmap += $_.CrawledPropertyName
}
ac $logfile "$($mp.Name),$($mp.Description),$(ManagedPropertyTypes $mp.ManagedType),$($mp.Searchable),$($mp.FullTextQueriable),$($mp.Queryable),$($mp.Retrievable),$($mp.Refinable),$($mp.Sortable),$($mp.HasMultipleValues),$($mp.SafeForAnonymous),$($mpmap -join '|')"
}
} else {
Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Limit All | %{
$mp = $_;
$mpmap = @();
Get-SPEnterpriseSearchMetadataMapping -SearchApplication $ssa -ManagedProperty $mp.Name | %{
$mpmap += $_.CrawledPropertyName
}
ac $logfile "$($mp.Name),$($mp.Description),$(ManagedPropertyTypes $mp.ManagedType),$($mp.Searchable),$($mp.FullTextQueriable),$($mp.Queryable),$($mp.Retrievable),$($mp.Refinable),$($mp.Sortable),$($mp.HasMultipleValues),$($mp.SafeForAnonymous),$($mpmap -join '|')"
}
}
}
if ($import){
if ((Get-ChildItem $logfile -ea SilentlyContinue) -eq $null){
Write-Host "The export file has not been found" -ForegroundColor Red
exit
} else {
Import-Csv $logfile -Delimiter "," | %{
$mp = $_;
if ((Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Identity $mp.Name -ea SilentlyContinue) -eq $null){
try {
$newmp = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Name $mp.Name -Description $mp.Description -Type $mp.ManagedType -FullTextQueriable:([bool]::Parse($mp.FullTextQueriable)) -Retrievable:([bool]::Parse($mp.Retrievable)) -Queryable:([bool]::Parse($mp.Queryable)) -SafeForAnonymous:([bool]::Parse($mp.SafeForAnonymous)) -EA Stop
ac $importlog "$(Get-Date),$($mp.Name),[INF],Managed Property,"
#$ump = Get-SPEnterpriseSearchMetadataManagedProperty $mp.Name -SearchApplication $ssa
$newmp.Searchable = ([bool]::Parse($mp.Searchable))
$newmp.Refinable = ([bool]::Parse($mp.Refinable))
$newmp.Sortable = ([bool]::Parse($mp.Sortable))
$newmp.HasMultipleValues = ([bool]::Parse($mp.HasMultipleValues))
$newmp.Update()
if (($mp.Mapping).Length -gt 0){
$cps = $mp.Mapping
foreach ($term in $cps.split("|")){
Write-Host "Processing Crawled Property $($term) on Managed Property $($mp.Name)" -ForegroundColor Cyan
if ((Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $ssa -Name $term -EA SilentlyContinue) -ne $null){
$cp = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $ssa -Name $term
try {
New-SPEnterpriseSearchMetadataMapping -SearchApplication $ssa -ManagedProperty $newmp -CrawledProperty $cp[0] -EA Stop
ac $importlog "$(Get-Date),$($mp.Name),[INF],Metadata Mapping,$term"
if($cp -is [system.array]){
ac $importlog "$(Get-Date),$($mp.Name),[WRN],Metadata Mapping,$term,More than one crawled property selected only the first has been used"
}
} catch {
Write-Host "Something went wrong :) $($Error[0].Exception.Message)" -ForegroundColor Red
ac $importlog "$(Get-Date),$($mp.Name),[ERR],Metadata Mapping,$term"
ac $importlogerr "$(Get-Date),$($mp.Name),[ERR],Metadata Mapping,$($Error[0].Exception.Message)"
}
} else {
Write-Host "Crawled property $($term) does not exists" -ForegroundColor Red
ac $importlog "$(Get-Date),$($mp.Name),[ERR],Crawled Property,$term"
ac $importlogerr "$(Get-Date),$($mp.Name),[ERR],Crawled Property,$term does not exists"
}
}
}
} catch {
Write-Host "Something went wrong :) $($Error[0].Exception.Message)" -ForegroundColor Red
ac $importlog "$(Get-Date),$($mp.Name),[ERR],"
ac $importlogerr "$(Get-Date),$($mp.Name),[ERR],Managed Property,$($Error[0].Exception.Message)"
}
} else {
Write-Host "Managed property $($mp.Name) already exists" -ForegroundColor Red
ac $importlog "$(Get-Date),$($mp.Name),[WRN],Managed Property,Managed property already exists"
}
}
}
}
if ($delete){
Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Limit All | ?{$_.Name -like "$($filter)*"} | %{
$mp = $_;
$mp.DeleteAllMappings();
$mp.Delete();
}
}