Wednesday, 18 July 2018

UserHits for Particular site

#Functions to Imitate SharePoint 2010 Cmdlets in MOSS 2007
function global:Get-SPWeb($url)
{
  $site= New-Object Microsoft.SharePoint.SPSite($url)
        if($site -ne $null)
        {
              $web=$site.OpenWeb();      
        }
    return $web
}
 
  
        #Method to Get Usage Data
        Function GetWebUsageData($Web)
        {
            try
            {
                #DataTable for Hits result - Because GetUsageData returns DataTable!
                $dtHits = New-Object System.Data.DataTable 
  
                $dtHits = $Web.GetUsageData("url", "lastMonth")
 
                if ($dtHits -ne $null)
                {
                    return ($dtHits);
                }
                else
                {
                    return ($null);
                }
            }
            catch
            {
                Write-Host $_.Exception.Message -ForegroundColor Red
                return ($null);
            }
        }
  
    $WebURL = "http://sharepoint.crescent.com/operations/sfdc/"
     
    $CurrentPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
    $OutPutFile = Join-path $CurrentPath "SiteUsageReport.txt"
     
    #Get the Web
    $Web = Get-SPWeb $WebURL
  
 
    #create a CSV file
    "Page `t Total Hits `t Last Accessed" > $OutPutFile #Write the Headers in to a text file
 
    #Get the Hits Data
    $HitsDT = GetWebUsageData($Web)
    if ($HitsDT -ne $null)
    {
        foreach($dr in $HitsDT)
        {
           
          $result = $webURL +"/" + $dr["Folder"]+"/"+$dr["Page"] + "`t" + $dr["Total Hits"] + "`t" + $dr["Most Recent Day"]
          $result >> $OutPutFile  #append the data
        }
    }

No comments:

Post a Comment