forked from microsoft/Windows-universal-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetupServer.ps1
77 lines (67 loc) · 2.97 KB
/
SetupServer.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
#*********************************************************
#
# Copyright (c) Microsoft. All rights reserved.
# THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
# ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
# IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
# PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
#
#*********************************************************
$scriptPath = $(Split-Path $MyInvocation.MyCommand.Path)
$iisAppName = "BackgroundTransferSample"
$iisAppPath = "$env:systemdrive\inetpub\wwwroot\BackgroundTransferSample"
$websitePath = "$scriptPath\website"
$firewallRuleName = "BackgroundTransferSample - HTTP 80"
$requiredFeatures = "IIS-WebServer", "IIS-WebServerRole", "NetFx4Extended-ASPNET45", "IIS-ApplicationDevelopment", "IIS-ASPNET45", "IIS-ISAPIExtensions", "IIS-ISAPIFilter", "IIS-NetFxExtensibility45"
$settingsFile = "$scriptPath\BackgroundTransferSampleScriptSettings"
# Check if running as Administrator.
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object System.Security.Principal.WindowsPrincipal($windowsIdentity)
$administratorRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator
if ($principal.IsInRole($administratorRole) -eq $false)
{
"Please run the script from elevated PowerShell."
return
}
# Check if the config file was created. If yes, user should first run RemoveServer.ps1.
if (Test-Path $settingsFile)
{
"The script has already been run. Please run RemoveServer.ps1 before running it again."
return;
}
# Get features that should be enabled.
$featuresToEnable = @()
Get-WindowsOptionalFeature -Online | ?{ $_.State -eq [Microsoft.Dism.Commands.FeatureState]::Disabled -and $requiredFeatures -contains $_.FeatureName } | %{ $featuresToEnable += $_.FeatureName }
# Save enabled features to the config file.
$featuresToEnable -join "," > $settingsFile
# Enable features.
if ($featuresToEnable.Count -gt 0)
{
"Enabling following features: $($featuresToEnable -join ", ")."
Enable-WindowsOptionalFeature -Online -FeatureName $featuresToEnable > $null
}
# Copy the webpage files
if (-not (Test-Path $iisAppPath))
{
mkdir $iisAppPath > $null
Copy-Item $websitePath\* $iisAppPath -r
# Allow IIS_IUSRS to write to this directory.
$acl = Get-Acl $iisAppPath\data
$writePermission = "BUILTIN\IIS_IUSRS", "Write", "Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $writePermission
$acl.SetAccessRule($accessRule)
Set-Acl $iisAppPath\data $acl
}
# Add web application.
if ($(Get-WebApplication $iisAppName) -eq $null)
{
"Adding a `'$iisAppName`' web application at $iisAppPath."
New-WebApplication -Site "Default Web Site" -Name $iisAppName -PhysicalPath $iisAppPath > $null
}
else
{
"Web application `'$iisAppName`' already exists."
}
# Add firewall rule.
"Adding firewall rule `'$firewallRuleName`'."
New-NetFirewallRule -DisplayName $firewallRuleName -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow > $null