Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

How to get all site collections and subsites with in the web application using powershell

$
0
0

Hi,

i need to get all site collections and subsites with in the web application using powershell, below code written to generate the site url, site owner, created date and modified date.

But i am getting an error with below script.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$timestamp = get-date -format "yyyyMMdd_hhmmtt"
$filenameStart = "AllWebAppsites"
$logfile = ("{0}{1}.csv" -f $filenamestart, $timestamp)

#$header = "Type,User,Name,Web URL,Web Name,Email ID"
#$header | out-file -FilePath $logfile

$Output = $logfile #Output file  

#$webapps = Get-SPWebApplication
$SiteDataCollection = @()  # Array to store data  
#Get All site collections
$SiteCollections = Get-SPWebApplication "https://mywebapplication/" | Get-SPSite -Limit All
#Loop through all site collections
 foreach($Site in $SiteCollections)
    {
        #Loop throuh all Sub Sites
       foreach($web in $Site.AllWebs)
        {
	        #Write-Host $web.title ": " $web.URL "  Tmplate: " $web.WebTemplate
            $SiteData = New-Object PSObject   
            $SiteData | Add-Member -type NoteProperty -name "Web Application URL" -value $web.URL  
            $SiteData | Add-Member -type NoteProperty -name "Web Application Name" -value $web.title

            foreach($ownerGroup in $web.AssociatedOwnerGroup)
            {
                foreach($owner in $ownerGroup.Users)
                {
                #Send the Data to Log file
                #"$($web.url) `t $($owner)`t $($owner.DisplayName) `t $($ownerGroup)" | Out-File SiteOwners.csv -Append
                $SiteData | Add-Member -type NoteProperty -name "Owner" -value $owner
                $SiteData | Add-Member -type NoteProperty -name "DisplayName" -value $owner.DisplayName

                #$SiteUserData += $SiteData
                }
            }

            $SiteData | Add-Member -type NoteProperty -name "Date Created" -value $Site.created.toShortDateString()
            $SiteData | Add-Member -type NoteProperty -name "Last Modified" -value $Site.LastItemModifiedDate.ToShortDateString()

        }
    
        $SiteDataCollection += $SiteData  
            #Write-Host ""
    }

    $SiteDataCollection | Export-Csv -Path $Output -Force  
  
Write-Host "Successfully Completed" -ForegroundColor DarkRed

Here an error is : 

You cannot call a method on a null-valued expression.
At D:\Nagendra\Allsites.ps1:40 char:13
+             $SiteData | Add-Member -type NoteProperty -name "Date Created" -valu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At D:\Nagendra\Allsites.ps1:41 char:13
+             $SiteData | Add-Member -type NoteProperty -name "Last Modified" -val ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Please help me on this issue.



Thanking You, Nagendra.


Viewing all articles
Browse latest Browse all 7589

Trending Articles