简体   繁体   中英

How to stop a IIS Website completely from powershell?

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 an error as following

ZipArchiveHelper: The process cannot access the file 'C:\.netpub...\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?

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 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM