Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
DeployWebsite PowerShell WinRM Compatible
[CmdletBinding()]
param (
[Parameter(Mandatory=$True,Position=1)][string]$packagePath,
[Parameter(Mandatory=$True)][string]$siteName,
[Parameter(Mandatory=$True)][string]$siteFullPath,
[Parameter(Mandatory=$True)][string]$environmentName,
[Parameter(Mandatory=$False)][string]$projectUri
)
function Run-Command
{
param(
[string]$command,
[bool] $failOnErr = $true
)
$ErrorActionPreference = 'Continue'
if( $psversiontable.PSVersion.Major -le 4)
{
$result = cmd.exe /c "`"$command`""
}
else
{
$result = cmd.exe /c "$command"
}
$ErrorActionPreference = 'Stop'
if($failOnErr -and $LASTEXITCODE -ne 0)
{
throw $result
}
return $result
}
function DeployWebsite([string]$packagePath, [string]$siteName, [string]$siteFullPath, [string]$environmentName) {
$MSDeployKey = 'HKLM:\SOFTWARE\Microsoft\IIS Extensions\MSDeploy\3'
if(!(Test-Path $MSDeployKey)) {
throw "Could not find MSDeploy. Use Web Platform Installer to install the 'Web Deployment Tool' and re-run this command"
}
$InstallPath = (Get-ItemProperty $MSDeployKey).InstallPath
if(!$InstallPath -or !(Test-Path $InstallPath)) {
throw "Could not find MSDeploy. Use Web Platform Installer to install the 'Web Deployment Tool' and re-run this command"
}
$MSDeploy = Join-Path $InstallPath "msdeploy.exe"
if(!(Test-Path $MSDeploy)) {
throw "Could not find MSDeploy. Use Web Platform Installer to install the 'Web Deployment Tool' and re-run this command"
}
# To prevent App_Offline not found error during msdeploy that directory should exist!
New-Item -ItemType Directory -Force -Path $siteFullPath
# Write-Host "Package path $packagePath"
$uri = New-Object System.Uri($packagePath)
$fileServer = $uri.Host;
# Add AppOffline.htm
Copy-Item "\\$fileServer\files\app_offline.htm" $siteFullPath
# SET ENVIRONMENT NAME
[Environment]::SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", $environmentName, "User")
# DEPLOY!
$arguments = [string[]]@(
"-verb:sync",
"-source:package='$packagePath'",
"-dest:contentPath='$siteName'",
"-enableRule:AppOffline -skip:Directory=\\Lucene")
Start-Process $msdeploy -ArgumentList $arguments -NoNewWindow -Wait
# Remove AppOffline.htm to start IIS App
Remove-Item $siteFullPath\app_offline.htm
}
DeployWebsite $packagePath $siteName $siteFullPath $environmentName
# Application start
if($projectUri){
try {
$response = Invoke-WebRequest $projectUri
}
catch {
$_.Exception.Response.StatusCode.Value__
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment