-
Notifications
You must be signed in to change notification settings - Fork 30
/
Microsoft.PowerShell_profile.install.ps1
322 lines (224 loc) · 9.11 KB
/
Microsoft.PowerShell_profile.install.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
param(
[switch]
$Force
)
<#
$Metadata = @{
Title = "Profile Installation"
Filename = "Microsoft.PowerShell_profile.install.ps1"
Description = ""
Tags = "powershell, profile, installation"
Project = ""
Author = "Janik von Rotz"
AuthorContact = "http://janikvonrotz.ch"
CreateDate = "2013-03-18"
LastEditDate = "2014-02-05"
Version = "7.1.1"
License = @'
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ or
send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
'@
}
#>
#--------------------------------------------------#
# project init
#--------------------------------------------------#
# check compatiblity
if($Host.Version.Major -lt 2){
throw "Only compatible with Powershell version 2 and higher"
}
# create PowerShell Profile
if (-not (Test-Path $Profile)){
# Create a profile
Write-Host "Add a default profile script"
New-Item -path $Profile -type file -force | Out-Null
}
#--------------------------------------------------#
# global settings
#--------------------------------------------------#
# load global configurations
$PSProfileConfig = Join-Path -Path (Get-Location).Path -ChildPath "Microsoft.PowerShell_profile.config.ps1"
if((Test-Path $PSProfileConfig) -and ($PSProfile -eq $null -or $Force)){
iex $PSProfileConfig
}elseif($PSProfile -ne $null){
Write-Host "Using global configuration of this session"
}elseif(-not (Test-Path $PSProfileConfig)){
throw "Couldn't find $PSProfileConfig"
}
#--------------------------------------------------#
# prerequisites
#--------------------------------------------------#
# autoinclude functions
Get-childitem ($PSfunctions.Path) -Recurse | where{($_.Name.EndsWith("ps1")) -and (-not $_.PSIsContainer)} | foreach{. ($_.Fullname)}
#--------------------------------------------------#
# Profile settings
#--------------------------------------------------#
# cast vars
$Features = @()
$Systemvariables = @()
# load configuration files
Get-ChildItem -Path $PSconfigs.Path -Filter $PSconfigs.Profile.Filter -Recurse | %{
[xml]$(get-content $_.FullName)} | %{
$Features += $_.Content.Feature;$Systemvariables += $_.Content.Systemvariable}
#--------------------------------------------------#
# Profile scripts
#--------------------------------------------------#
# cast vars
$PPContent = @()
$PPISEContent = @()
# Metadata
$PPContent += @'
$Metadata = @{
Title = "Powershell Profile"
Filename = "Microsoft.PowerShell_profile.ps1"
Description = ""
Tags = "powershell, profile"
Project = ""
Author = "Janik von Rotz"
AuthorContact = "http://janikvonrotz.ch"
CreateDate = "2013-04-22"
LastEditDate = "2014-01-29"
Version = "7.1.1"
License = "This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA."
}
'@
# Metadata ISE
$PPISEContent += @'
$Metadata = @{
Title = "Powershell ISE Profile"
Filename = "Microsoft.PowerShellISE_profile.ps1"
Description = ""
Tags = "powershell, ise, profile"
Project = ""
Author = "Janik von Rotz"
AuthorContact = "http://janikvonrotz.ch"
CreateDate = "2013-04-22"
LastEditDate = "2014-01-29"
Version = "7.1.1"
License = "This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA."
}
'@
#--------------------------------------------------#
# default features
#--------------------------------------------------#
$PPContent += $Content = @'
# main
. '
'@ + (Join-Path -Path $PSProfile.Path -ChildPath "Microsoft.PowerShell_profile.config.ps1") + "'`n"
$PPISEContent += $Content
# Autoinclude Functions
Write-Host "Add Autoinclude Functions to the profile script"
$PPContent += $Content = @'
# Autoinclude Functions
Get-childitem ($PSfunctions.Path) -Recurse | where{($_.Name.EndsWith("ps1")) -and (-not $_.PSIsContainer)} | foreach{. ($_.Fullname)}
'@
$PPISEContent += $Content
# Transcript Logging
Write-Host "Add Transcript Logging to the profile script"
$PPContent += @'
# Transcript Logging
Start-Transcript -path $PSlogs.SessionFile
Write-Host ""
'@
#--------------------------------------------------#
# functions
#--------------------------------------------------#
function Check-ProfileFeatureStatus{
param(
[string]
$Name
)
if($Features | Where{($_.Name -eq $Name) -and ($_.Status -eq "Enabled")}){
$true
}elseif($Features | Where{($_.Name -eq $Name) -and ($_.Status -eq "Disabled")}){
$false
}else{
throw "Could not find feature definition for: $Name"
}
}
#--------------------------------------------------#
# customizable features
#--------------------------------------------------#
# Enable Open Powershell here
if(Check-ProfileFeatureStatus "Enable Open Powershell here"){
Write-Host "Adding 'Open PowerShell Here' to context menu "
(Get-ChildItem -Path $PStemplates.Path -Filter "Open PowerShell Here.reg" -Recurse) | %{Invoke-Expression "regedit /s '$($_.Fullname)'"}
}
# Get Quote Of The Day
if(Check-ProfileFeatureStatus "Get Quote Of The Day"){
Write-Host "Add Get Quote Of The Day to the profile script"
$PPContent += $Content = @'
# Get Quote Of The Day
Get-QuoteOfTheDay
Write-Host ""
'@
$PPISEContent += $Content
}
# Git Update
if(Check-ProfileFeatureStatus "Git Update"){
Update-PowerShellPowerUp
Copy-PPConfigurationFile -Name $PStemplates.GitUpdate.Name -Force:$Force
Update-ScheduledTask "Git Update"
}
# Log File Retention
if($Features | Where{($_.Name -contains "Log File Retention") -and ($_.Status -eq "Enabled") -and ($_.Run -match "asDailyJob")}){
Copy-PPConfigurationFile -Name $PStemplates.LogFileRetention.Name -Force:$Force
Update-ScheduledTask "Log File Retention"
}
# Log File Retention
if($Features | Where{($_.Name -contains "Log File Retention") -and ($_.Status -eq "Enabled") -and ($_.Run -match "withProfileScript")}){
Write-Host "Add Log File Retention to the profile script"
$PPContent += $Content = @'
# Log File Retention
Delete-ObsoleteLogFiles
'@
$PPISEContent += $Content
}
# Powershell Remoting
if(Check-ProfileFeatureStatus "Powershell Remoting"){
Write-Host "Enabling Powershell Remoting"
try{Enable-PSRemoting -Confirm:$false}catch{$Error}
Set-Item WSMan:\localhost\Client\TrustedHosts "RemoteComputer" -Force
Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1024
restart-Service WinRM -Confirm:$false
}
# Custom PowerShell Profile script
if(Check-ProfileFeatureStatus "Custom PowerShell Profile script"){
Copy-PPConfigurationFile -Name $PStemplates.CustomPPscript.Name -Force:$Force
Write-Host "Include Custom PowerShell Profile script"
$PPContent += $(Get-Content (Get-ChildItem -Path $PSconfigs.Path -Filter $PStemplates.CustomPPscript.Name -Recurse).Fullname) + "`n"
}
# Custom PowerShell Profile ISE script
if(Check-ProfileFeatureStatus "Custom PowerShell Profile ISE script"){
Copy-PPConfigurationFile -Name $PStemplates.CustomPPISEscript.Name -Force:$Force
Write-Host "Include Custom PowerShell Profile script"
$PPISEContent += $Content = $(Get-Content (Get-ChildItem -Path $PSconfigs.Path -Filter $PStemplates.CustomPPISEscript.Name -Recurse).Fullname) + "`n"
}
# add system variables
if($SystemVariables -ne $Null){$SystemVariables | %{
$Path = Get-Path $_.Value
if(Test-Path $Path){
$SystemVariable = $_
Set-EnvironmentVariableValue -Name $SystemVariable.Name -Value (";" + $Path) -Target $SystemVariable.Target -Add
}else{
Write-Error "Path: $Path doesn't exist. Not possible to add value to system variable: $($_.Name)"
}
}
}
#--------------------------------------------------#
# feature selection end
#--------------------------------------------------#
$PPContent += $Content = @'
# main end
Set-Location $WorkingPath
'@
$PPISEContent += $Content
# Write content to PowerShell Profile script file
Write-Host "Creating PowerShell Profile Script"
Set-Content -Value $PPContent -Path $Profile
# Add ISE Profile Script
Write-Host "Creating PowerShell ISE Profile Script"
Set-Content -Value $PPISEContent -Path (Join-Path -Path (Split-Path $profile -Parent) -ChildPath "Microsoft.PowerShellISE_profile.ps1")
Set-Location $WorkingPath
Write-Host "Finished" -BackgroundColor Black -ForegroundColor Green