PSWebDeploy.psm1
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','Dump','Delete','GetDependencies','GetParameters','GetSystemInfo')] [string]$Verb, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [Alias('SourcePath')] [Alias('SourcePackage')] [string]$SourceContent, [Parameter()] [ValidateNotNullOrEmpty()] [Alias('TargetPath')] [string]$TargetContent, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string]$ComputerName, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [pscredential]$Credential, [Parameter()] [ValidateNotNullOrEmpty()] [ValidateSet('DoNotDelete')] [string[]]$EnableRule, [Parameter()] [ValidateNotNullOrEmpty()] [string]$AuthType = 'Basic' ) $connHt = @{ ComputerName = $Computername UserName = $Credential.UserName Password = $Credential.GetNetworkCredential().Password AuthType = $AuthType } $deployArgs = @{ verb = $Verb } if ($PSBoundParameters.ContainsKey('EnableRule')) { $deployArgs.EnableRule = $EnableRule -join ',' } if (Test-Path -Path $SourceContent -PathType Leaf) { $sourceProvider = 'Package' } elseif (Test-Path -Path $SourceContent -PathType Container) { $sourceProvider = 'ContentPath' } else { throw 'Could not determine the source provider from Source.' } if ($TargetPath) { $targetProvider = 'ContentPath' } if ($PSBoundParameters.ContainsKey('TargetContent') -and $PSBoundParameters.ContainsKey('SourceContent')) { $deployArgs.dest = ($connHt + @{ $targetProvider = '"{0}"' -f $TargetContent }) $deployArgs.source = @{ $sourceProvider = '"{0}"' -f $SourceContent } } elseif ($PSBoundParameters.ContainsKey('SourceContent')) { $deployArgs.source = ($connHt + @{ $sourceProvider = '"{0}"' -f $SourceContent }) } $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 ) Start-Process -FilePath $Defaults.MSDeployExePath -ArgumentList $Arguments -Wait -NoNewWindow } #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 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 = 'ByFolder')] [ValidateNotNullOrEmpty()] [string]$SourcePath, [Parameter(Mandatory,ParameterSetName = 'ByPackage')] [ValidateNotNullOrEmpty()] [ValidateScript({ $_ -match '\.zip$' })] [string]$SourcePackage, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string]$TargetPath, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string]$ComputerName, [Parameter()] [ValidateNotNullOrEmpty()] [switch]$DoNotDelete, [Parameter()] [ValidateNotNullOrEmpty()] [pscredential]$Credential ) begin { $ErrorActionPreference = 'Stop' } process { try { $cliArgStringParams = @{ Verb = 'sync' TargetContent = ($TargetPath -replace '/','\') ComputerName = $ComputerName Credential = $Credential } if ($PSCmdlet.ParameterSetName -eq 'ByFolder') { $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)]" Invoke-MSDeploy -Arguments $argString } catch { Write-Error $_.Exception.Message } } } #endregion function Sync-Website #region function Get-WebSiteFile function Get-WebSiteFile { [OutputType('string')] [CmdletBinding()] param ( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string]$ComputerName, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [pscredential]$Credential, [Parameter()] [ValidateNotNullOrEmpty()] [string]$SourcePath = 'wwwroot' ) begin { $ErrorActionPreference = 'Stop' } process { try { $cliArgStringParams = @{ Verb = 'dump' ComputerName = $ComputerName Credential = $Credential SourcePath = $SourcePath } $argString = NewMsDeployCliArgumentString @cliArgStringParams Write-Verbose -Message "Using the MSDeploy CLI string: [$($argString)]" Invoke-MSDeploy -Arguments $argString } catch { Write-Error -Message $_.Exception.Message } } } #endregion function Get-WebSite |