How do I determine the last time a Web Site was visited?


COMMENT: This one looks at a list of IIS servers, checks the Site Logs
to determine the last time someone visited the site.


<# =====================================================================
========================================================================
#>
function Test-Port {
    Param(
        [string] $srv,
        $port=135,
        $timeout=3000,
        [switch]$verbose
    )
    # Test-Port.ps1
    # Does a TCP connection on specified port (135 by default)
    $ErrorActionPreference = "SilentlyContinue"
    # Create TCP Client
    $tcpclient = new-Object system.Net.Sockets.TcpClient
    # Tell TCP Client to connect to machine on Port
    $iar = $tcpclient.BeginConnect($srv,$port,$null,$null)
    # Set the wait time
    $wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)
    # Check to see if the connection is done
    if(!$wait)
    {
        # Close the connection and report timeout
        $tcpclient.Close()
        if($verbose){Write-Host "Connection Timeout"}
        Return $false
    }
    else
    {
        # Close the connection and report the error if there is one
        $error.Clear()
        $tcpclient.EndConnect($iar) | out-Null
        if(!$?){if($verbose){write-host $error[0]};$failed = $true}
        $tcpclient.Close()
    }
    # Return $true if connection Establish else $False
    if($failed){
        return $false
    } else {
        return $true
    }
} # <a href="http://technet.microsoft.com/en-us/library/ff730959.aspx">http://technet.microsoft.com/en-us/library/ff730959.aspx</a>

$ErrorActionPreference = "SilentlyContinue"
$i = $j = 1
$strFile = "E:\scripts\LogFile.txt"
$oLogFile = New-Item -ItemType file $strFile -Force
$srvName = "IIS Servers"
$strDate = Get-Date -format g
Add-Content -Path $strFile "Server `tOS `tSite ID `tSite Name `tLog File Directory `tDate/Time `tHow many days ago `tLogging Enabled `t$strDate"

$servers = gc IISservers.txt
$C = $Servers.count
foreach($server in $servers)
{ Write-Progress -Activity "Servers..." -Status "$i of $C" `
    -percentcomplete ($i / $C*100) -id 1
      If (Test-Connection -computername $server -Quiet -count 1){  
           $a = Test-Port $server
           If ($a) {
               $WMIJob = Get-WmiObject Win32_OperatingSystem -Comp $server -AsJob
            Wait-Job -ID $WMIJob.ID -Timeout 20
            $os = Receive-Job $WMIJob.ID
            if ($os -ne $null) {
            switch ($os.version) {
                "5.1.2600" {$osVer = "XP/IIS5.1"}
                "5.2.3790" {$osVer = "Server 2003/IIS6"}
                "6.0.6001" {$osVer = "Server 2008/IIS7"}
                "6.1.7600" {$osVer = "Server 2008 R2/IIS7.5"}
                    }
                $iis = [adsi]"IIS://$server/w3SVC"
                $sites = $iis.Children | ?{$_.keytype -eq "IIsWebServer"}
                $sc = $sites.count
                $j = 1
        foreach($site in $sites)
        {     
        Write-Progress -Activity "Sites..." -Status "$j of $sc" `
        -percentcomplete ($j / $sc*100) -ParentID 1
         $daysago = $null
        $logdir = $site.LogFileDirectory
        $lfd = $logdir -replace ':\\', '$\'
        $lfd = "\\$server\$lfd\w3svc$($site.name)"
        $Comment = [string]$($site.ServerComment)
        $WebSite = [string]$($Site.Name)
        Switch ($site.LogType) {
                                0 { $result = "Disabled" }
                                1 { $result = "Enabled" }
                                }
        if(Test-Path $lfd)
            {
            $lwt = gci $lfd | sort lastwritetime -Descending | select -first 1 -ExpandProperty lastwritetime         
            If ($lwt) {
            $daysago = (New-TimeSpan -Start $lwt -End ([datetime]::Now)).days }
                Else {
                    $lwt = "Dir is Empty"
                    }
            $WebSite = [string]$($Site.Name)
            $lfd = [string]$lfd
            $Comment = [string]$($site.ServerComment)
            Add-Content -Path $strFile "$server`t$osVer`t$WebSite `t$Comment `t$lfd `t$lwt `t$daysago `t$result"
                    }
                    Else {
                    Add-Content -Path $strFile "$server`t$osVer`t$WebSite `t$Comment `t$lfd `tLogFile Directory not found `t `t$result"
                        }
                    $j++
                    }
                }
            #   GWMI fail    
            Else {
            $b = $error | select Exception
              $E = $b -split (":")
              $x = $E[1]
            $Error.Clear()
            $d = $x.StartsWith(" Access")
            If ($d){
                    Add-Content -Path $strFile "$server `tAccess Denied"
                    }
                Else {
                    Add-Content -Path $strFile "$server `t$x"
                    }
                }    
             }       
            Else {
                    Add-Content -Path $strFile "$server `tRPC Port not listening"
                    $i++
                    }
        }
    Else {Add-Content -Path $strFile "$server `tDoes Not Ping"
            $i++   
            }
    $i++
}

############# End POSH Script ###############

Advertisements

6 thoughts on “How do I determine the last time a Web Site was visited?

  1. Hello there, just became aware of your blog through Google, and found that it’s truly informative. I’m gonna watch out for brussels. I’ll appreciate if you continue this in future. Many people will be benefited from your writing. Cheers!

  2. Needed to send you a little bit of observation to be able to say thanks a lot yet again for these pretty secrets you have discussed above. This is simply incredibly generous with you to convey easily just what many people could have sold for an e-book to make some cash for their own end, mostly since you might have done it if you ever decided. These points additionally acted like the good way to fully grasp that someone else have a similar fervor like my personal own to grasp a great deal more when considering this problem. I’m certain there are numerous more pleasurable moments up front for people who examine your site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s