Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell + WebAdministration - How to get website from webapplication?

I'm writing a PowerShell script to perform certain administrative functions in IIS 7.5.

import-module WebAdministration

In some cases I know the name of the web application I want to work with but not the web site it is under. Getting the application is easy:

$app = get-webapplication -name 'MyApp'

But I cannot figure out how to get the name of the web site given the app. It doesn't seem to be a property off the webapplication object. Best I could come up with was trying to get it via test-path:

get-website | where {test-path "iis:\sites\$_.name\MyApp"}

For some reason that comes up empty. Any thoughts on how to go about this? Thanks in advance.

like image 841
Todd Menier Avatar asked Sep 21 '11 17:09

Todd Menier


2 Answers

I haven't dug into why, but if you use an asterisk or question mark anywhere in the string it works like a wildcard and returns as such.

I.E.

get-website -name '*myapp'

   Name      ID   State    Physical Path        Bindings
   ----      --   -----    -------------        --------

   myapp     12   Started  C:\inetpub\wwwroot   http:*:80:
  amyapp     13   Stopped  C:\anetpub\          http:*:81:
 aamyapp     14   Stopped  C:\another\place     http:172.198.1.2:80:host.header.com

or

get-website -name '?myapp'

Name   ID State   Physical Path Bindings
----   -- -----   ------------- --------
amyapp 13 Stopped C:\anetpub    http:*:81:
like image 115
Larry Dukek Avatar answered Oct 03 '22 06:10

Larry Dukek


This is how you can get the site name:

$siteName = (Get-WebApplication -name 'YourApp').GetParentElement().Attributes['name'].Value

Or even shorter:

$siteName = (Get-WebApplication -name 'YourApp').GetParentElement()['name']
like image 42
mthierba Avatar answered Oct 03 '22 06:10

mthierba