Wednesday, March 13, 2013

SharePoint 2010 / PowerShell - How to get ALL of the site collection information into CSV

Hi Everyone, long time no speak...

After Googling and Googling more I still did not find a script that would do what I wanted. So I wrote mine from bits and pieces I gathered from various places that I cannot remember :)

Sorry for anyone who recognises their code and not been mentioned, I thank you warmly anyway.

The following script will loop through ALL the site collections of a SharePoint 2010 Farm and output some key fields to a CSV file


001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
#Add SharePoint PowerShell SnapIn if not already added
 if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue-eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

cls;

# SET THE EXPORT FILEPATH
$today = (Get-Date -Format yyyy-MM-dd-hh-mm)
$exportFilePath = "C:\Temp\SP2010FarmQuotaOutput_" + $today + ".csv";

$t = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.QuotaTemplates

$tFound = $false

# Get a handle on ALL site collection...
$allSiteCols = Get-SPWebApplication | %{$_.Sites} | Get-SPSite -Limit ALL

$myData = @()

foreach ($site in $allSiteCols) {

try {

# Find what Quota is used for this site col...
foreach($qt in $t){
if($qt.QuotaId -eq [int](($site.Quota).QuotaID)){
$siteQuota = $qt.Name;
$tFound = $true
}
}

if($tFound -eq $false){
$siteQuota = "No Template Applied"
}

# Get a handle on the Root Web...
$web = $site.Rootweb;

# Get the owner if possible
$siteOwner = "";
$siteDBName = "";
$rootWebLastModifiedDate = "";
$siteUrl = "";
$siteOwnerName = "";
$siteOwnerEmail = "";

$siteOwner = $site.Owner;
$siteDBName = $site.ContentDatabase.Name;
$rootWebLastModifiedDate = $web.LastItemModifiedDate.ToShortDateString();
$siteUrl = $site.Url.ToString();

if ($siteOwner -ne $null) {
$siteOwnerName = $Site.Owner.Name;
$siteOwnerEmail = $Site.Owner.Email;
}

# Do some calculations around storage...
if ($site.Quota.StorageMaximumLevel -gt 0) {[int]$maxStorage = $site.Quota.StorageMaximumLevel / 1MBelse {$maxStorage = "0"};
if ($site.Quota.StorageWarningLevel -gt 0) {[int]$warningStorage = $site.Quota.StorageWarningLevel / 1MBelse {$warningStorage = "0"};
if ($site.Usage.Storage -gt 0) {[int]$storageUsed = $site.Usage.Storage / 1MB};
if ($storageUsed -gt 0 -and $maxStorage -gt 0) {[int]$siteQuotaUsed = $storageUsed / $maxStorage * 100else {$SiteQuotaUsed = "0"};

# Compile each line...
$myData += New-Object -TypeName PSObject -Property @{
Url = $siteUrl
SiteCollectionOwner = $siteOwnerName
SiteCollectionOwnerEmail = $siteOwnerEmail
RootWebLastModifiedDate = $rootWebLastModifiedDate.ToString()
ContentDBName = $siteDBName
StorageUsedMB = $storageUsed
StorageUsedPercent = $siteQuotaUsed
StorageAvailableWarningMB = $warningStorage
StorageAvailableMaximumMB = $maxStorage
SandboxedResourcePointsWarning = ($site.Quota).UserCodeWarningLevel
SandboxedResourcePointsMaximum = ($site.Quota).UserCodeMaximumLevel
QuotaName = $siteQuota
                } | Select-Object Url,
SiteCollectionOwner,
SiteCollectionOwnerEmail,
RootWebLastModifiedDate,
ContentDBName,
StorageUsedMB,
StorageUsedPercent,
StorageAvailableWarningMB,
StorageAvailableMaximumMB,
SandboxedResourcePointsWarning,
SandboxedResourcePointsMaximum,
QuotaName

# Reset the "found flag"
$tFound = $false;
}

catch [System.Exception] {
write-host 'EXCEPTION: ' + $error[0];
}

finally {
if ($site -ne $null) {$site.Dispose()};
}
}

# Export the data to CSV format
$myData | Export-Csv $exportFilePath -Delimiter "," -NoTypeInformation

2 comments:

Anonymous said...

I've been browsing on-line greater than 3 hours
today, yet I never discovered any attention-grabbing article like yours.
It's pretty value enough for me. In my opinion, if all site owners and bloggers made just right content as you did,
the net will likely be a lot more useful than ever before.


My web page - Hearthstone Heroes of Warcraft iOS download

Unknown said...

Thank you very much for the feedback I'm glad it helped you!