如何在代码部署到 EC2 实例后自动启动(或创建和启动)windows 服务
How to automatically start (or create and start) windows service after codedeploy to an EC2 instance
我正在尝试创建一个可以通过 AWS-CodeDeploy 自动部署并在 EC2 实例上启动 windows 服务的管道,但我无法正确设置 powershell 脚本。
我有以下内容:
Appspec.yml
version: 0.0
os: windows
files:
- source: \
destination: C:\temp\MyApp
hooks:
ApplicationStop:
- location: DeploymentScripts\applicationStop.bat
timeout: 180
ApplicationStart:
- location: DeploymentScripts\applicationStart.bat
timeout: 180
applicationStart.bat
c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\temp\MyApp\DeploymentScripts\service_create_start.ps1 -ServiceName MyService -BinaryPath 'C:\temp\MyApp\MyService.exe' -DisplayName 'Test Application' -Description 'This is a test'
以及以下 powershell service_create_start.ps1:
Param([Parameter(Mandatory=$true)][string]$ServiceName,[Parameter(Mandatory=$true)][ValidateScript({Test-Path $_ -PathType 'leaf'})][string]$BinaryPath, [string]$Displayname, [string]$Description)
If (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
If ((Get-Service $ServiceName).Status -eq 'Running') {
Stop-Service $ServiceName
Write-Host "Stopping $ServiceName"
} Else {
Write-Host "$ServiceName found, but it is not running."
}
} Else {
Write-Host "$ServiceName not found. Creating new Windows service."
}
$ArgumentList = '-Name "{0}" -BinaryPathName "{1} -k netsvcs" -DisplayName "{2}" -StartupType Automatic -Description "{3}"' -f $ServiceName, $BinaryPath, $DisplayName, $Description
New-Service $ArgumentList
Start-Service $ServiceName
我创建了 bat 文件,因为我似乎无法从带参数的代码部署启动 powershell 脚本。我与它无关。
我的第一个问题是它挂起 "BinaryPathName" 的提示我感觉这是我在其中一个文件中的引号的问题。
我的第二个问题(如果我在测试中手动输入)是我在 New-Service 行得到一个 Access is denied 异常。
为了解决这个问题,我尝试将 Start-Process 和 RunAs 动词用于 运行 提升的 powershell,但是我无法弄清楚将我的参数添加到其中的语法(超出 ps1 文件名) 使用此语法。
我这里的路径是否正确,或者有更好的方法吗?
我认为你不能像这样传入参数(作为单个字符串),你最好使用 splatting:
$myInput = @{
Name = $ServiceName
BinaryPathName = '{0} -k netsvc' -f $BinaryPath
StartupType = 'automatic'
DisplayName = $DisplayName
Description = $Description
}
New-Service @myInput
我正在尝试创建一个可以通过 AWS-CodeDeploy 自动部署并在 EC2 实例上启动 windows 服务的管道,但我无法正确设置 powershell 脚本。
我有以下内容:
Appspec.yml
version: 0.0
os: windows
files:
- source: \
destination: C:\temp\MyApp
hooks:
ApplicationStop:
- location: DeploymentScripts\applicationStop.bat
timeout: 180
ApplicationStart:
- location: DeploymentScripts\applicationStart.bat
timeout: 180
applicationStart.bat
c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\temp\MyApp\DeploymentScripts\service_create_start.ps1 -ServiceName MyService -BinaryPath 'C:\temp\MyApp\MyService.exe' -DisplayName 'Test Application' -Description 'This is a test'
以及以下 powershell service_create_start.ps1:
Param([Parameter(Mandatory=$true)][string]$ServiceName,[Parameter(Mandatory=$true)][ValidateScript({Test-Path $_ -PathType 'leaf'})][string]$BinaryPath, [string]$Displayname, [string]$Description)
If (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
If ((Get-Service $ServiceName).Status -eq 'Running') {
Stop-Service $ServiceName
Write-Host "Stopping $ServiceName"
} Else {
Write-Host "$ServiceName found, but it is not running."
}
} Else {
Write-Host "$ServiceName not found. Creating new Windows service."
}
$ArgumentList = '-Name "{0}" -BinaryPathName "{1} -k netsvcs" -DisplayName "{2}" -StartupType Automatic -Description "{3}"' -f $ServiceName, $BinaryPath, $DisplayName, $Description
New-Service $ArgumentList
Start-Service $ServiceName
我创建了 bat 文件,因为我似乎无法从带参数的代码部署启动 powershell 脚本。我与它无关。
我的第一个问题是它挂起 "BinaryPathName" 的提示我感觉这是我在其中一个文件中的引号的问题。
我的第二个问题(如果我在测试中手动输入)是我在 New-Service 行得到一个 Access is denied 异常。
为了解决这个问题,我尝试将 Start-Process 和 RunAs 动词用于 运行 提升的 powershell,但是我无法弄清楚将我的参数添加到其中的语法(超出 ps1 文件名) 使用此语法。
我这里的路径是否正确,或者有更好的方法吗?
我认为你不能像这样传入参数(作为单个字符串),你最好使用 splatting:
$myInput = @{
Name = $ServiceName
BinaryPathName = '{0} -k netsvc' -f $BinaryPath
StartupType = 'automatic'
DisplayName = $DisplayName
Description = $Description
}
New-Service @myInput