Home
%3CLINGO-SUB%20id%3D%22lingo-sub-392598%22%20slang%3D%22en-US%22%3ETerminate%20w3wp.exe%20process%20without%20stopping%20website%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-392598%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Jun%2020%2C%202018%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3EHere%20is%20a%20quick%20way%20to%20terminate%20w3wp.exe%20process%20in%20Azure%20App%20Service%20Website%20without%20stopping%20the%20whole%20website.%3C%2FP%3E%0A%20%20%3CP%3E%3CSTRONG%3ENote%20%3C%2FSTRONG%3E%20%3A%20Azure%20AppService%20website%20can%20run%20on%20multiple%20instances%20and%20there%20is%20another%20w3wp.exe%20process%20called%20SCM%20running%20on%20each%20instance%20(we%20shouldn%E2%80%99t%20kill%20this%20process).%20So%20we%20just%20have%20to%20get%20list%20of%20all%20instances%20running%20the%20website%20using%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fazurerm.resources%2FGet-AzureRmResource%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Get-AzureRMResource%20%3C%2FA%3E%20cmdlet.%20On%20each%20instance%2C%20we%20need%20to%20get%20list%20of%20processes%20running%20using%20same%20Get-AzureRMResource%20cmdlet.%20Next%2C%20we%20need%20to%20check%20if%20this%20process%20is%20w3wp.exe%20process%20and%20see%20if%20the%20SCM%20property%20is%20true.%20Last%20step%20is%20using%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fazurerm.resources%2Fremove-azurermresource%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Remove-AzureRMResource%20%3C%2FA%3E%20to%20terminate%20the%20process.%3C%2FP%3E%0A%20%20%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fresources.azure.com%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Resource%20Explorer%20%3C%2FA%3E%20is%20the%20best%20tool%20that%20can%20help%20us%20to%20pick%20the%20right%20PowerShell%20cmdlet%20to%20use%20and%20what%20parameters%20to%20pass.%20Just%20click%20on%20PowerShell%20tab%20in%20the%20resource%20explorer%20as%20shown%20below.%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F101204i98F62978FDEC2D0B%22%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3E%3CBR%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22powershell%22%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Login-AzureRmAccount%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24resoureGroupName%20%3D%20%22wabac%22%20%3CBR%20%2F%3E%20%24websiteName%20%3D%20%22wabacblue%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24instances%20%3D%20Get-AzureRmResource%20-ResourceGroupName%20%24resoureGroupName%20%60%20%3CBR%20%2F%3E%20-ResourceType%20Microsoft.Web%2Fsites%2Finstances%20%60%20%3CBR%20%2F%3E%20-ResourceName%20%24websiteName%20%60%20%3CBR%20%2F%3E%20-ApiVersion%202018-02-01%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20foreach(%24instance%20in%20%24instances)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%24instanceName%20%3D%20%24instance.Name%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Write-Host%20%22Site%20Instance%20Name%20%3A%20%22%20%24instanceName%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24processes%20%3D%20Get-AzureRmResource%20-ResourceGroupName%20%24resoureGroupName%20%60%20%3CBR%20%2F%3E%20-ResourceType%20Microsoft.Web%2Fsites%2Finstances%2Fprocesses%20%60%20%3CBR%20%2F%3E%20-ResourceName%20%24websiteName%2F%24instanceName%20%60%20%3CBR%20%2F%3E%20-ApiVersion%202018-02-01%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20foreach(%24process%20in%20%24processes)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%24exeName%20%3D%20%24process.Properties.Name%20%3CBR%20%2F%3E%20Write-Host%20%22%60tEXE%20Name%20%3A%20%22%20%24exeName%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20if(%24exeName%20-eq%20%22w3wp%22)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%24processId%20%3D%20%24process.Properties.id%20%3CBR%20%2F%3E%20Write-Host%20%22%60t%60tProcess%20ID%20%3A%20%22%20%24processId%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24processDetails%20%3D%20Get-AzureRmResource%20-ResourceGroupName%20%24resoureGroupName%20%60%20%3CBR%20%2F%3E%20-ResourceType%20Microsoft.Web%2Fsites%2Finstances%2Fprocesses%20%60%20%3CBR%20%2F%3E%20-ResourceName%20%24websiteName%2F%24instanceName%2F%24processId%20%60%20%3CBR%20%2F%3E%20-ApiVersion%202018-02-01%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Write-Host%20%22%60t%60tSCM%20Value%20%3A%20%22%20%24processDetails.Properties.is_scm_site%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20if(%24processDetails.Properties.is_scm_site%20-ne%20%24true)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20Write-Host%20%22%60t%60t%60tNot%20a%20SCM%20Process%20%3A%20%22%20%24process.Properties.Name%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24deleted%20%3D%20Remove-AzureRmResource%20-ResourceGroupName%20%24resoureGroupName%20%60%20%3CBR%20%2F%3E%20-ResourceType%20Microsoft.Web%2Fsites%2Finstances%2Fprocesses%20%60%20%3CBR%20%2F%3E%20-ResourceName%20%24websiteName%2F%24instanceName%2F%24processId%20%60%20%3CBR%20%2F%3E%20-ApiVersion%202018-02-01%20-Force%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20if(%24deleted%20-eq%20%24true)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20Write-Host%20%22%60t%60t%60t%60tW3wp%20process%20killed%22%20-ForegroundColor%20Green%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20else%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20Write-Host%20%22%60t%60t%60t%60tFailed%20to%20kill%20W3wp%20process%22%20-ForegroundColor%20Red%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-392598%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Jun%2020%2C%202018%20Here%20is%20a%20quick%20way%20to%20terminate%20w3wp.%3C%2FLINGO-TEASER%3E
Microsoft
First published on MSDN on Jun 20, 2018


Here is a quick way to terminate w3wp.exe process in Azure App Service Website without stopping the whole website.

Note : Azure AppService website can run on multiple instances and there is another w3wp.exe process called SCM running on each instance (we shouldn’t kill this process). So we just have to get list of all instances running the website using Get-AzureRMResource cmdlet. On each instance, we need to get list of processes running using same Get-AzureRMResource cmdlet. Next, we need to check if this process is w3wp.exe process and see if the SCM property is true. Last step is using Remove-AzureRMResource to terminate the process.

Azure Resource Explorer is the best tool that can help us to pick the right PowerShell cmdlet to use and what parameters to pass. Just click on PowerShell tab in the resource explorer as shown below.






[code language="powershell"]

Login-AzureRmAccount

$resoureGroupName = "wabac"
$websiteName = "wabacblue"


$instances = Get-AzureRmResource -ResourceGroupName $resoureGroupName `
-ResourceType Microsoft.Web/sites/instances `
-ResourceName $websiteName `
-ApiVersion 2018-02-01


foreach($instance in $instances)
{
$instanceName = $instance.Name

Write-Host "Site Instance Name : " $instanceName

$processes = Get-AzureRmResource -ResourceGroupName $resoureGroupName `
-ResourceType Microsoft.Web/sites/instances/processes `
-ResourceName $websiteName/$instanceName `
-ApiVersion 2018-02-01

foreach($process in $processes)
{
$exeName = $process.Properties.Name
Write-Host "`tEXE Name : " $exeName

if($exeName -eq "w3wp")
{
$processId = $process.Properties.id
Write-Host "`t`tProcess ID : " $processId

$processDetails = Get-AzureRmResource -ResourceGroupName $resoureGroupName `
-ResourceType Microsoft.Web/sites/instances/processes `
-ResourceName $websiteName/$instanceName/$processId `
-ApiVersion 2018-02-01

Write-Host "`t`tSCM Value : " $processDetails.Properties.is_scm_site

if($processDetails.Properties.is_scm_site -ne $true)
{
Write-Host "`t`t`tNot a SCM Process : " $process.Properties.Name

$deleted = Remove-AzureRmResource -ResourceGroupName $resoureGroupName `
-ResourceType Microsoft.Web/sites/instances/processes `
-ResourceName $websiteName/$instanceName/$processId `
-ApiVersion 2018-02-01 -Force

if($deleted -eq $true)
{
Write-Host "`t`t`t`tW3wp process killed" -ForegroundColor Green
}
else
{
Write-Host "`t`t`t`tFailed to kill W3wp process" -ForegroundColor Red
}
}
}
}
}




[/code]