• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1543
  • Last Modified:

IIS website status-powershell c#

I need to create a program that checks  a website/webapplication  and return the status if its started or stop.
I am running IIS on my webserver.

Can somone start me off with doing this in powershell or C#
0
wademi
Asked:
wademi
2 Solutions
 
jessc7Commented:
Here is an example that includes pulling back the State value of a Site using PowerShell:

How To: Use Microsoft.Web.Administration from Powershell http://blogs.iis.net/ma_khan/archive/2010/09/12/how-to-use-microsoft-web-administration-from-powershell.aspx
0
 
Ryan WeaverInfrastructure ManagerCommented:
You can use get-website and get-websitestatus from the WebAdministration snappin.
http://technet.microsoft.com/en-us/library/ee790599.aspx
0
 
daveTechSearchCommented:
If you are using IIS 7 or 7.5...
I am using something like this as a scheduled task to check a few of our sites... you may need to modify for your systems (just modify for your site(s).
# Function to check Default Web Site status and start it if not running   
$mailServer = 'mail.domain.com'
$eMailTo = 'email@domain.com' 
function check-webSite
	{
	$datetime = get-date
	# Import the Web Administration module for PowerShell
	import-module webadministration
	# Create variable for Default Web Site
	$defaultSite = get-item "IIS:\sites\Default Web Site"

        if($defaultSite.state -eq "started")
		{write-output "$datetime --  Default Web Site check OK. Default Web Site is Running." |
			out-file -append D:\PSOutput\check-defaultSite.log}

		else

		{start-website $defaultSite.name
            		if($? -eq "True"){
			write-output "$datetime --  Default Web Site has been started" | 
			out-file -append D:\PSOutput\check-defaultSite.log
			# Send notification email
                	send-mailmessage  -smtpserver $mailServer -to $eMailTo `
			-from 'server@domain.com' -subject "Default Web Site has been Started" `
                    	-body "The Default Web Site was not running.  It was started on $datetime."}
			
			else

			{
			write-output "$datetime --  The Default Web Site could not be started" |
			out-file -append D:\PSOutput\check-defaultSite.log
			write-output "$datetime --  Sending alert email" | out-file -append D:\PSOutput\check-defaultSite.log
			send-mailmessage  -smtpserver $mailServer -to $eMailTo -from `
			'server@domain.com' -subject "Default Web Site could not be started" `
			-body "Default Web Site could not be started.`r`fPlease log on and start the Default Web Site."
			# Exit Script - Default Web Site could not be started
			write-output "$datetime --  Exiting script as the Default Web Site could not be started" | 
			out-file -append D:\PSOutput\check-defaultSite.log
			Exit
			}
		}
	}

check-webSite

Open in new window

0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now