IIS: how to undeploy/delete/remove a webapp from command line?

前端 未结 4 1672
暗喜
暗喜 2021-02-05 11:22

Suppose there\'s a webapp deployed on local IIS server. When I need to remove/undeploy it, I can go to IIS Manager, right-click on the app, and then select \"Delete application

4条回答
  •  别那么骄傲
    2021-02-05 11:53

    I know the question says "command line", but you can use PowerShell and the IIS Administration Cmdlets to do this task. I provide all of the functions and explain the process of how to automate this on my blog. Also, you can easily swap out the IIS Administration Cmdlet calls with calls to msdeploy, appcmd, IIsVdir.vbs, etc.

    For your specific question, this PowerShell code should do the trick:

    $block = {
        Import-Module WebAdministration
        $website = "YourWebsiteName"
        $applicationName = "PathUnderWebsite\ToYourApplication"
    
        $fullPath = Join-Path $website $applicationName
        Write-Host "Checking if we need to remove '$fullPath'..."
        if (Get-WebApplication -Site "$website" -Name "$applicationName")
        {
            Write-Host "Removing '$fullPath'..."
            Remove-WebApplication -Site "$website" -Name "$applicationName"
        }
    
        Write-Host "Deleting the directory '$fullPath'..."
        Remove-Item -Path "IIS:\Sites\$fullPath" -Recurse -Force
    }
    $session = New-PSSession -ComputerName "Your.WebServer.HostName"
    Invoke-Command -Session $session -ScriptBlock $block
    Remove-PSSession -Session $session
    

提交回复
热议问题