Date: Thu, 28 Mar 2024 10:40:08 +0000 (GMT) Message-ID: <995842431.393.1711622408356@ip-172-31-38-4.eu-west-1.compute.internal> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_392_684238417.1711622408353" ------=_Part_392_684238417.1711622408353 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
As a convention-oriented deployment tool, Deployment Manager aut= omatically takes care of replacing appSettings and connectionStrings, runni= ng XML config transforms, and updating IIS websites. Sometimes however you'= ll need to do more than this - and that's where PowerShell scripts come in.=
PowerShell is a powerful scripting language which can be used to customi= ze the behavior of your package deployments. For useful examples of scripts= which can be customized to suit your project, take a look at our PowerShell scripts forum.
At the root of your NuGet package, you can add any of the following file= s:
After extracting your package, the Agent will detect these scripts and i= nvoke them in a custom PowerShell host on the target machine.
If your package contains multiple PreDeploy.ps1, Deploy= .ps1, or PostDeploy.ps1 scripts, they = will all be run during deployment.
After deployment is complete, any extracted PowerShell scripts will be d= eleted from the target machine.
Deployment Manager allows you to define variables to parameterize your d= eployments. These variables, along with some predefined variables, will be = available from within your PowerShell script. For example, a variable named= MyApp.ConnectionString will be available as either:
$MyAppConnectionString
$DeploymentManagerParameters["MyApp.ConnectionString"]
The first form is not case-sensitive, while the second form is case-sens= itive. Also, in the first form, non-alphanumeric characters are removed, wh= ile in the second they appear just as they appear in the Deployment Manager= web interface.
When invoking a command in your PowerShell scripts, you'll need to pipe = output to the Write-Host command. For example, this script will not show an= y output in the Deployment Manager deployment log:
Get-ChildItem
While this script will show output:
Get-ChildItem | Write-Host
The same goes for invoking an external executable. For example:
& "MyProgram.exe" "-arg1" "-arg2" | Write-Host
This script uses the Service Control tool (sc.exe) to create or= update a Windows Service. It assumes some variables have been set in the D= eployment Manager web interface:
#= These variables should be set via the Deployment Manager web interface: # # ServiceName - Name of the Windows service # ServiceExecutable - Path to the .exe containing the service # # sc.exe is the Service Control utility in Windows $service =3D Get-Service $ServiceName -ErrorAction SilentlyContinue $fullPath =3D Resolve-Path $ServiceExecutable if (! $service) { Write-Host "The service will be installed" New-Service -Name $ServiceName -BinaryPathName $fullPath -StartupType Autom= atic } else { Write-Host "The service will be stopped and reconfigured" Stop-Service $ServiceName -Force & "sc.exe" config $service.Name binPath=3D $fullPath start=3D auto | Wr= ite-Host } Write-Host "Starting the service" Start-Service $ServiceName
This script uses InstallUtil.exe to install custom Ev= ent Log sources and Windows Services:
$= NetFrameworkDirectory =3D $(System.Runtime.InteropServices.RuntimeEnvironme= nt::GetRuntimeDirectory()) & (Join-Path $NetFrameworkDirectory "InstallUtil.exe") "path\to\my.dll"= | Write-Host
One of the first commands performed by Agent's custom PowerShell host is= to automatically set the execution policy to bypass. This means that Power= Shell's inbuilt security measures, such as blocking execution of external s= cripts, will not apply. This also means there is no need to change the defa= ult system-wide execution policy when using Agent for deployment.
Also, keep in mind that scripts are executed in the context of the accou= nt that the Agent Windows Service runs as. By default this is Local System,= which has extensive local privileges, but usually cannot access file share= s, remote SQL databases, or other external resources. If you need wider per= missions, you'll need to configure Agent to run under a custom service acco= unt.