-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.ps1
More file actions
482 lines (418 loc) · 17.4 KB
/
functions.ps1
File metadata and controls
482 lines (418 loc) · 17.4 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# Check for admin privileges, without which this script will not work
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "You need to run this script as an Administrator."
Break
}
# Load IIS commands into session
Import-Module WebAdministration
# Workaround for bug where IIS module takes time to load. Example error message is "Get-ChildItem : Could not load file or assembly 'Microsoft.PowerShell.Commands.Management'
# or one of its dependencies. The system cannot find the file specified."
# http://stackoverflow.com/questions/14862854/powershell-command-get-childitem-iis-sites-causes-an-error
Sleep 1
# Functions to ensure ASP.NET 4 is enabled on the IIS server. Script from http://www.ifunky.net/Blog/post/How-To-Enable-IIS-ISAPI-CGI-Restrictions-With-Powershell.aspx
# To use these, just call "EnableDotNet40InIIS"
function Is64Bit
{
[IntPtr]::Size -eq 8
}
function EnableIsapiRestriction($isapiPath){
$isapiConfiguration = get-webconfiguration "/system.webServer/security/isapiCgiRestriction/add[@path='$isapiPath']/@allowed"
if (!$isapiConfiguration.value){
set-webconfiguration "/system.webServer/security/isapiCgiRestriction/add[@path='$isapiPath']/@allowed" -value "True" -PSPath:IIS:\
Write-Host "Enabled ISAPI - $isapiPath " -ForegroundColor Green
}
}
function EnableDotNet40Isapi($systemArchitecture){
$frameworkPath = "$env:windir\Microsoft.NET\Framework$systemArchitecture\v4.0.30319\aspnet_isapi.dll"
EnableIsapiRestriction $frameworkPath
}
function EnableDotNet40InIIS() {
Write-Host "Ensuring ASP.NET 4.0 ISAPI filter is enabled in IIS"
if (Is64Bit){
EnableDotNet40Isapi "64"
}
EnableDotNet40Isapi
}
# Create an application pool if it doesn't already exist
function CreateApplicationPool($applicationPoolName, $classicMode, $dotNet2) {
if (@(Get-ChildItem IIS:\AppPools | Where-Object {$_.Name -eq $applicationPoolName}).Length -eq 0)
{
Write-Host Creating application pool $applicationPoolName
New-WebAppPool -Name $applicationPoolName
if ($classicMode) {
Set-ItemProperty "IIS:\AppPools\$applicationPoolName" -name managedPipelineMode -value 1
}
if ($dotNet2) {
Set-ItemProperty "IIS:\AppPools\$applicationPoolName" managedRuntimeVersion v2.0
} else {
Set-ItemProperty "IIS:\AppPools\$applicationPoolName" managedRuntimeVersion v4.0
}
}
else
{
Write-Host Application pool $applicationPoolName already exists
}
}
# Create a self-signed certificate to run the site over SSL, in a way supported by IIS7. Script is from
# http://blogs.technet.com/b/vishalagarwal/archive/2009/08/22/generating-a-certificate-self-signed-using-powershell-and-certenroll-interfaces.aspx
# The only alteration is changing the string in the second line to the $projectName variable.
function CreateSSLCertificate($certificateName) {
$sslCertificate = Get-ChildItem 'CERT:\LocalMachine\My' | Where-Object { $_.Subject -ilike "*$certificateName*" };
if (-not $sslCertificate) {
Write-Host
Write-Host Creating self-signed SSL certificate $certificateName
$name = new-object -com "X509Enrollment.CX500DistinguishedName.1"
$name.Encode("CN=$certificateName", 0)
$key = new-object -com "X509Enrollment.CX509PrivateKey.1"
$key.ProviderName = "Microsoft RSA SChannel Cryptographic Provider"
$key.KeySpec = 1
$key.Length = 1024
$key.SecurityDescriptor = "D:PAI(A;;0xd01f01ff;;;SY)(A;;0xd01f01ff;;;BA)(A;;0x80120089;;;NS)"
$key.MachineContext = 1
$key.Create()
$serverauthoid = new-object -com "X509Enrollment.CObjectId.1"
$serverauthoid.InitializeFromValue("1.3.6.1.5.5.7.3.1")
$ekuoids = new-object -com "X509Enrollment.CObjectIds.1"
$ekuoids.add($serverauthoid)
$ekuext = new-object -com "X509Enrollment.CX509ExtensionEnhancedKeyUsage.1"
$ekuext.InitializeEncode($ekuoids)
$cert = new-object -com "X509Enrollment.CX509CertificateRequestCertificate.1"
$cert.InitializeFromPrivateKey(2, $key, "")
$cert.Subject = $name
$cert.Issuer = $cert.Subject
$cert.NotBefore = get-date
$cert.NotAfter = $cert.NotBefore.AddDays(90)
$cert.X509Extensions.Add($ekuext)
$cert.Encode()
$enrollment = new-object -com "X509Enrollment.CX509Enrollment.1"
$enrollment.InitializeFromRequest($cert)
$certdata = $enrollment.CreateRequest(0)
$enrollment.InstallResponse(2, $certdata, 0, "")
}
else
{
Write-Host SSL certificate $certificateName already exists
}
}
# Create a web site if it doesn't already exist.
# 3rd parameter optional. Assumes the website uses an application pool with the same name.
function CreateWebsite($websiteName, $wwwrootPath, $applicationPoolName) {
if (@(Get-ChildItem IIS:\Sites | Where-Object {$_.Name -eq $websiteName}).Length -eq 0)
{
Write-Host
Write-Host Creating web site $websiteName
# If there are no websites, specify the ID parameter to avoid a Powershell bug
if (@(Get-ChildItem IIS:\Sites).Length -eq 0)
{
New-Website -Name $websiteName -Id 0
}
else
{
New-Website -Name $websiteName
}
Set-ItemProperty "IIS:\Sites\$websiteName" -Name PhysicalPath -Value "$wwwrootPath"
## Remove the default binding so that we can set up our own bindings without having to clean up first
# RemoveHTTPBinding $websiteName 80
## https://technet.microsoft.com/en-us/library/hh867895(v=wps.630).aspx
Get-WebBinding -Name $websiteName | Remove-WebBinding
}
else
{
Write-Host Web site $websiteName already exists
}
if (!$applicationPoolName) {
$applicationPoolName = $websiteName
}
Write-Host "Setting web site $websiteName to use application pool $applicationPoolName"
Set-ItemProperty "IIS:\Sites\$websiteName" -Name ApplicationPool -Value $applicationPoolName
}
# Bind site to a custom port using the SSL certificate created earlier
function CreateHTTPSBinding($websiteName, $certificateName, $port) {
if (@(Get-ChildItem IIS:\Sites | Where-Object {$_.Name -eq $websiteName}).Length -eq 1)
{
# If there are no bindings on any protocol Get-WebBinding throws an exception. This catches it, so that we can test for having no bindings.
trap [System.Management.Automation.PSArgumentNullException] {
continue
}
$httpsBindings = Get-WebBinding -Name $websiteName -Protocol https
if (!$httpsBindings)
{
if (!$port) {
Write-Host
$port = Read-Host "What HTTPS port would you like $websiteName to use?"
Write-Host
}
Write-Host Binding website $websiteName to port $port using HTTPS
# Binding code is from comment by Dynamotion on https://social.technet.microsoft.com/Forums/lync/en-US/4f083f00-1f4c-466e-acf8-7ca8bb5baddf/unable-to-enable-https-binding-for-website-using-powershell?forum=winserverpowershell
if (!$certificateName) {
$certificateName = "localhost"
}
New-WebBinding -Name $websiteName -IP "*" -Port $port -Protocol https
$cert=Get-ChildItem -Path Cert:\LocalMachine\My | where-Object {$_.subject -like "*$certificateName*"} | Select-Object -ExpandProperty Thumbprint
get-item -Path "cert:\localmachine\my\$cert" | new-item -path IIS:\SslBindings\0.0.0.0!$port
}
else
{
$existingPort = @(Get-WebBinding -Name $websiteName -Protocol https)[0].BindingInformation -replace "[^0-9]", ""
Write-Host "Web site $websiteName is already bound to port $existingPort"
}
}
else
{
Write-Host Web site $websiteName does not exist
}
}
function CreateHTTPBinding($websiteName, $port) {
if (@(Get-ChildItem IIS:\Sites | Where-Object {$_.Name -eq $websiteName}).Length -eq 1)
{
# If there are no bindings on any protocol Get-WebBinding throws an exception. This catches it, so that we can test for having no bindings.
trap [System.Management.Automation.PSArgumentNullException] {
continue
}
$httpBindings = Get-WebBinding -Name $websiteName -Protocol http
if (!$httpBindings)
{
if (!$port) {
Write-Host
$port = Read-Host "What HTTP port would you like $websiteName to use?"
Write-Host
}
Write-Host Binding website $websiteName to port $port using HTTP
# Binding code is from comment by Dynamotion on https://social.technet.microsoft.com/Forums/lync/en-US/4f083f00-1f4c-466e-acf8-7ca8bb5baddf/unable-to-enable-https-binding-for-website-using-powershell?forum=winserverpowershell
New-WebBinding -Name $websiteName -IP "*" -Port $port -Protocol http
}
else
{
$existingPort = @(Get-WebBinding -Name $websiteName -Protocol http)[0].BindingInformation -replace "[^0-9]", ""
Write-Host "Web site $websiteName is already bound to port $existingPort"
}
}
else
{
Write-Host Web site $websiteName does not exist
}
}
function RemoveHTTPBinding($websiteName, $port) {
if (@(Get-ChildItem IIS:\Sites | Where-Object {$_.Name -eq $websiteName}).Length -eq 1)
{
if (@(Get-WebBinding -Name $websiteName -Protocol http -Port $port).Length -ge 1)
{
Write-Host Removing HTTP binding for web site $projectName on port $port
Remove-WebBinding -Name $websiteName -Protocol http -Port $port
}
}
else
{
Write-Host Web site $websiteName does not exist
}
}
function DisableAnonymousAuthentication($websiteName, $directoryUrl) {
SwitchAuthentication $websiteName $directoryUrl "anonymousAuthentication" False "Disabling anonymous authentication"
}
function EnableWindowsAuthentication($websiteName, $directoryUrl) {
SwitchAuthentication $websiteName $directoryUrl "windowsAuthentication" True "Enabling Windows authentication"
}
# Private method with shared code for DisableAnonymousAuthentication and EnableWindowsAuthentication
function SwitchAuthentication($websiteName, $directoryUrl, $nodeName, $enabled, $message) {
if (@(Get-ChildItem IIS:\Sites | Where-Object {$_.Name -eq $websiteName}).Length -eq 1)
{
$pathToUpdate = "$websiteName"
if ($directoryUrl)
{
$pathToUpdate = $pathToUpdate + "/$directoryUrl"
if ((Test-Path "IIS:\Sites\$pathToUpdate") -eq 0)
{
"Directory $directoryUrl does not exist in IIS site $websiteName"
Break
}
}
Write-Host "$message for $pathToUpdate"
Set-WebConfigurationProperty -Filter "/system.webServer/security/authentication/$nodeName" -Name Enabled -Value $enabled -PSPath IIS:\ -Location $pathToUpdate
}
else
{
Write-Host Web site $websiteName does not exist
}
}
# Creates a virtual directory. If the optional $applicationPoolName is specified, it's setup as an application using that app pool.
function CreateVirtualDirectory($websiteName, $virtualDirectoryUrl, $virtualDirectoryPath, $allowScripts, $applicationPoolName) {
if (@(Get-ChildItem IIS:\Sites | Where-Object {$_.Name -eq $websiteName}).Length -eq 1)
{
if (Test-Path "IIS:\Sites\$websiteName\$virtualDirectoryUrl")
{
if ($applicationPoolName)
{
Write-Host "Application $virtualDirectoryUrl already exists"
}
else
{
Write-Host "Virtual directory $virtualDirectoryUrl already exists"
}
}
else
{
if ((Test-Path $virtualDirectoryPath) -eq 0)
{
Write-Host "Creating physical directory $virtualDirectoryPath"
md $virtualDirectoryPath
}
if ($applicationPoolName)
{
Write-Host "Creating application $virtualDirectoryUrl"
New-Item "IIS:\Sites\$websiteName\$virtualDirectoryUrl" -PhysicalPath $virtualDirectoryPath -Type Application
}
else
{
Write-Host "Creating virtual directory $virtualDirectoryUrl"
New-WebVirtualDirectory -Site $websiteName -Name $virtualDirectoryUrl -PhysicalPath $virtualDirectoryPath
}
}
if ($applicationPoolName)
{
Write-Host "Setting application pool to $applicationPoolName"
Set-ItemProperty "IIS:\Sites\$websiteName\$virtualDirectoryUrl" -Name applicationPool -Value $applicationPoolName
}
if ($allowScripts)
{
$accessPolicy = "Read,Script"
}
else
{
$accessPolicy = "Read"
}
Write-Host "Setting virtual directory feature permissions to $accessPolicy"
Set-WebConfigurationProperty -PSPath "IIS:\Sites\$websiteName\$virtualDirectoryUrl" -Filter '/system.webserver/handlers' -Name accessPolicy -Value $accessPolicy
}
else
{
Write-Host Web site $websiteName does not exist
}
}
# Make a copy of a folder before changing its contents
function BackupApplication($applicationFolder, $backupFolder, $comment) {
if ((Test-Path $applicationFolder) -eq 1)
{
# Try to make the comment safe for a folder name
$invalidCharacters = "[{0}]" -f ([Regex]::Escape( [System.IO.Path]::GetInvalidFileNameChars() -join '' ))
$comment = $comment -replace $invalidCharacters, ""
# Create a folder for this application
$path = [System.IO.Path];
$applicationBackupFolder = $path::GetFileName($path::GetDirectoryName($applicationFolder.Trim() + "/"))
# Create a folder for this specific backup
$thisBackupFolder = ("{0} {1} {2}" -f (Get-Date).ToString("s").Replace(":","."), $env:USERNAME, $comment).Trim();
md -Force "$backupFolder\$applicationBackupFolder\$thisBackupFolder"
# Copy the entire contents of the source folder to the backup
robocopy $applicationFolder "$backupFolder\$applicationBackupFolder\$thisBackupFolder" /MIR
}
}
# Copy a *.example.config to a *.config file
function CopyConfig($from, $to) {
if (Test-Path $to) {
Write-Host "$to already exists"
} else {
Copy-Item $from $to
Write-Host "Created $to"
}
}
# Copy *.example.config to a *.config file, transforming it using an XDT file
function TransformConfig($from, $to, $transformFile) {
if ((Test-Path Env:\TEMP) -eq 0)
{
Write-Warning "The TEMP environment variable is not set"
Break
}
if ((Test-Path Env:\MSBUILD_PATH) -eq 0)
{
Write-Warning "The MSBUILD_PATH environment variable is not set"
Break
}
if ((Test-Path $from) -eq 0)
{
Write-Warning "File not found $from"
Break
}
if ((Test-Path $transformFile) -eq 0)
{
Write-Warning "File not found $transformFile"
Break
}
# Transform the file, but via a temp file to avoid file locking problems
$tempFile = $env:TEMP.Trim("\") + "\" + [Guid]::NewGuid().ToString() + ".transform"
$scriptPath = Split-Path -Parent $PSCommandPath
Invoke-Expression '& ${Env:MSBUILD_PATH} "$scriptPath\TransformConfig.xml" /p:TransformInputFile="$from" /p:TransformFile="$transformFile" /p:TransformOutputFile="$tempFile"'
Copy-Item $tempFile $to
Remove-Item $tempFile
}
# Run nuget restore on an individual project
function NuGetRestoreForProject($parentFolderPath, $projectName) {
if (Get-Command "nuget.exe" -ErrorAction SilentlyContinue)
{
$parentFolderPath = $parentFolderPath.TrimEnd("/", "\")
$projectFolderPath = "$parentFolderPath\$projectName"
Write-Host "Restoring NuGet packages for $projectFolderPath"
& nuget restore "$projectFolderPath\packages.config" -PackagesDirectory "$projectFolderPath\packages"
} else {
Write-Warning "Unable to restore NuGet packages because nuget.exe was not found in your path. If you get build errors, add nuget.exe to your path and run this script again."
}
}
# Check root site is set up, so that we can set this up as an application within it
function CheckSiteExistsBeforeAddingApplication($websiteName)
{
if (@(Get-ChildItem IIS:\Sites | Where-Object {$_.Name -eq $websiteName}).Length -eq 0)
{
Write-Warning "You need to set up the $websiteName website first, before adding this application to it. Run app-setup-dev.cmd in the $websiteName project, then try this script again."
Break
}
}
# Check another application is already present before installing this one
function CheckApplicationExists($destinationFolder, $application)
{
if ((Test-Path "$destinationFolder/$application") -eq 0)
{
Write-Warning "You need to set up the $application application first, then try this script again."
Break
}
}
# Download a project from git if it's not already found
function DownloadProjectIfMissing($parentFolderPath, $projectName) {
$projectPath = Join-Path -Path $parentFolderPath -ChildPath $projectName
if (Test-Path $projectPath) {
Write-Host "Checking $projectName is up-to-date"
Push-Location $projectPath
git pull origin master
Pop-Location
Write-Host
} else {
if ($env:GIT_ORIGIN_URL) {
$repoUrl = $env:GIT_ORIGIN_URL -f $projectName
git clone $repoUrl $projectPath
Write-Host
}
else
{
Write-Warning '$projectName project not found. Please set a GIT_ORIGIN_URL environment variable on your system so that it can be downloaded.
Example: C:\>set GIT_ORIGIN_URL=https://example-git-server.com/{0}"
{0} will be replaced with the name of the repository to download.'
Return
}
}
}
# Ensure any user-submitted paths resolve to an absolute file path
function NormaliseFolderPath($path, $defaultPath)
{
if (!$path)
{
$path = $defaultPath
}
if ($path)
{
$path = $path.Trim()
if (!$path.StartsWith("\\"))
{
$path = Resolve-Path $path
}
}
return $path
}