我有一个应用程序,使用AWS Codedeploy将其部署到运行Windows Server 2012 R2的EC2实例并安装了代码部署。 Codedeploy使用推荐的CloudFormation模板安装到实例上,并且一直运行良好。
我已经创build了一个简单的脚本,它应该在默认网站的IIS中创build一个文件系统上的一个文件夹和一个虚拟目录,脚本如下:
# Are you running in 32-bit mode? # (\SysWOW64\ = 32-bit mode) if ($PSHOME -like "*SysWOW64*") { Write-Warning "Restarting this script under 64-bit Windows PowerShell." # Restart this script under 64-bit Windows PowerShell. # (\SysNative\ redirects to \System32\ for 64-bit mode) & (Join-Path ($PSHOME -replace "SysWOW64", "SysNative") powershell.exe) -File ` (Join-Path $PSScriptRoot $MyInvocation.MyCommand) @args # Exit 32-bit script. Exit $LastExitCode } # Was restart successful? Write-Warning "Hello from $PSHOME" Write-Warning " (\SysWOW64\ = 32-bit mode, \System32\ = 64-bit mode)" Write-Warning "Original arguments (if any): $args" # Your 64-bit script code follows here... # Install IIS Web Server administrator Import-Module -Name ServerManager Import-Module WebAdministration Install-WindowsFeature Web-Server $folderLoc = 'C:\inetpub\imageprocessorcache' if (-not (Test-Path $folderLoc)) { # Create new folder New-Item $folderLoc -type directory # Get rule and add new rule to it $existingAcl = Get-Acl $folderLoc $permissions = 'Users', 'FullControl', 'ContainerInherit, ObjectInherit', 'None', 'Allow' $rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $permissions $existingAcl.AddAccessRule($rule) # Apply new rule to the folder $existingAcl | Set-Acl -Path $folderLoc # Get the ACL for the folder for output purposes Get-Acl -Path $folderLoc } $virtualPathLocation = 'IIS:\Sites\Default Web Site\imageprocessorcache' # Check if the virtual directory exists in IIS if (-not (Test-Path $virtualPathLocation)) { # Create it because it doesn't exist yet New-WebVirtualDirectory -Name 'imageprocessorcache' -PhysicalPath $folderLoc -Site 'Default Web Site' }
这是我有的appspec文件:
version: 0.0 os: windows files: - source: MyWebsite destination: c:\temp\CodeDeployFiles hooks: BeforeInstall: - location: \Deployment Scripts\IISCreateImageProcessorVirtualDirectory.ps1 timeout: 900
出于某种原因,脚本不作为部署过程的一部分执行,看起来好像被跳过或者只是悄然失败。
我能够远程桌面,导航到EC2实例上的临时文件夹(CodeDeploy在部署过程中将文件复制到的文件夹),并右键单击IISCreateImageProcessorVirtualDirectory.ps1并在复制的脚本上select“使用Powershell运行”。 这工作得很好,文件夹和虚拟目录被创build。
我已经成功运行.ps1脚本,使用我当前的安装程序进行代码部署,但是这个脚本不起作用,或者留下任何错误消息。
什么可能导致脚本不运行?
谢谢你的帮助。
[2016-11-24 21:10:54.909] [d-DSQ9EQ28J]Script - .\Powershell Scripts\IISCreateImageProcessorVirtualDirectory.ps1 [2016-11-24 21:10:55.112] [d-DSQ9EQ28J][stderr]Processing -File 'C:\ProgramData/Amazon/CodeDeploy/c979dfe5-9d99-4cee-870d-cc9e3cb518bc/d-DSQ9EQ28J/deployment-archive/.\Powershell' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again.
CodeDeploy代理如何执行脚本( 源代码 ):
powershell.exe -ExecutionPolicy Bypass -File <absolute_path_to_your_script_here>
所以请尝试改变你的钩子:
hooks: BeforeInstall: - location: .\Deployment Scripts\IISCreateImageProcessorVirtualDirectory.ps1 timeout: 900
运算符.\ 允许您在当前目录中运行脚本 。
常规故障排除
如果您需要进一步排除故障,可以在这里find最近的部署: C:\ProgramData\Amazon\CodeDeploy\
并且您的日志每个部署在这里: C:\ProgramData\Amazon\CodeDeploy\DEPLOYMENTGROUPIDGOESHERE\DEPLOYMENTIDGOESHERE\logs
您可以在脚本中写入主机或stdout / stderr,如果正在运行,那么输出将在这些日志中结束。
我终于让我的Powershell脚本再次运行CodeDeploy。 似乎我给自己的问题,把ps1脚本放入我的部署包中的一个文件夹,而不是我的appspec.yml文件的根目录。
我认为这可能是一个消息,当我阅读powershell.exe的帮助文件执行powershell.exe /? :
-文件
运行本地作用域中的指定脚本(“点源”),以便脚本创build的函数和variables在当前会话中可用。 input脚本文件path和任何参数。 文件必须是命令中的最后一个参数,因为在File参数名称后键入的所有字符都被解释为脚本文件path,后面跟着脚本参数。