IIS website status-powershell c#

Posted on 2011-10-08
Last Modified: 2012-08-03
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#
Question by:wademi
    LVL 16

    Accepted Solution

    Here is an example that includes pulling back the State value of a Site using PowerShell:

    How To: Use Microsoft.Web.Administration from Powershell
    LVL 4

    Assisted Solution

    You can use get-website and get-websitestatus from the WebAdministration snappin.
    LVL 5

    Expert Comment

    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 = ''
    $eMailTo = '' 
    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}
    		{start-website $
                		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 '' -subject "Default Web Site has been Started" `
                        	-body "The Default Web Site was not running.  It was started on $datetime."}
    			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 `
    			'' -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

    Open in new window


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    In this previous article (, we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
    "Migrate" an SMTP relay receive connector to a new server using info from an old server.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now