Reputation: 3108
I have multiple sites configured in IIS7 on my Windows 7 development machine to run on the same port and usually only run one at a time depending on what I'm working on. I would like to be able to start and stop my development sites from PowerShell instead of having the IIS manager opened. Does anyone have a good resource to point me in the right direction or a script that already accomplishes this?
Upvotes: 75
Views: 62288
Reputation: 1178
In my case, I had several machines running the same website as part of an IIS web farm. I needed a script that would take a list of machines, and stop or start the website on each machine. All the machines use a shared file repository and the files are locked by the respective website when running. I have to stop all the sites to make file updates. (website not of my own design... it's part of an ERP application.)
Here's my take on this automated stop/start script:
#uncomment the relevant action stop or start.
$action = "start"
#$action = "stop"
#Enter the name of your website as seen in IIS.
$website = "Website Name"
#Create an array of server names (FQDN or just the AD host name)
$serverList = @(
"server1.domain.com",
"server2.domain.com",
"server3.domain.com"
)
#Loop through the server name array and perform the action.
ForEach($server in $serverList) {
#For debugging/status purposes, show the action/website/server.
$msg = $action + "ing " + $website + " on server " + $server
Write-Output $msg
#Invoke a command on the remote server.
Invoke-Command -ComputerName $server -ScriptBlock {
$sm = Get-IISServerManager
if ($action -eq "stop") {
$sm.Sites[$website].Stop()
} elseif ($action -eq "start") {
$sm.Sites[$website].Start()
}
$sm.Dispose();
}
}
Script assumes that
Enable-PSRemoting
)Import-Module WebAdministration
on local computer running script.Upvotes: 0
Reputation: 30177
You can use the newer IISAdministration
module with the Start-IISSite
cmdlet like this:
Import-Module IISAdministration
Start-IISSite -Name "Default Web Site"
Or by using Get-IISSite
, you can then start / stop like this:
Import-Module IISAdministration
$site = Get-IISSite "Default Web Site"
$site.Start()
$site.Stop()
Upvotes: 2
Reputation: 31
I found that the following to stop individual websites on a remote server to work:
Invoke-Command -Computername $servername -Scriptblock {
(Import-Module WebAdministration);
Stop-Website -Name "WebsiteName";
Stop-Website -Name "AnotherWebsiteName"
}
I had some of the errors above until Import-Module
was put in ()
Upvotes: 3
Reputation: 353
Adding to Keith's answer, you can 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
Upvotes: 22
Reputation: 31
To get access to system modules, Powershell needs to be run like this:
[path]\powershell.exe -NoExit -ImportSystemModules
I found the above on this iis forum.
Upvotes: 3
Reputation: 202012
Just for future quick reference, the commands are:
Import-Module WebAdministration
Stop-WebSite 'Default Web Site'
Start-WebSite 'Default Web Site'
Upvotes: 116