microsoft / IIS.ServiceMonitor

An entrypoint process for running IIS in Windows containers
MIT License
126 stars 42 forks source link

Empty environment variables causes APPCMD to fail #73

Open gamingrobot opened 3 years ago

gamingrobot commented 3 years ago

Azure DevOps sets a few empty environment variables. ServiceMonitor then converts these to an APPCMD command and then APPCMD fails with a configuration error.

ServiceMonitor Output:

Stopping service 'w3svc'

 Service 'w3svc' has been stopped 

APPCMD failed with error code 13

Example environment variables:

PSPath        : Microsoft.PowerShell.Core\Environment::INPUT_ARGUMENTS
PSDrive       : Env
PSProvider    : Microsoft.PowerShell.Core\Environment
PSIsContainer : False
Key           : INPUT_ARGUMENTS
Value         : 
Name          : INPUT_ARGUMENTS

PSPath        : Microsoft.PowerShell.Core\Environment::RESOURCES_TRIGGERINGALIAS
PSDrive       : Env
PSProvider    : Microsoft.PowerShell.Core\Environment
PSIsContainer : False
Key           : RESOURCES_TRIGGERINGALIAS
Value         : 
Name          : RESOURCES_TRIGGERINGALIAS

PSPath        : Microsoft.PowerShell.Core\Environment::RESOURCES_TRIGGERINGCATEGORY
PSDrive       : Env
PSProvider    : Microsoft.PowerShell.Core\Environment
PSIsContainer : False
Key           : RESOURCES_TRIGGERINGCATEGORY
Value         : 
Name          : RESOURCES_TRIGGERINGCATEGORY

Example Azure DevOps yaml:

- powershell: |
    Invoke-WebRequest -UseBasicParsing -Uri "https://dotnetbinaries.blob.core.windows.net/servicemonitor/2.0.1.10/ServiceMonitor.exe" -OutFile "C:\ServiceMonitor.exe"
  displayName: Install Service Monitor
- powershell: |
    & C:\ServiceMonitor.exe w3svc
  displayName: Start IIS

Currently we are running this as a workaround before starting ServiceMonitor

Get-ChildItem -Path env: | Where-Object Value -eq "" | ForEach-Object { Set-Item -Path "Env:$($_.Name)" -Value "" }
Silvenga commented 3 years ago

It should be noted that removing said empty environment variables as above did allow appcmd to successfully execute.

Under Windows, setting an environment variable to empty string, in either PowerShell or CMD will remove said environment variable (a DOS carryover?). Creating an empty environment variable is incredibly difficult to do in Windows, and many tools downright prevent it (e.g. Docker). This may be a Azure DevOps bug, but it's incredibly difficult to get bugs fixed there, soo...