-
Notifications
You must be signed in to change notification settings - Fork 0
/
Invoke-SPFeatures.ps1
114 lines (105 loc) · 4.38 KB
/
Invoke-SPFeatures.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
# Requires:
# Log.ps1
function Invoke-SPFeatures{
param(
[System.Xml.XmlElement]$Config,
[string]$ProgressTitle
)
$featureActions = $Config.SelectNodes("./*")
[int]$featureCount = $featureActions.Count
[int]$featureIndex = 0
$scriptPath = Split-Path -Parent $script:MyInvocation.MyCommand.Path
if(-not(Get-PSSessionConfiguration | Where-Object {$_.Name -eq "PS2"})) {
Register-PSSessionConfiguration -Name PS2 -Confirm:$false -SecurityDescriptorSddl "O:NSG:BAD:P(A;;GA;;;BA)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)"
}
#Open a new session (process: wsmprovhost.exe)
$session = New-PSSession -ConfigurationName "PS2"
Invoke-Command -Session $session -ScriptBlock $snapin
Write-Output "Waiting 10sec for debugger..."
sleep 10
Write-Output "Waiting finished."
$featureActions | Where-Object {($_.LocalName -eq "activate") -or ($_.LocalName -eq "deactivate") } | ForEach-Object {
$featureIndex++
[bool]$featureActivate = $_.LocalName -eq "activate"
[string]$featureId = $_.Id
[string]$featureDesc = $_.Description
[string]$featureUrl = ""
if ($featureActivate) {
# Report progress
Write-Progress -Activity $ProgressTitle -Status "Activating $featureDesc" -PercentComplete ($featureIndex / $featureCount * 100)
try {
# Check if there is a URL
if (![string]::IsNullOrEmpty($_.Url)) {
$featureUrl = $_.Url
# WebApp / Site / Web scoped feature
Log "Information" "Activating $featureDesc ($featureId) on $featureUrl..."
$command = {
param($featureId, $featureUrl)
Enable-SPFeature -Id $featureId -Url $featureUrl -Confirm:$false -PassThru -Force > $null
}
Invoke-Command -Session $session -ScriptBlock $command -ArgumentList $featureId, $featureUrl -ErrorVariable featureErrors
}
else {
# Farm scoped feature
Log "Information" "Activating $featureDesc ($featureId) on farm..."
$command = {
param($featureId)
Enable-SPFeature -Id $featureId -Confirm:$false -PassThru -Force > $null
}
Invoke-Command -Session $session -ScriptBlock $command -ArgumentList $featureId -ErrorVariable featureErrors
}
} catch [Exception] {
Log-Error $_.Exception "An exception has been thrown while ACTIVATING $featureId. Please check the following logs for more details."
}
# if ($featureErrors -and $featureErrors.Count -gt 0) {
# $featureErrors | ForEach-Object {
# Log-Error $_ "An exception has been thrown while ACTIVATING $featureId. Please check the following logs for more details."
# }
# }
# else {
if ($featureErrors -eq $null -or $featureErrors.Count -eq 0) {
Log "Information" "Feature has been activated successfully."
}
}
else {
# Report progress
Write-Progress -Activity $ProgressTitle -Status "Deactivating $featureDesc" -PercentComplete ($featureIndex / $featureCount * 100)
try {
# Check if there is a URL
if (![string]::IsNullOrEmpty($_.Url)) {
$featureUrl = $_.Url
# WebApp / Site / Web scoped feature
Log "Information" "Deactivating $featureDesc ($featureId) on $featureUrl..."
$feature = Get-SPFeature $featureId -ErrorAction SilentlyContinue
if ($feature) {
$command = {
param($featureId, $featureUrl)
Disable-SPFeature -Id $featureId -Url $featureUrl -Confirm:$false
}
Invoke-Command -Session $session -ScriptBlock $command -ArgumentList $featureId, $featureUrl -ErrorVariable featureErrors
Log "Information" "Feature has been deactivated successfully."
}
else {
Log "Information" "Feature deactivation has been skipped since no feature has been found with the following ID: $featureId"
}
}
else {
# Farm scoped feature
Log "Information" "Deactivating $featureDesc ($featureId) on farm..."
$command = {
param($featureId)
Disable-SPFeature -Id $featureId -Confirm:$false
}
Invoke-Command -Session $session -ScriptBlock $command -ArgumentList $featureId -ErrorVariable featureErrors
Log "Information" "Feature has been deactivated successfully"
}
} catch [Exception] {
Log-Error $_.Exception "An exception has been thrown while DEACTIVATING $featureId. Please check the following logs for more details."
}
}
}
if ($session) { Remove-PSSession $session }
#Unregister-PSSessionConfiguration -Name "powershell2" -Confirm:$false
# Report progress (completed)
Write-Progress -Activity $ProgressTitle -Status "Completed" -Completed
}