Search This Blog

Showing posts with label Power Shell. Show all posts
Showing posts with label Power Shell. Show all posts

Wednesday, December 19, 2018

SharePoint- Get All DBName and WebApplication using PowerShell




Step : Copy and Save .PS1 run the powershell


# add the snapin so executed directly from the standard PowerShell console
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

# Get the current script execution path so our CSV file gets saved back there
[string]$curloc = get-location
out-file -filepath "$curloc\DBSites.csv"

# Create the empty CSV array
$arrCSV = @()
# Get an object of all of the Web Applications in the Farm
$webapps = get-spwebapplication

# Loop through each Web App

foreach ($webapp in $webapps)
{   
    # Loop through each database in the Web Application
    foreach ($database in $webapp.contentdatabases)
    {
       # Create the empty record object and start populating it with information about the current Web Application and database
       $objDB = "" | select DBName,DBStatus,WebApplication
       $objDB.DBName = $database.name
       $objDB.DBStatus = $database.status
       $objDB.WebApplication = $webapp.URL

       # Add the new record as a new node in the CSV array
       $arrCSV += $objDB 
    }
    # Now that we’ve added all of the records to the array we need to write the CSV array out to the file system
    $arrCSV | Export-Csv "$curloc\DBSites.csv" -NoTypeInformation
}




ref: https://sharepoint.rackspace.com/how-to-start-using-powershell-with-sharepoint

Wednesday, May 16, 2018

SharePoint - download all document library files and metadata Using PowerShell


Reference : http://www.nikcraik.ca/sharepoint-powershell-script-to-extract-all-documents-and-their-versions/

# This script will extract all of the documents and their versions from a site. It will also
# download all of the list data and document library metadata as a CSV file.

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
#
# $destination: Where the files will be downloaded to
# $webUrl: The URL of the website containing the document library for download
# $listUrl: The URL of the document library to download

#Where to Download the files to. Sub-folders will be created for the documents and lists, respectively.
$destination = "C:\Export"

#The site to extract from. Make sure there is no trailing slash.
$site = "http://win-2016/"
$docName ="Documents";
# Function: HTTPDownloadFile
# Description: Downloads a file using webclient
# Variables
# $ServerFileLocation: Where the source file is located on the web
# $DownloadPath: The destination to download to

function HTTPDownloadFile($ServerFileLocation, $DownloadPath)
{
       $webclient = New-Object System.Net.WebClient
       $webClient.UseDefaultCredentials = $true
       $webclient.DownloadFile($ServerFileLocation,$DownloadPath)
}

function DownloadMetadata($sourceweb, $metadatadestination)
{
       Write-Host "Creating Lists and Metadata"
       $sourceSPweb = Get-SPWeb -Identity $sourceweb
       $metadataFolder = $destination+"\"+$sourceSPweb.Title+" Lists and Metadata"
       $createMetaDataFolder = New-Item $metadataFolder -type directory
       $metadatadestination = $metadataFolder

       foreach($list in $sourceSPweb.Lists)
       {
        if($list.Title -eq $docName)   #remove this condition you can get all Site Document and list metadata
               {
                      Write-Host "Exporting List MetaData: " $list.Title
                      $ListItems = $list.Items
                      $Listlocation = $metadatadestination+"\"+$list.Title+".csv"
                      $ListItems | Select * | Export-Csv $Listlocation  -Force
         }
       }
}

# Function: GetFileVersions
# Description: Downloads all versions of every file in a document library
# Variables
# $WebURL: The URL of the website that contains the document library
# $DocLibURL: The location of the document Library in the site
# $DownloadLocation: The path to download the files to

function GetFileVersions($file)
{
       foreach($version in $file.Versions)
       {
              #Add version label to file in format: [Filename]_v[version#].[extension]
              $filesplit = $file.Name.split(".")
              $fullname = $filesplit[0]
              $fileext = $filesplit[1]
              $FullFileName = $fullname+"_v"+$version.VersionLabel+"."+$fileext                

              #Can't create an SPFile object from historical versions, but CAN download via HTTP
              #Create the full File URL using the Website URL and version's URL
              $fileURL = $webUrl+"/"+$version.Url

              #Full Download path including filename
              $DownloadPath = $destinationfolder+"\"+$FullFileName

              #Download the file from the version's URL, download to the $DownloadPath location
              HTTPDownloadFile "$fileURL" "$DownloadPath"
       }
}

# Function: DownloadDocLib
# Description: Downloads a document library's files; called GetGileVersions to download versions.
# Credit
# Used Varun Malhotra's script to download a document library
# as a starting point: http://blogs.msdn.com/b/varun_malhotra/archive/2012/02/13/10265370.aspx
# Variables
# $folderUrl: The Document Library to Download
# $DownloadPath: The destination to download to
function DownloadDocLib($folderUrl)
{
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files)
       {
        #Ensure destination directory
              $destinationfolder = $destination + "\" + $folder.Url
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory
        }

        #Download file
        $binary = $file.OpenBinary()
        $stream = New-Object System.IO.FileStream($destinationfolder + "\" + $file.Name), Create
        $writer = New-Object System.IO.BinaryWriter($stream)
        $writer.write($binary)
        $writer.Close()

              #Download file versions. If you don't need versions, comment the line below.
              GetFileVersions $file
       }
}

# Function: DownloadSite
# Description: Calls DownloadDocLib recursiveley to download all document libraries in a site.
# Variables
# $webUrl: The URL of the site to download all document libraries
function DownloadSite($webUrl)
{
       $web = Get-SPWeb -Identity $webUrl

       #Create a folder using the site's name
       $siteFolder = $destination + "\" +$web.Title+" Documents"
       $createSiteFolder = New-Item $siteFolder -type directory
       $destination = $siteFolder

       foreach($list in $web.Lists)
       {
              if($list.BaseType -eq "DocumentLibrary")  
              {
            if($list.Title -eq $docName)   #remove this condition you can get all Site Document and list
                  {
                         Write-Host "Downloading Document Library: " $list.Title
                         $listUrl = $web.Url +"/"+ $list.RootFolder.Url
                         #Download root files
                         DownloadDocLib $list.RootFolder.Url
                         #Download files in folders
                         foreach ($folder in $list.Folders)
                         {
                         DownloadDocLib $folder.Url
                         }
            }
              }
       }
}

#Download Site Documents + Versions
DownloadSite "$site"

#Download Site Lists and Document Library Metadata
DownloadMetadata $site $destination



Monday, April 2, 2018

Get all List/Document/Folder Level Permission - SharePoint PowerShell


# This script gets permissions for all users in a web application on all objects (web application > site collection > web > list/library > item)


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function GetUserAccessReport($WebAppURL, $FileUrl)
{
       Write-Host "Generating permission report..."

       #Get All Site Collections of the WebApp
       $SiteCollections = Get-SPSite -WebApplication $WebAppURL -Limit All

       #Write CSV- TAB Separated File) Header
       "URL`tSite/List/Folder/Item`tTitle/Name`tPermissionType`tPermissions `tLoginName" | out-file $FileUrl

       #Check Web Application Policies
       $WebApp= Get-SPWebApplication $WebAppURL
       #Loop through all site collections
       foreach($Site in $SiteCollections)
    {
         #Check Whether the Search User is a Site Collection Administrator
         foreach($SiteCollAdmin in $Site.RootWeb.SiteAdministrators)
      {
                     "$($Site.RootWeb.Url)`tSite`t$($Site.RootWeb.Title)`tSite Collection Administrator`tSite Collection Administrator`t$($SiteCollAdmin.DisplayName)" | Out-File $FileUrl -Append
              }
 
          #Loop throuh all Sub Sites
       foreach($Web in $Site.AllWebs)
       {     
                     if($Web.HasUniqueRoleAssignments -eq $True)
                     {
                           #Get all the users granted permissions to the list
                           foreach($WebRoleAssignment in $Web.RoleAssignments )
                           {
                                  #Is it a User Account?
                                  if($WebRoleAssignment.Member.userlogin)   
                                  {
                                         #Get the Permissions assigned to user
                                         $WebUserPermissions=@()
                                         foreach ($RoleDefinition  in $WebRoleAssignment.RoleDefinitionBindings)
                                         {
                                                $WebUserPermissions += $RoleDefinition.Name +";"
                                         }
                                        
                                         #Send the Data to Log file
                                         "$($Web.Url)`tSite`t$($Web.Title)`tDirect Permission`t$($WebUserPermissions) `t$($WebRoleAssignment.Member.DisplayName)" | Out-File $FileUrl -Append
                                  }
                                  #Its a SharePoint Group, So search inside the group and check if the user is member of that group
                                  else 
                                  {
                                         foreach($user in $WebRoleAssignment.member.users)
                                         {
                                                #Get the Group's Permissions on site
                                                $WebGroupPermissions=@()
                                                foreach ($RoleDefinition  in $WebRoleAssignment.RoleDefinitionBindings)
                                                {
                                                       $WebGroupPermissions += $RoleDefinition.Name +";"
                                                }
                                               
                                                #Send the Data to Log file
                                                "$($Web.Url)`tSite`t$($Web.Title)`tMember of $($WebRoleAssignment.Member.Name) Group`t$($WebGroupPermissions)`t$($user.DisplayName)" | Out-File $FileUrl -Append
                                         }
                                  }
                           }
                     }
                          
                     #********  Check Lists, Folders, and Items with Unique Permissions ********/
                     foreach($List in $Web.lists)
                     { Write-Host "Checking List "$List.Title" level permissions..." -ForegroundColor Green
                           if($List.HasUniqueRoleAssignments -eq $True -and ($List.Hidden -eq $false))
                           {
                                  #Get all the users granted permissions to the list
                                  foreach($ListRoleAssignment in $List.RoleAssignments )
                                  {
                                         #Is it a User Account?
                                         if($ListRoleAssignment.Member.userlogin)   
                                         {
                                                #Get the Permissions assigned to user
                                                $ListUserPermissions=@()
                                                foreach ($RoleDefinition  in $ListRoleAssignment.RoleDefinitionBindings)
                                                {
                                                       $ListUserPermissions += $RoleDefinition.Name +";"
                                                }
                                               
                                                #Send the Data to Log file
                                                "$($List.ParentWeb.Url)/$($List.RootFolder.Url)`tList`t$($List.Title)`tDirect Permission`t$($ListUserPermissions) `t$($ListRoleAssignment.Member.DisplayName)" | Out-File $FileUrl -Append
                                         }
                                         #Its a SharePoint Group, So search inside the group and check if the user is member of that group
                                         else 
                                         {
                                                foreach($user in $ListRoleAssignment.member.users)
                                                {
                                                       #Get the Group's Permissions on site
                                                       $ListGroupPermissions=@()
                                                       foreach ($RoleDefinition  in $ListRoleAssignment.RoleDefinitionBindings)
                                                       {
                                                              $ListGroupPermissions += $RoleDefinition.Name +";"
                                                       }
                                                      
                                                       #Send the Data to Log file
                                                       "$($List.ParentWeb.Url)/$($List.RootFolder.Url)`tList`t$($List.Title)`tMember of $($ListRoleAssignment.Member.Name) Group`t$($ListGroupPermissions)`t$($user.DisplayName)" | Out-File $FileUrl -Append
                                                }
                                         }     
                                  }
                           }
                          
                           #Get Folder level permissions
                           foreach($Folder in $List.folders)
                           {
                    Write-Host "Checking Folder "$Folder.Name" level permissions..." -ForegroundColor Magenta
                                  if($Folder.HasUniqueRoleAssignments -eq $True)
                                  {
                                         #Get all the users granted permissions to the folder
                                         foreach($FolderRoleAssignment in $Folder.RoleAssignments )
                                         {
                                                #Is it a User Account?
                                                if($FolderRoleAssignment.Member.userlogin)   
                                                {
                                                       #Get the Permissions assigned to user
                                                       $FolderUserPermissions=@()
                                                       foreach ($RoleDefinition  in $FolderRoleAssignment.RoleDefinitionBindings)
                                                       {
                                                              $FolderUserPermissions += $RoleDefinition.Name +";"
                                                       }
                                                      
                                                       #Send the Data to Log file
                                                       "$($Folder.Web.Url)/$($Folder.Url)`tFolder`t$($Folder.Name)`tDirect Permission`t$($FolderUserPermissions) `t$($FolderRoleAssignment.Member.DisplayName)" | Out-File $FileUrl -Append
                                                }
                        #Is it a domain Account?
                                                if($FolderRoleAssignment.Member.IsDo)   
                                                {
                                                       #Get the Permissions assigned to user
                                                       $FolderUserPermissions=@()
                                                       foreach ($RoleDefinition  in $FolderRoleAssignment.RoleDefinitionBindings)
                                                       {
                                                              $FolderUserPermissions += $RoleDefinition.Name +";"
                                                       }
                                                      
                                                       #Send the Data to Log file
                                                       "$($Folder.Web.Url)/$($Folder.Url)`tFolder`t$($Folder.Title)`tDirect Permission`t$($FolderUserPermissions) `t$($FolderRoleAssignment.Member.DisplayName)" | Out-File $FileUrl -Append
                                                }
                                                #Its a SharePoint Group, So search inside the group and check if the user is member of that group
                                                else 
                                                {
                                                       foreach($user in $FolderRoleAssignment.member.users)
                                                       {
                                                              #Get the Group's Permissions on site
                                                              $FolderGroupPermissions=@()
                                                              foreach ($RoleDefinition  in $FolderRoleAssignment.RoleDefinitionBindings)
                                                              {
                                                                     $FolderGroupPermissions += $RoleDefinition.Name +";"
                                                              }
                                                             
                                                              #Send the Data to Log file
                                                              "$($Folder.Web.Url)/$($Folder.Url)`tFolder`t$($Folder.Title)`tMember of $($FolderRoleAssignment.Member.Name) Group`t$($FolderGroupPermissions)`t$($user.DisplayName)" | Out-File $FileUrl -Append

                                                       }
                                                }     
                                         }
                                  }
                           }
               
                     }
              }     
       }
}

#Call the function to Check User Access
GetUserAccessReport "http://win-2016" "D:\PowerShell-help\April2018\3rd\SharePoint_Permission_Report.csv"
Write-Host "Complete"