Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to get IIS web site directory

2,997 views
Skip to first unread message

George

unread,
Aug 27, 2008, 3:04:01 AM8/27/08
to
Hello everyone,


In my local computer, if I know the sub-web site name is "MyTestSite" under
the Web Sites category of IIS manager, how could I retrieve the absolute
physical file path for the sub-web site?


thanks in advance,
George

RichS [MVP]

unread,
Aug 27, 2008, 11:54:01 AM8/27/08
to
For IIS 7

using the IIS provider
(Get-Item iis:\sites\testsite).physicalpath


--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk

George

unread,
Aug 27, 2008, 11:14:00 PM8/27/08
to
Thanks Richard,


I have tried but failed, here is the error message, any ideas?

--------------------
Get-Item : Cannot find drive. A drive with name 'iis' does not exist.
At line:1 char:10
+ (get-item <<<< iis:\sites\testsite).physicalpath
--------------------


regards,
George

Shay Levy [MVP]

unread,
Aug 31, 2008, 12:25:26 PM8/31/08
to
Hello George,

This will get the phisical path for the 'default web site' website:

$server = "localhost"
$siteName = "default web site"

PS > $iis = [ADSI]"IIS://$server/W3SVC"
PS > $site = $iis.psbase.children | where {$_.schemaClassName -eq "IIsWebServer"
-AND $_.ServerComment -eq 'default web site'}
PS > $path = $site.psbase.children | where {$_.name -eq 'root'}
PS > $path.path

c:\inetpub\wwwroot

If you know the website ordinal number then it can be as simple as:

PS > $site = [ADSI]"IIS://$server/W3SVC/1/ROOT"
PS > $site.path
c:\inetpub\wwwroot


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic

G> Hello everyone,
G>
G> In my local computer, if I know the sub-web site name is "MyTestSite"
G> under the Web Sites category of IIS manager, how could I retrieve the
G> absolute physical file path for the sub-web site?
G>
G> thanks in advance,
G> George


George

unread,
Sep 1, 2008, 10:13:00 AM9/1/08
to
Thanks Shay,


1.

I have tried that your 2nd solution works but the 1st one does not work
(there is no output from command $path.path), any ideas to do further
analysis?

2.

I want to learn more about like what does W3SVC or W3SVC/1/ROOT mean in IIS?
Any reference to follow up learning?


regards,
George

Shay Levy [MVP]

unread,
Sep 1, 2008, 12:43:49 PM9/1/08
to

1. Can you try this again, don't forget to change the $server and $siteName
variables to match your criteria :

$server = "localhost"
$siteName = "default web site"

$iis = [ADSI]"IIS://$server/W3SVC"

$site = $iis.psbase.children | where { $_.schemaClassName -eq "IIsWebServer"

-AND $_.ServerComment -eq $siteName }

$path = $site.psbase.children | where {$_.name -eq 'root'}

$path.path


2. To get access to the contents of your iis server you point it to the W3SVC
container.

PS > $iis = [ADSI]"IIS://$server/W3SVC"

When you list the children (I'm selecting path only for bravity), you 'll
see something similar to:


PS > $iis.psbase.children | ft path

Path
----
IIS://localhost/W3SVC/Info
IIS://localhost/W3SVC/Filters
IIS://localhost/W3SVC/1
IIS://localhost/W3SVC/2
IIS://localhost/W3SVC/3


The last three rows (e.g W3SVC/1..3) denotes websites (the number is the
site index). You may see more (or less) if you're IIs has more then three
websites.
Typically, the first one (W3SVC/1) is associated with the "Default Web Site".
Exploring this as a starting point to [ADSI] shows:


PS > $iis = [ADSI]"IIS://$server/W3SVC/1"
PS > $iis.psbase.children | ft path

path
----
{c:\inetpub\wwwroot}
IIS://localhost/W3SVC/1/IIsCertMapper


The first row is the path to the site root folder (i.e ROOT container) and
is equivalent to :


PS > $iis = [ADSI]"IIS://$server/W3SVC/1/ROOT"
PS > $iis.path
C:\Inetpub\wwwroot

I have this book and I find it very usefull:

Windows NT/2000 ADSI Scripting for System Administration
http://my.safaribooksonline.com/1578702194


Also check the Script Repository: Internet Information Server 6.0
http://www.microsoft.com/technet/scriptcenter/scripts/iis/iis6/default.mspx?mfr=true


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic

G> Thanks Shay,
G>
G> 1.
G>
G> I have tried that your 2nd solution works but the 1st one does not
G> work (there is no output from command $path.path), any ideas to do
G> further analysis?
G>
G> 2.
G>
G> I want to learn more about like what does W3SVC or W3SVC/1/ROOT mean
G> in IIS? Any reference to follow up learning?
G>
G> regards,
G> George


George

unread,
Sep 2, 2008, 1:48:01 AM9/2/08
to
Thanks for your comprehensive reply, Shay. I followed your comments and did
more experiments, and very strange results -- approach 1 works and approach 2
does not work. I share my detailed analysis here, do you have any ideas to
analyze further? :-)

I changed your scripts by 2 points,

- I changed the machine name from localhost to TestMachine, which is the
machine name I run the script;
- I changed from "default web site" to "Default Web Site" (see the 3 capital
letters).

Approach 1 (works)

PS D:\> $site=[ADSI]"IIS://TestMachine/W3SVC/1/ROOT"
PS D:\> $site.path

output is c:\inetpub\wwwroot

Approach 2 (not working)

$server = "TestMachine"
$siteName = "Default Web Site"


$iis = [ADSI]"IIS://$server/W3SVC"
$site = $iis.psbase.children | where { $_.schemaClassName -eq "IIsWebServer"
-AND $_.ServerComment -eq $siteName }
$path = $site.psbase.children | where {$_.name -eq 'root'}
$path.path

(output is null)


regards,
George

Shay Levy [MVP]

unread,
Sep 2, 2008, 9:32:00 AM9/2/08
to
Hello George,

Strings comparisons are case insenesitive so when you use the -eq operator,
"Default Web Site" is the same as "default web site".
For case-senesitive comparisons use -ceq. You can find more information in
this help file:

PS > help about_comparison_operators


Approach 2 works for me because I'm working with PowerShell CTP2. Here's
a workaround for v1:


$server = "localhost"
$siteName = "default web site"

$iis = [ADSI]"IIS://$server/W3SVC"

$site = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND
$_.ServerComment -eq $siteName }
$path = [adsi]($site.psbase.path+"/ROOT")
$path.path

---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic

G> Thanks for your comprehensive reply, Shay. I followed your comments
G> and did more experiments, and very strange results -- approach 1
G> works and approach 2 does not work. I share my detailed analysis
G> here, do you have any ideas to analyze further? :-)
G>
G> I changed your scripts by 2 points,
G>
G> - I changed the machine name from localhost to TestMachine, which is
G> the
G> machine name I run the script;
G> - I changed from "default web site" to "Default Web Site" (see the 3
G> capital
G> letters).
G> Approach 1 (works)
G>
G> PS D:\> $site=[ADSI]"IIS://TestMachine/W3SVC/1/ROOT" PS D:\>
G> $site.path
G>
G> output is c:\inetpub\wwwroot
G>
G> Approach 2 (not working)
G>
G> $server = "TestMachine"
G> $siteName = "Default Web Site"
G> $iis = [ADSI]"IIS://$server/W3SVC"
G> $site = $iis.psbase.children | where { $_.schemaClassName -eq
G> "IIsWebServer"
G> -AND $_.ServerComment -eq $siteName }
G> $path = $site.psbase.children | where {$_.name -eq 'root'}
G> $path.path
G> (output is null)

George

unread,
Sep 2, 2008, 9:10:01 AM9/2/08
to
I have tried your solution works, cool! Two more comments,

1. Suppose I have a virtual directory called "Monitor" under the "default
web site", then how to get the related physical directory for the virtual
directory "Monitor"?

2.

I found when change code from

$site = $iis.psbase.children | where { $_.schemaClassName -eq "IIsWebServer"
-AND $_.ServerComment -eq $siteName }


$path = $site.psbase.children | where {$_.name -eq 'root'}

to

$site = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND
$_.ServerComment -eq $siteName }
$path = [adsi]($site.psbase.path+"/ROOT")

, it works under v1.0. Why the fix works (I have never used v2.0 before and
I am not familiar with it)?


regards,
George

Shay Levy [MVP]

unread,
Sep 2, 2008, 2:37:39 PM9/2/08
to
Hi George,


Get the related physical directory for any virtual directory:


$server = "localhost"
$vDirName = "Monitor"


$siteName = "default web site"

$iis = [ADSI]"IIS://$server/W3SVC"

$site = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND
$_.ServerComment -eq $siteName }

$dws = [ADSI]($site.psbase.path+"/ROOT")

$vDir= $dws.psbase.children | where {$_.keyType -eq "IISWebVirtualDir" -AND
$_.AppFriendlyName -eq $vDirName}
$vDir.path


The ADSI type accelerator is behaving differently in v1 vs. v2, I'll do my
best to find out why.

---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic

G> I have tried your solution works, cool! Two more comments,
G>
G> 1. Suppose I have a virtual directory called "Monitor" under the
G> "default web site", then how to get the related physical directory
G> for the virtual directory "Monitor"?
G>
G> 2.
G>
G> I found when change code from
G>

G> $site = $iis.psbase.children | where { $_.schemaClassName -eq
G> "IIsWebServer"
G> -AND $_.ServerComment -eq $siteName }

G> $path = $site.psbase.children | where {$_.name -eq 'root'}
G> to
G>
G> $site = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer"
G> -AND
G> $_.ServerComment -eq $siteName }
G> $path = [adsi]($site.psbase.path+"/ROOT")
G> , it works under v1.0. Why the fix works (I have never used v2.0
G> before and I am not familiar with it)?

George

unread,
Sep 3, 2008, 5:08:01 AM9/3/08
to
Thanks Shay,


I have tested that your script works. Cool! And I have made more learning by
implementing in some altervative ways. Here is my code to achieve the same
purpose, but the output is nothing. Do you have any ideas?

$website="TestMachine"

$siteName = (Get-Wmiobject -namespace "root/MicrosoftIISv2" -q "SELECT

* FROM IIsWebServerSetting WHERE ServerComment = '$website'").Name

$de = new-object -typename system.directoryservices.directoryentry

-argumentlist "IIS://TestMachine/Monitor/root"

[system.directoryservices.directoryentries]$der=$de.psbase.children

trap { continue }
$v=$der.find($virdir, $de.psbase.schemaclassname.tostring())

if ($v -ne $null)
{
$v.psbase.Properties.Path
}


regards,
George

Shay Levy [MVP]

unread,
Sep 3, 2008, 1:33:36 PM9/3/08
to
Hello George,


If the output for this is nothing then you are probably supplying a wrong
value, it works on my side, try with "Deafult Web Site" and see if it works:

$website="Deafult Web Site"


$siteName = (Get-Wmiobject -namespace "root/MicrosoftIISv2" -q "SELECT *
FROM IIsWebServerSetting WHERE ServerComment = '$website'").Name


[ADSI] is a type shortcut (accelerator). So instaed of typing:

$de = new-object -typename system.directoryservices.directoryentry -argumentlist
"IIS://TestMachine/Monitor/root"


You can simply do:
[ADSI]"IIS://TestMachine/Monitor/root"


You can see that the objects are from the same type when when you pipe the
result to Get-Member:

PS 11> [ADSI]"IIS://localhost" | gm

TypeName: System.DirectoryServices.DirectoryEntry

...

It is good to know that you are trying to implement your alternatives, keep
doing it. This is a part of the learning curve and I'm sure you'll
find interesting stuff as you proceed, explore and you shall find ;-)

---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic

G> Thanks Shay,
G>
G> I have tested that your script works. Cool! And I have made more
G> learning by implementing in some altervative ways. Here is my code to
G> achieve the same purpose, but the output is nothing. Do you have any
G> ideas?
G>
G> $website="TestMachine"
G>
G> $siteName = (Get-Wmiobject -namespace "root/MicrosoftIISv2" -q
G> "SELECT
G>
G> * FROM IIsWebServerSetting WHERE ServerComment = '$website'").Name
G>
G> $de = new-object -typename system.directoryservices.directoryentry
G>
G> -argumentlist "IIS://TestMachine/Monitor/root"
G>
G> [system.directoryservices.directoryentries]$der=$de.psbase.children
G>
G> trap { continue } $v=$der.find($virdir,
G> $de.psbase.schemaclassname.tostring())
G>
G> if ($v -ne $null)
G> {
G> $v.psbase.Properties.Path

George

unread,
Sep 4, 2008, 1:24:00 AM9/4/08
to
Thanks for your comprehensive reply, Shay!


I have tried below but seems ADSI is not working for my system? Do I need to
load some reference DLLs in order to use ADSI?

PS D:\> [ADSI]"iis://localhost"
out-lineoutput : Exception retrieving member
"ClassId2e4f51ef21dd47e99d3c952918aff9cd": "Unknown error (0x80005000)"


regards,
George

0 new messages