-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPSWebDeploy.psm1
293 lines (247 loc) · 7.84 KB
/
PSWebDeploy.psm1
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
Set-StrictMode -Version Latest;
$Defaults = @{
MSDeployExePath = 'C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe'
}
if (-not (Test-Path -Path $Defaults.MSDeployExePath -PathType Leaf)) {
throw 'MSDeploy was not found. In order to use the MSDeploy module, you must have Web Deploy installed. It can be found at https://www.microsoft.com/en-us/download/details.aspx?id=43717'
}
function NewMsDeployCliArgumentString
{
[OutputType([string])]
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[ValidateSet('Sync')]
[string]$Verb,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[Alias('SourcePath')]
[Alias('SourcePackage')]
[string]$SourceContent,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ComputerName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[Alias('TargetPath')]
[string]$TargetContent,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
$Credential,
[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateSet('DoNotDelete')]
[string[]]$EnableRule,
[Parameter()]
[ValidateNotNullOrEmpty()]
[int]$RetryAttempts,
[Parameter()]
[ValidateNotNullOrEmpty()]
[int]$RetryInterval,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$AuthType = 'Basic'
)
$connHt = @{
ComputerName = $Computername
UserName = $Credential.UserName
Password = $Credential.GetNetworkCredential().Password
AuthType = $AuthType
}
$deployArgs = @{
verb = $Verb
}
if ($PSBoundParameters.ContainsKey('RetryInterval'))
{
$deployArgs.retryInterval = $RetryInterval
}
if ($PSBoundParameters.ContainsKey('RetryAttempts'))
{
$deployArgs.RetryAttempts = $RetryAttempts
}
if ($PSBoundParameters.ContainsKey('EnableRule'))
{
$deployArgs.EnableRule = $EnableRule -join ','
}
## If this is a ZIP file, it needs to be Package otherwise assuming it's a file path or a web service path
if (Test-Path -Path $SourceContent -PathType Leaf) {
$sourceProvider = 'Package'
} else {
$sourceProvider = 'ContentPath'
}
$targetProvider = 'ContentPath'
if (Test-Path -Path $SourceContent) {
## No authentication needed if source is a folder/file path
$deployArgs.source = @{
$sourceProvider = '"{0}"' -f $SourceContent
}
## Assuming that destination is a web service if source is not. Authentication needed.
$deployArgs.dest = ($connHt + @{
$targetProvider = '"{0}"' -f $TargetContent
})
} else {
## Assuming this is a web service. Authenticate here
$deployArgs.source = $connHt + @{
$sourceProvider = '"{0}"' -f $SourceContent
}
## Assuming that destination is a file/folder path. No authentication needed.
$deployArgs.dest = @{
$targetProvider = '"{0}"' -f $TargetContent
}
}
$argString = ''
$deployArgs.GetEnumerator().foreach({
if ($_.Value -is 'hashtable') {
$val = ''
$_.Value.GetEnumerator().foreach({
$val += "$($_.Key)=$($_.Value),"
})
$val = $val.TrimEnd(',')
} else {
$val = $_.Value
}
$argString += " -$($_.Key):$val"
})
$argString
}
function Invoke-MSDeploy
{
[OutputType([string])]
[CmdletBinding()]
param
(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Arguments
)
$stdOutTempFile = New-TemporaryFile
$stdErrTempFile = New-TemporaryFile
$startProcessParams = @{
FilePath = $Defaults.MSDeployExePath
ArgumentList = $Arguments
RedirectStandardError = $stdErrTempFile.FullName
RedirectStandardOutput = $stdOutTempFile.FullName
Wait = $true;
PassThru = $true;
NoNewWindow = $true;
}
$cmd = Start-Process @startProcessParams
$cmdOutput = Get-Content -Path $stdOutTempFile.FullName -Raw
$cmdError = Get-Content -Path $stdErrTempFile.FullName -Raw
if ([string]::IsNullOrEmpty($cmdOutput) -eq $false)
{
Write-Verbose -Message $cmdOutput
}
if ($cmd.ExitCode -ne 0)
{
throw $cmdError
}
Remove-Item -Path $stdOutTempFile.FullName,$stdErrTempFile.FullName -Force
}
#region function Sync-Website
function Sync-Website {
<#
.SYNOPSIS
This function uses msdeploy to copy files from a source location to a destination folder path or URL.
.EXAMPLE
PS> Sync-Website -SourcePath C:\TestSite -TargetPath wwwroot -ComputerName https://azureurl.com
.PARAMETER SourcePath
A mandatory string parameter (if not using SourcePackage) representing the location where the files are located.
.PARAMETER SourcePackage
A mandatory string parameter (if not using SourcePath) representing the location of a zip file that contains the
website files/folders.
.PARAMETER TargetPath
A mandatory string parameter representing the folder location to copy the files.
.PARAMETER ComputerName
A mandatory string parameter representing a computer name or a deployment URL.
.PARAMETER DoNotDelete
By default, any files/folders in the destination path will be removed if not in the SourcePath. Use this
parameter if you'd simply like to copy the contents from SourcePath to TargetPath without removing TargetPath
files/folders.
.PARAMETER RetryInterval
A optional int parameter representing the interval (in seconds) in which MSDeploy will attempt to retry the action. By default,
this is 10 seconds. This parameter is expressed in milliseconds.
.PARAMETER Credential
Specifies a user account that has permission to perform this action. The default is the current user.
Type a user name, such as 'User01' or 'Domain01\User01', or enter a variable that contains a PSCredential
object, such as one generated by the Get-Credential cmdlet. When you type a user name, you will be prompted for a password.
#>
[OutputType([void])]
[CmdletBinding()]
param
(
[Parameter(Mandatory,ParameterSetName = 'BySourcePath')]
[ValidateNotNullOrEmpty()]
[string]$SourcePath,
[Parameter(Mandatory,ParameterSetName = 'BySourcePackage')]
[ValidateNotNullOrEmpty()]
[string]$SourcePackage,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$TargetPath,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ComputerName,
[Parameter()]
[ValidateNotNullOrEmpty()]
[switch]$DoNotDelete,
[Parameter()]
[ValidateNotNullOrEmpty()]
[int]$RetryInterval = 10,
[Parameter()]
[ValidateNotNullOrEmpty()]
[int]$Timeout = 60,
[Parameter()]
[ValidateNotNullOrEmpty()]
$Credential
)
begin {
$ErrorActionPreference = 'Stop'
}
process {
try
{
$cliArgStringParams = @{
Verb = 'sync'
TargetPath = ($TargetPath -replace '/','\')
ComputerName = $ComputerName
Credential = $Credential
RetryInterval = ($RetryInterval * 10)
}
if ($PSCmdlet.ParameterSetName -eq 'BySourcePath') {
$cliArgStringParams.SourcePath = $SourcePath
} else {
$cliArgStringParams.SourcePackage = $SourcePackage
}
if ($DoNotDelete.IsPresent) {
$cliArgStringParams.EnableRule = 'DoNotDelete'
}
$argString = NewMsDeployCliArgumentString @cliArgStringParams
Write-Verbose -Message "Using the MSDeploy CLI string: [$($argString)]"
try {
Invoke-MSDeploy -Arguments $argString
} catch {
$timer = [Diagnostics.Stopwatch]::StartNew()
while ($timer.Elapsed.TotalSeconds -lt $Timeout) {
try {
Invoke-MSDeploy -Arguments $argString
} catch {
Write-Verbose -Message "MSdeploy failed. Retrying after [$($RetryInterval)] seconds..."
Start-Sleep -Seconds $RetryInterval
}
}
$timer.Stop()
if ($timer.Elapsed.TotalSeconds -gt $Timeout) {
throw 'Msdeploy timed out attempting to sync website.'
}
}
}
catch
{
$PSCmdlet.ThrowTerminatingError($_)
}
}
}
#endregion function Sync-Website