user2944173
user2944173

Reputation: 65

Powershell script to get status of web site whether it is running or stopped in IIS manager

I have created a script to get the website status is running or stopped from IIS manager.

But i coudln't get exact output.

Question: just i need to display website status is running or stopped in IIS using scripts.

$serverList = 'SSCCL35'
foreach ($server in $serverList) {
$iis = Get-WmiObject Win32_Service -Filter "Name = 'sample'" -ComputerName $server
if ($iis.State -eq 'Running') { Write-Host "IIS is running on $server" }
Else { Write-Host "IIS is NOT running on $server" -ForegroundColor Red }
}

Note: "Sample" Service is running in my IIS. But i get output as "Not running".

Upvotes: 1

Views: 10175

Answers (1)

scrthq
scrthq

Reputation: 1076

The current example shows you looking for a service named "sample", not a site named "sample", which is why it's always stating that it's not running.

From the local IIS host, you could find it using AppCmd:

Invoke-Expression "$env:SystemRoot\system32\inetsrv\AppCmd.exe list site 'sample'"

OR you could use the WebAdministration module to go pure Powershell (if it's available on the IIS host):

Import-Module WebAdministration; Get-WebsiteState -Name "sample"

If you want to run the command on remote servers, you'll need to use Invoke-Command and add one of those lines to the ScriptBlock:

Invoke-Command -ComputerName $server -ScriptBlock {enter one of the above lines of code here}

Upvotes: 2

Related Questions