9

I have hit a complete roadblock and cannot figure out how to print out the virtual directories for each website in IIS 8. I keep retrieving Applications and empty null argument errors and just can't find a way around to get the virtual directories to list out.

I need the following data to list out about the Virtual Directories:

  • Physical Path
  • App Pool Association
  • Site Name
  • Credentials

Below is the code I have been using and messing around with:

    *See revised code below*
    

I have also used the Appcmd.exe command to try and to list out the Virtual Directories:

C:\Windows\System32\inetsrv\appcmd.exe LIST VDIRS

But I am still not getting the output I need. Anything or any help would be highly appreciated! Thanks!

EDIT#----------------------------------------------------------------------

Got my virtual directories to list out by commenting out a couple lines but it only prints out the name and the:

  • Physical Path
  • App Pool Association
  • Site Name
  • Credentials

are still unknown to me, below is the revised code:

  Import-Module WebAdministration
    $Websites = Get-ChildItem IIS:\Sites 
    foreach($Site in $Websites)
            {
                $webapps = Get-WebApplication -Site $Site.name 
                $VDir = Get-WebVirtualDirectory -Site $Site.name #-Application $webapps.name
                                foreach($webvdirectory in $VDir)
                                    {
                                        $webvdirectory2 = $webvdirectory.path
                                        Write-Host $webvdirectory2.split("/")[-1]
                                        #Write-Host $webvdirectory
                                        #Write-Host $webvdirectory.name
                           
                                    } 
                            #Write-Host $VDir 
            
            }
user38725
  • 369
  • 3
  • 5
  • 13
  • Unless you convert a virtual directory to an application, it inherits the site's application pool. Are you also looking for virtual directories which are converted to applications as well as "normal" virtual directories? – jscott Jul 08 '14 at 15:31
  • $jscott I am looking to print out **all** virtual directories that exist in IIS, and the specified information I listed at the top. – user38725 Jul 08 '14 at 15:54

3 Answers3

12

You write that you can't figure out how to retrieve:

  • Physical Path
    • is directly accessible through the physicalPath noteproperty
  • App Pool Association
    • doesn't make sense, Applications are assigned to app pools, directories themselves are not
  • Site Name
    • You already know this ($Site.name)
  • Credentials
    • I assume you just want the username if present

These could all be retrieved with some slight alterations to your existing script:

Import-Module WebAdministration

$Websites = Get-ChildItem IIS:\Sites 

$AllVDirs = @()

foreach($Site in $Websites)
{
    $VDirs = Get-WebVirtualDirectory -Site $Site.name
    foreach($webvdirectory in $VDirs)
    {
            $vdir = New-Object psobject -Property @{
                "Name" = ($webvdirectory.path -split "/")[-1]
                "Site" = $Site.name
                "Path" = $webvdirectory.path
                "PhysicalPath" = $webvdirectory.physicalPath
                "PhysicalPathCredentials" = $webvdirectory.userName
            }

            $AllVDirs += $vdir
    } 
}

$AllVDirs

Now you can export $AllVDirs to Xml, Csv or simply print it to the PowerShell host.

Morpheus
  • 103
  • 4
Mathias R. Jessen
  • 24,907
  • 4
  • 62
  • 95
2

Thank you @Mathias R. Jessen , I forgot about this post and I should've came back because I figured most of it out. Your script works perfect and here's mine just for S&Gs, didn't realize all I had to do was call on the objects and properties.

$Websites = Get-ChildItem IIS:\Sites 
    foreach($Site in $Websites)
            {
                $webapps = Get-WebApplication -Site $Site.name 
                $VDir = Get-WebVirtualDirectory -Site $Site.name #-Application $webapps.name
                                foreach($webvdirectory in $VDir)
                                    {
                                        $webvdirectory2 = $webvdirectory.path
                                        Write-Host $webvdirectory2.split("/")[-1] "::: is the Virtual Directory" -ForegroundColor Green
                                        Write-Host $webvdirectory.physicalPath   
                                        Write-Host $webvdirectory.userName                                         
                                        Write-Host $webvdirectory.password
                                        Write-Host $webvdirectory.logonMethod
                                    } 
                            #Write-Host $VDir 

            }

Also have another problem though and didn't know if you had the answer, how would I call on the Physical Path credentials property for web applications? I cant print out the username or password, but I can print the logonMethod which uses the same property? below is my code for web applications, it is very similar to my virtual directories code:

$Websites = Get-ChildItem IIS:\Sites 
    foreach($Site in $Websites)
            { 
             $webapps = Get-WebApplication -Site $Site.name 
                    foreach($webapp in $webAPPS)
                        {
                        Write-Host $webapp.applicationPool
                        Write-Host $WebApp.virtualDirectoryDefaults.userName
                        Write-Host $webapp.virtualDirectoryDefaults.password
                        Write-Host $webapp.virtualDirectoryDefaults.logonMethod
                        $webapp2 = $webapp.path
                        Write-Host $WebApp2.split("/")[-1] "::: is the Web Application" -ForegroundColor Green

                        } 
            }
user38725
  • 369
  • 3
  • 5
  • 13
  • I am also thinking if I get the $Site.userName and the $Site.password, wouldn't they be the same for the Web App? – user38725 Jul 11 '14 at 15:26
0

This will create an array that contains the Site Name, Virtual Directory Name and Virtual Directory Physical Path.

$SiteVirtualDirectories = @()
$Sites = gci IIS:\Sites
foreach($Site in $Sites)
    {
    $VirtualDirectories = gci "IIS:\Sites\$($Site.Name)" | ?{$_.NodeType -match "virtualDirectory"}
    foreach($VirtualDirectory in $VirtualDirectories)
        {
        $SiteVirtualDirectories += @([pscustomobject]@{Site=$Site.Name;VirtualDirectory=$VirtualDirectory.Name;PhysicalPath=$VirtualDirectory.PhysicalPath})
        }
    }
Write-Host $SiteVirtualDirectories