Skip to content

Instantly share code, notes, and snippets.

@randomcodenz
Last active January 2, 2016 17:59
Show Gist options
  • Save randomcodenz/8340183 to your computer and use it in GitHub Desktop.
Save randomcodenz/8340183 to your computer and use it in GitHub Desktop.
<#
.Synopsis
Provides a PowerShell wrapper over MSDeploy.
.Description
Allows MSDeploy to be invoked via powershell without shelling out to Start-Process or cmd.exe. Abuses reflection and internal knowledge of the msdeploy exe - may fail on other versions
.Parameter Verb
Action to perform.
.Parameter Source
The source object for the operation.
.Parameter Dest
The destination object for the operation.
.Parameter SetParamsFile
Applies parameter settings from an xml file.
.Parameter DisableLinks
Disables the specified link extension(s).
.Parameter EnableLinks
Enables the specified link extension(s).
.Parameter DisableRule
Disables the specified synchronization rule(s).
.Parameter EnableRule
Enables the specified synchronization rule(s).
.Parameter WhatIf
Displays what would have happened without actually performing any operations.
.Parameter AllowUntrusted
Allow untrusted server certificate when using SSL.
.Example
# See what will happen when syncing a package using a manifest and a SetParameters file against a server with an untrusted certificate with verbose output
$verb = 'sync'
$source = 'package=C:\development\MyPackage.zip'
$destination = 'manifest=C:\development\DestManifest.xml'
$setParamsFile = 'C:\development\MySite.SetParameters.xml'
$enableRules = "AppOffline","DoNotDeleteRule"
Invoke-MSDeploy -Verb $verb -Source $source -Dest $destination -SetParamsFile $setParamsFile -EnableRules $enableRules -AllowUntrusted -WhatIf -Verbose
#>
function Invoke-MSDeploy
{
param(
[Parameter(Mandatory=$true)]
[string]$Verb,
[string]$Source,
[string]$Dest,
[string]$SetParamsFile,
[string[]]$DisableLinks,
[string[]]$EnableLinks,
[string[]]$DisableRules,
[string[]]$EnableRules,
[switch]$WhatIf,
[switch]$AllowUntrusted
)
$debug = $false
if($DebugPreference) {
$debug = $true
}
$trace = $false
$help = $false
$argList = New-Object System.Collections.Generic.List[System.String]
$argList.Add( "-verb:$Verb" )
if($Source) {
$argList.Add( "-source:$Source" )
}
if($Dest) {
$argList.Add( "-dest:$Dest" )
}
if($SetParamsFile) {
$argList.Add( "-setParamFile:$SetParamsFile" )
}
if ($DisableLinks) {
$DisableLinks | % { $argList.Add( "-disableLink:$_" ) }
}
if ($EnableLinks) {
$EnableLinks | % { $argList.Add( "-enableLink:$_" ) }
}
if ($DisableRules) {
$DisableRules | % { $argList.Add( "-disableRule:$_" ) }
}
if ($EnableRules) {
$EnableRules | % { $argList.Add( "-enableRule:$_" ) }
}
if ($AllowUntrusted) {
$argList.Add( "-allowUntrusted" )
}
if ($WhatIf) {
$argList.Add( "-whatif" )
}
if ($VerbosePreference) {
$argList.Add( "-verbose" )
}
$verboseArguments = [System.String]::Join( " ", $argList.ToArray() )
Write-Verbose "Executing MSDeploy with the following parameters: debug=$debug, trace=$trace, help=$help"
Write-Verbose "Executing MSDeploy with the following arguments: $verboseArguments"
# Screw powershell / msdeploy command-line shenanigans - create an instance of the ms deploy class and manipulate it via reflection
$msDeployPath = (Join-Path (gci "HKLM:\SOFTWARE\Microsoft\IIS Extensions\MSDeploy" | Select -last 1).GetValue("InstallPath") "msdeploy.exe")
$msDeployExe = [Reflection.Assembly]::LoadFile($msDeployPath)
$msDeploy = $msDeployExe.GetType( "MSDeploy.MSDeploy" )
# MSDeploy( bool debug, bool trace, bool help, IEnumerable<string> args )
$createMsDeploySignature = [Type[]]@( [bool], [bool], [bool], [System.Collections.Generic.IEnumerable[string]] )
$bindingFlags= [Reflection.BindingFlags] "NonPublic,Instance"
$createMsDeploy = $msDeploy.GetConstructor( $bindingFlags, $null, $createMsDeploySignature, $null );
$msDeployConstructorArgs = [object[]]@( $debug, $trace, $help, [System.Collections.Generic.IEnumerable[string]]$argList )
$msDeployInstance = $createMsDeploy.Invoke( $msDeployConstructorArgs );
$msDeployExecute = $msDeploy.GetMethod( "Execute", $bindingFlags );
$msDeployExecute.Invoke( $msDeployInstance, $null );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment