none
How do I find the last time a web site was accessed

Answers

  • my take on this...

    $results = @()
    
    $servers = gc server.txt
    foreach($server in $servers)
    {
        $iis = [adsi]"IIS://$server/w3SVC"
        $sites = $iis.Children | ?{$_.keytype -eq "IIsWebServer"}
        foreach($site in $sites)
        {
            $logdir = $site.LogFileDirectory
            $logdir = $logdir -replace ':\\', '$\'
            $logdir = "\\$server\$logdir\w3svc$($site.name)"
            if(Test-Path $logdir)
            {
                $lwt = gci $logdir | sort lastwritetime -Descending | select -first 1 -ExpandProperty lastwritetime
                $daysago = (New-TimeSpan -Start $lwt -End ([datetime]::Now)).days
                $obj = New-Object psobject -Property @{
                    Server=$server
                    Site = $($Site.name)
                    Comment = $($site.ServerComment)
                    LastUsed = $lwt
                    DaysAgo = $daysago
                    }
                $results += $obj
            }
        }
    }
    I should add, I tested this in V3 but im pretty sure there is nothing V3 specific in here, also i only tested it on one working server, might need to add some error trapping in there

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by jrich Monday, May 07, 2012 12:49 PM
    • Marked as answer by OldDog1 Monday, May 07, 2012 7:05 PM
    Monday, May 07, 2012 12:48 PM

All replies

  • the web site is essentially files on a computer. could you not just use find the root directory of the site and use get-childitem to get the last access time of the folders?

    Saturday, May 05, 2012 4:02 AM
  • IMHO date info for folders is sometimes misleading. Might work for key files, however, last accessed date will be updated for *any* access, not just through the web. If some background process happens to periodically upload files for example. Or if the developer/maintainer takes a copy to start developing version 2.

    I would think that the best approach would be for the site to run script that logs accesses. This would probably be better done through server side scripting, rather than client side.


    Al Dunbar

    Saturday, May 05, 2012 4:51 AM
  • You can use log parser tool to find out that. You may refer below thread to create batches / Scripts.  You can run those batches on IIS logs.

    http://blogs.msdn.com/b/carlosag/archive/2010/03/25/analyze-your-iis-log-files-favorite-log-parser-queries.aspx


    Sunil Pathare SR. System Admin


    Saturday, May 05, 2012 5:19 AM
  • To clarify my purpose here. There are over 4500 servers in our domain that have IIS installed. Management wants to know how many of these are actually being used to serve web sites and how many are just "installed". If they are being used  to serve a web site or web sites, when was the last time they were accessed? So, if a developer hits it, that counts.  Or " If some background process happens to periodically upload files" that would count as well.
    Saturday, May 05, 2012 5:19 PM
  • " find the root directory of the site "

    If a web server has multiple sites, how do you determine which directory belongs to which site?

    Sorry for the really basic questions, but IIS is a bit of a mystery to me.

    Saturday, May 05, 2012 5:26 PM
  • Every file which you are accessing is counted as a hit.  To find out directory of website

    Run > Inetmgr > Expand Websites > Right Click on any website > check Home Directory Tab


    Sunil Pathare SR. System Admin

    Monday, May 07, 2012 4:01 AM
  • my take on this...

    $results = @()
    
    $servers = gc server.txt
    foreach($server in $servers)
    {
        $iis = [adsi]"IIS://$server/w3SVC"
        $sites = $iis.Children | ?{$_.keytype -eq "IIsWebServer"}
        foreach($site in $sites)
        {
            $logdir = $site.LogFileDirectory
            $logdir = $logdir -replace ':\\', '$\'
            $logdir = "\\$server\$logdir\w3svc$($site.name)"
            if(Test-Path $logdir)
            {
                $lwt = gci $logdir | sort lastwritetime -Descending | select -first 1 -ExpandProperty lastwritetime
                $daysago = (New-TimeSpan -Start $lwt -End ([datetime]::Now)).days
                $obj = New-Object psobject -Property @{
                    Server=$server
                    Site = $($Site.name)
                    Comment = $($site.ServerComment)
                    LastUsed = $lwt
                    DaysAgo = $daysago
                    }
                $results += $obj
            }
        }
    }
    I should add, I tested this in V3 but im pretty sure there is nothing V3 specific in here, also i only tested it on one working server, might need to add some error trapping in there

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by jrich Monday, May 07, 2012 12:49 PM
    • Marked as answer by OldDog1 Monday, May 07, 2012 7:05 PM
    Monday, May 07, 2012 12:48 PM