Home > OS >  How to stop a IIS Website completely from powershell?
How to stop a IIS Website completely from powershell?

Time:06-13

I'm trying to write a backup plan with powershell that archives the website root directory and send it to sftp server the problem is that even when i stop the website with either of these two commands

Stop-WebSite "website"
Stop-IISSite -Name "website"

as the compression wants to start it throws a error as following

ZipArchiveHelper : The process cannot access the file 'C:\inetpub...\www\AutoMapper.dll' because it is being used by another process. At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:697 char:30

When i stop the website from IIS manager the code works fine so is there any way to stop a website completely from powershell?

CodePudding user response:

You can try to stop the App Pool of the Website. So it will not load process.

Step will be, 1.Stop IIS Website. 2.Stop IIS Website App Pool. 3.Copy Files

CodePudding user response:

The usual way is this:

In recent version of Windows (10, 2016, and later), you need to import IISAdministration instead of WebAdministration.

Import-Module IISAdministration
Stop-WebSite 'Default Web Site'
Start-WebSite 'Default Web Site'

For older version of Windows and IIS, you need to do.

Import-Module WebAdministration
Stop-WebSite 'Default Web Site'
Start-WebSite 'Default Web Site'

you can also perform this remotely using Invoke-Command.

Import-Module WebAdministration
$siteName = "Default Web Site"
$serverName = "name"
$block = {Stop-WebSite $args[0]; Start-WebSite $args[0]};  
$session = New-PSSession -ComputerName $serverName
Invoke-Command -Session $session -ScriptBlock $block -ArgumentList $siteName 
  • Related