This repository was archived by the owner on May 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSystemPrep-Content-WindowsTemplate.ps1
More file actions
137 lines (120 loc) · 5.64 KB
/
Copy pathSystemPrep-Content-WindowsTemplate.ps1
File metadata and controls
137 lines (120 loc) · 5.64 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
[CmdLetBinding()]
Param(
[Parameter(Mandatory=$false,Position=0,ValueFromRemainingArguments=$true)]
$RemainingArgs
,
[Parameter(Mandatory=$false,ValueFromPipeLine=$false,ValueFromPipeLineByPropertyName=$false)]
[switch] $SourceIsS3Bucket
,
[Parameter(Mandatory=$false,ValueFromPipeLine=$false,ValueFromPipeLineByPropertyName=$false)]
[string] $AwsRegion
# ,
# [Parameter(Mandatory=$true,Position=0,ValueFromPipeLine=$false,ValueFromPipeLineByPropertyName=$false)]
# [ValidateSet("value1","value2","etc")]
# [string] $ParamName
)
#Parameter Descriptions
#$RemainingArgs #Parameter that catches any undefined parameters passed to the script.
#Used by the bootstrapping framework to pass those parameters through to other scripts.
#This way, we don't need to know in advance all the parameter names for downstream scripts.
#$SourceIsS3Bucket #Set to $true if all content to be downloaded is hosted in an S3 bucket and should be retrieved using AWS tools.
#$AwsRegion #Set to the region in which the S3 bucket is located.
#$ParamName #ParamDescription...
#System variables
$ScriptName = $MyInvocation.mycommand.name
$SystemRoot = $env:SystemRoot
$ScriptStart = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
$ScriptEnd = "--------------------------------------------------------------------------------"
#Convert RemainingArgs to a hashtable
if ($PSVersionTable.PSVersion -eq "2.0") { #PowerShell 2.0 receives remainingargs in a different format than PowerShell 3.0
$RemainingArgsHash = $RemainingArgs | ForEach-Object -Begin { $index = 0; $hash = @{} } -Process { if ($index % 2 -eq 0) { $hash[$_] = $RemainingArgs[$index+1] }; $index++ } -End { Write-Output $hash }
} else {
$RemainingArgsHash = $RemainingArgs | ForEach-Object -Begin { $index = 0; $hash = @{} } -Process { if ($_ -match "^-.*:$") { $hash[($_.trim("-",":"))] = $RemainingArgs[$index+1] }; $index++ } -End { Write-Output $hash }
}###
###
#User variables
$PackageUrl = ""
$TemplateWorkingDir = ""
###
#Define functions
###
function log {
[CmdLetBinding()]
Param(
[Parameter(Mandatory=$false,Position=0,ValueFromPipeLine=$true,ValueFromPipeLineByPropertyName=$true)] [string[]]
$LogMessage,
[Parameter(Mandatory=$false,Position=1,ValueFromPipeLine=$false,ValueFromPipeLineByPropertyName=$true)] [string]
$LogTag
)
PROCESS {
foreach ($message in $LogMessage) {
$date = get-date -format "yyyyMMdd.HHmm.ss"
"${date}: ${LogTag}: $message" | Out-Default
}
}
}
function Download-File {
[CmdLetBinding()]
Param(
[Parameter(Mandatory=$true,Position=0,ValueFromPipeLine=$true,ValueFromPipeLineByPropertyName=$true)] [string[]] $Url,
[Parameter(Mandatory=$true,Position=1,ValueFromPipeLine=$false,ValueFromPipeLineByPropertyName=$false)] [string] $SavePath,
[Parameter(Mandatory=$false,Position=2,ValueFromPipeLine=$false,ValueFromPipeLineByPropertyName=$false)] [switch] $SourceIsS3Bucket,
[Parameter(Mandatory=$false,Position=3,ValueFromPipeLine=$false,ValueFromPipeLineByPropertyName=$false)] [string] $AwsRegion
)
BEGIN {
New-Item -Path ${SavePath} -ItemType Directory -Force -WarningAction SilentlyContinue > $null
}
PROCESS {
foreach ($url_item in $Url) {
$FileName = "${SavePath}\$((${url_item}.split('/'))[-1])"
if ($SourceIsS3Bucket) {
log -LogTag ${ScriptName} "Downloading file from S3 bucket: ${url_item}"
$SplitUrl = $url_item.split('/') | where { $_ -notlike "" }
$BucketName = $SplitUrl[2]
$Key = $SplitUrl[3..($SplitUrl.count-1)] -join '/'
$ret = Invoke-Expression "Powershell Read-S3Object -BucketName $BucketName -Key $Key -File $FileName -Region $AwsRegion"
}
else {
log -LogTag ${ScriptName} "Downloading file from HTTP host: ${url_item}"
(new-object net.webclient).DownloadFile("${url_item}","${FileName}")
}
Write-Output (Get-Item $FileName)
}
}
}
function Expand-ZipFile {
[CmdLetBinding()]
Param(
[Parameter(Mandatory=$true,Position=0,ValueFromPipeLine=$true,ValueFromPipeLineByPropertyName=$true)] [string[]] $FileName,
[Parameter(Mandatory=$true,Position=1,ValueFromPipeLine=$false,ValueFromPipeLineByPropertyName=$false)] [string] $DestPath,
[Parameter(Mandatory=$false,Position=2,ValueFromPipeLine=$false,ValueFromPipeLineByPropertyName=$false)] [switch] $CreateDirFromFileName
)
PROCESS {
foreach ($file in $FileName) {
$Shell = new-object -com shell.application
if (!(Test-Path "$file")) {
throw "$file does not exist"
}
log -LogTag ${ScriptName} "Unzipping file: ${file}"
if ($CreateDirFromFileName) { $DestPath = "${DestPath}\$((Get-Item $file).BaseName)" }
New-Item -Path $DestPath -ItemType Directory -Force -WarningAction SilentlyContinue > $null
$Shell.namespace($DestPath).copyhere($Shell.namespace("$file").items(), 0x14)
Write-Output (Get-Item $DestPath)
}
}
}
###
#Begin Script
###
#Create log entry to note the script that is executing
log -LogTag ${ScriptName} $ScriptStart
log -LogTag ${ScriptName} "Within ${ScriptName} --"
log -LogTag ${ScriptName} "RemainingArgsHash = $(($RemainingArgsHash.GetEnumerator() | % { `"-{0}: {1}`" -f $_.key, $_.value }) -join ' ')"
#Insert script commands
###
$PackageFile = Download-File -Url $PackageUrl -SavePath "${TemplateWorkingDir}" -SourceIsS3Bucket:$SourceIsS3Bucket -AwsRegion $AwsRegion
$PackageDir = Expand-ZipFile -FileName ${PackageFile} -DestPath -DestPath ${TemplateWorkingDir}
###
#Log exit from script
log -LogTag ${ScriptName} "Exiting ${ScriptName} --"
log -LogTag ${ScriptName} $ScriptEnd