Need help with Exporting Data

Hi Guys

I'm trying to write a script to export site usage data in SharePoint. I was able to get data in following format:

"User","Jul <br> 14","Jul <br> 13","Jul <br> 12","Jul <br> 11","Jul <br> 10"
"ad\spuser1","0","0","0","1","1088"
"ad\spuser2","0","0","0","0","0"

This data is per SP site. I want to have data in following format(which is one single output file for all sites):

"SiteUrl", "SiteTitle","User","Jul <br> 14","Jul <br> 13","Jul <br> 12","Jul <br> 11","Jul <br> 10"

"Siteurl1","SiteTitle1","ad\spuser1","0","0","0","1","1088"

"Siteurl1","SiteTitle1","ad\spuser2","0","0","0","1","1088"

"Siteurl2","SiteTitle2","ad\spuser1","0","0","0","1","1088"

"Siteurl2","SiteTitle2","ad\spuser2","0","0","0","1","1088"

Below is the code I written:

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") $SitesList = Get-Content -Path 'Sites2.txt' function GetUsageInfo($Web) { Write-Host "Working on site: " $Web.Url $Period = New-Object Microsoft.SharePoint.Administration.SPUsagePeriodType $Report = New-Object Microsoft.SharePoint.Administration.SPUsageReportType [Microsoft.SharePoint.Administration.SPUsageReportType] $report = "user" $Usage = $web.GetUsageData($Report,$Period) $Usage | Export-CSV Out.csv -NoTypeInformation

} foreach($SiteUrl in $SitesList) { $SiteUrl = $SiteUrl.TrimEnd() $Site = New-Object Microsoft.SharePoint.SPSite($SiteUrl) $Web = $Site.OpenWeb() GetUsageInfo($Web) }

Can you pls help me with the script for getting output in required format.

July 15th, 2015 10:06am

Not sure, if this will help: $Usage is of type System.Data.DataTable

Free Windows Admin Tool Kit Click here and download it now
July 15th, 2015 10:16am

You are way overcomplicating this.

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SharePoint')

Get-Content Sites2.txt |
    ForEach-Object{
        $Site = New-Object Microsoft.SharePoint.SPSite($_)
        $Web = $Site.OpenWeb()
        $Usage = $web.GetUsageData('User', 'day or lastmonth')
    } |
    Export-Csv out.csv -Notype
July 15th, 2015 10:42am

Thanks jrv. Your code simplifies a lot. Surprisingly, out.csv file is empty. I did verify that $Usage gets filled when using below code:

Get-Content Sites2.txt |
    ForEach-Object{
        $Site = New-Object Microsoft.SharePoint.SPSite($_)
        $Web = $Site.OpenWeb()
        $Usage = $web.GetUsageData('user', 'day')
    } | Export-Csv out.csv -Notype

Did I miss something?
Free Windows Admin Tool Kit Click here and download it now
July 15th, 2015 11:26am

Sorry  - I forgot too remove that:

Get-Content Sites2.txt |
    ForEach-Object{
        $Site = New-Object Microsoft.SharePoint.SPSite($_)
        $Web = $Site.OpenWeb()
        $web.GetUsageData('user', 'day')
    } | Export-Csv out.csv -Notype

With scritping try to keep it as simple as possible.  That is the whole pointoof scripts.  You are not a programmer.  You are an admin or tech and are not designing solutions or programs; you are doing reporting,maintenance and configurations.  Simpllicity is your friend.

July 15th, 2015 11:29am

Thanks again jrv.  This is very close to what I'm looking for. However I cannot get site url / site title in this output. So its basically unable to tell the url for which statistics is associated. I tried to use a custom PS object and then output to text but could not make it to required formatting.  

We can use this line for site url: $Url = $web.url

I need to put this url on every output from $web.GetUsageData('user', 'day'). Can you pls help me with same.

Free Windows Admin Tool Kit Click here and download it now
July 15th, 2015 1:39pm

 $web.GetUsageData('url', 'lastMonth')

 $web.GetUsageData('system', 'day')

 $web.GetUsageData('user', 'day')

https://msdn.microsoft.com/EN-US/library/ms426295



July 15th, 2015 1:43pm

Sorry for not being clear. I'm more looking to this sort of output:

Get-Content Sites2.txt |
    ForEach-Object{
        $Site = New-Object Microsoft.SharePoint.SPSite($_)
        $Web = $Site.OpenWeb()
        $Usage = $web.GetUsageData('user', 'day')
        $PS = New-Object psobject @{
            Url = $Web.url
            Usage = $Usage
        }
        $PS.url, $PS.usage #Using $PS also wont generate proper o/p
    } | Export-Csv out.csv -Notype 
Above code won't work. So I just want to get site url in each line of same output. We don't need to use a different function.
Free Windows Admin Tool Kit Click here and download it now
July 15th, 2015 2:07pm

Get-Content Sites2.txt |
    ForEach-Object{
        $Site = New-Object Microsoft.SharePoint.SPSite($_)
        $Web = $Site.OpenWeb()
        $Usage = $web.GetUsageData('user', 'day')
        $Usage | Add-Member -Name URL -Value $Web.Url -MemberType NoteProperty -PassThru
    } | 
    Export-Csv out.csv -Notype		
July 15th, 2015 3:39pm

Sorry for not being clear. I'm more looking to this sort of output:

Get-Content Sites2.txt |
    ForEach-Object{
        $Site = New-Object Microsoft.SharePoint.SPSite($_)
        $Web = $Site.OpenWeb()
        $Usage = $web.GetUsageData('user', 'day')
        $PS = New-Object psobject @{
            Url = $Web.url
            Usage = $Usage
        }
        $PS.url, $PS.usage #Using $PS also wont generate proper o/p
    } | Export-Csv out.csv -Notype 
Above code won't work. So I just want to get site url in each line of same output. We don't need to use a different function.
  • Edited by mohit.goyal Wednesday, July 15, 2015 6:07 PM
Free Windows Admin Tool Kit Click here and download it now
July 15th, 2015 6:03pm

Perfect !!! You made my day...thanks so much. :) I have lot to learn from you...
July 16th, 2015 3:13am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics