Search code examples
windowspowershellaws-code-deploy

Powershell script throws error (80040154 Class not registered) only when it is executed from codedeploy, Why?


I have windows instance that is running CodeDeploy Agent. I also have a powershell script that creates site in IIS:

# Define parameters
$siteName = "test1"
$physicalPath = "C:\connect\MicroServices\authentication"
$applicationPool = "testPool"
$dnsName = "devteam5.localhost"
# Check if the application pool exists
$AppPoolExists = Get-WebAppPoolState -Name $applicationPool -ErrorAction SilentlyContinue
if (-not $AppPoolExists) {
    Write-Host "Application Pool '$applicationPool' does not exist. Creating..."
    New-WebAppPool -Name $applicationPool
    Write-Host "Application Pool '$applicationPool' created successfully."
}
# Check if the site already exists
$siteExists = Get-Website -Name $siteName -ErrorAction SilentlyContinue
if ($siteExists) {
    Write-Host "The website '$siteName' already exists in IIS. Script stopped."
    exit
}
# If site doesn't exist, create it with the DNS binding
New-WebSite -Name $siteName -PhysicalPath $physicalPath -ApplicationPool $applicationPool -HostHeader $dnsName -Port 80 -Force
Write-Host "Website '$siteName' created successfully in IIS and bound to '$dnsName'."

When I run it manually, this powershell executes without any error. But when I execute the script with codedeploy it throws error. My appspec.yml looks like:

version: 0.0
os: windows
files:
  - source: /
    destination: C:\connect\MicroServices\authentication
    overwrite: true
file_exists_behavior: OVERWRITE
hooks:
  AfterInstall:
    - location: automation-script1.ps1
      timeout: 3000
      runas: Administrator

It throws this error:

get-webitemstate : Retrieving the COM class factory for component with CLSID failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 2024-04-19 11:09:16 stderr)

Manually running script works fine, but throws error when it runs with codedeploy. Kindly let me know why this behavior?


Solution

  • After days of troubleshooting I have found the solution and the actual problem. So when we deploy run powershell script through codedeploy (appspec.yml) then it will execute our powershell script in 32 bit mode. In my case, I need 64 bit mode of the powershell script that's why it was running manually but not with codedeploy. If anyone want to execute the 64 bit mode powershell script with Codedeploy then just add the following code at the start of your powershell script which will initiate your script in 64 bit mode:

    # 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...
    # ...
    

    It will resolve your problem. I think it will help someone facing same error.

    Reference https://docs.aws.amazon.com/codedeploy/latest/userguide/troubleshooting-deployments.html