PowerShell Script that stops Local Web Server in case if its running

When you run the up.ps1 to docker-compose up your Sitecore instance, in case if IIS is running, Traefik doesn't like the same and throws the following error:

ERROR: for basic-company-tds_traefik_1  Cannot start service traefik: failed to create endpoint basic-company-tds_traefik_1 on network basic-company-tds_default: failed during hnsCallRawResponse: hnsCall failed in Win32: The process cannot access the file because it is being used by another process. (0x20)

ERROR: for traefik  Cannot start service traefik: failed to create endpoint basic-company-tds_traefik_1 on network basic-company-tds_default: failed during hnsCallRawResponse: hnsCall failed in Win32: The process cannot access the file because it is being used by another process. (0x20)

ERROR: Encountered errors while bringing up the project.


I didn't like seeing this error every time I restarted my machine since I would often forget stopping IIS  before running up.ps1. So, I decided to accommodate the script for stopping IIS within the up.ps1. The script is just as simple as checking if w3svc is running and if it is, stop the service. As simple as this:

# Stop IIS if running in this computer

Write-Host "Checking IIS Status..." -ForegroundColor Green

$service=get-service w3svc  -ErrorAction SilentlyContinue

if ($null -ne $service.Name) {

if ($service.Status -eq 'Running') {

Write-Host "IIS running. Attempting to Stop...." -ForegroundColor Green

$svcStatus=Stop-Service w3svc -Force

if($svcStatus.Status -eq 'Stopped') {

Write-Host "IIS Stopped Successfully...." -ForegroundColor Green   

}

}

}

Just stick it to the correct location in the up.ps1 script:


Now, retry the up.ps1 with IIS started and running , this script must take care of the rest -



There is obviously scope for some fine tuning with regard to suppressing the warning message or getting the output and then showing the success status message.


Comments