Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a website is installed in IIS7 with Powershell?

I'm pretty new to powershell, and I'm trying to automate the removal of a prior version of a website and addition of the newer version as a part of a TFS 2010 Build Template (Windows Workflow 4.0). Is it possible to see if a website or web app pool exists in IIS7 with powershell? I've tried running the following command:

import-module WebAdministration
Get-Website -Name "Default Web Site"

The output lists all of the websites installed on the box, not just the default web site.

Name             ID   State      Physical Path                  Bindings
-------------------------------------------------------------------------
Default Web Site 1    Started    %SystemDrive%\inetpub\wwwroot  http *:80:
                                                                net.tcp 808:*
                                                                net.pipe *
                                                                net.msmq localhost
                                                                msmq.formatname localhost 
MyWebsite1       2    Started    C:\inetpub\MyWebsite1          http *:80:mywebsite1.com
MyWebsite2       3    Started    C:\inetpub\MyWebsite2          http *:80:mywebsite2.com

If I try to run the command without the "-Name" parameter, the result is exactly the same.

like image 573
Jarrett Coggin Avatar asked Jun 14 '12 21:06

Jarrett Coggin


People also ask

How do I see IIS sites?

The Get-IISSite cmdlet gets information about Internet Information Services (IIS) web sites and their current status and other key information. If you a request a specific site or a comma delimited list of sites, only those whose names are passed as an argument are returned. Otherwise all the websites are returned.

How do I stop a website from PowerShell using IIS?

To stop an IIS website, you will use the Stop-IISSite cmdlet. By default, the site that you created earlier should be in a Started state.

How do I tell if IIS is installed on PowerShell?

Start by hitting the WINKEY + R button combination to launch the Run utility, type in '%SystemRoot%\system32\inetsrv\InetMgr.exe' and hit Enter. Also, you can enter inetmgr and hit Enter to launch the same IIS Manager and follow the same steps as for the Command Prompt method.

How do I check my application pool status?

The Get-IISAppPool cmdlet gets information about application pools and their current status and other key information. If a specific application pool or a comma delimited list of application pools are requested, only those whose names are passed as an argument are returned.


2 Answers

You can use Test-Path for both checking web sites & application pools:

Import-Module webadministration
$alias = "MyWebSite1"
$IISPath = "IIS:\Sites\Default Web Site\$alias"

if (Test-Path $IISPath) { Write-Host "$alias exists." }

$IISPath = "IIS:\AppPools"
cd $IISPath
if (Test-Path ".\MyAppPool") { Write-Host "MyAppPool exists." }
like image 52
David Brabant Avatar answered Oct 01 '22 16:10

David Brabant


I just noticed the same behavior. Seems like its not working as expected. However, you can roll your own:

get-website | where-object { $_.name -eq 'MyWebsite1' }

That just pipes the list returned by get-website to the where-object cmdlet and just return that single object.

If your new to PowerShell, I can't recommend Master PowerShell enough.

like image 28
Zach Bonham Avatar answered Oct 01 '22 17:10

Zach Bonham