Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Friday, February 9, 2018

Powershell to add dummy documents to SharePoint List for Testing purpose

Write-Host "Loading SharePoint Powershell Snapin"
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} 
if ($snapin -eq $null) {  Add-PSSnapin "Microsoft.SharePoint.Powershell" }

# ---- Script settings ----
$sourceDocumentPath = "C:\temp\TestDoc.docx" # Source document to spawn new documents from for the creation
$newFilenamePrefix = "TestDoc"
$newFilenameExtension = ".docx"
$numberDocsToCreate = 5000

# Settings for the destination to create documents in

$webUrl = "http://site:1111/sites/tdm3"
$docLibraryName = "Lib"
$folderPathWithinDocLibrary = "" # Leave empty e.g. "" to create documents in root folder of library otherwise specify path relative to root folder e.g. "/Testing/Folder A"

# -------------------------

#Open web and library
$web = Get-SPWeb $webUrl
$docLibrary = $web.Lists[$docLibraryName]
$docLibraryUrl = $docLibrary.RootFolder.ServerRelativeUrl
$uploadfolder = $web.getfolder($docLibraryUrl + $folderPathWithinDocLibrary)

#Open file
$file = get-item $sourceDocumentPath
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

# Create documents in SharePoint
write-host "Creating $i documents based on the file $sourceDocumentPath"

for($i=1; $i -le $numberDocsToCreate; $i++)
{
$newFilePath = $docLibraryUrl + $folderPathWithinDocLibrary + "/" + $newFilenamePrefix+$i+$newFilenameExtension
write-host "Creating document: $newFilePath ..."
$spFile = $uploadfolder.Files.Add($newFilePath, [System.IO.Stream]$fileStream, $true)
}

write-host "Completed"

#Close file stream
$fileStream.Close()

#Dispose web
$web.Dispose()

Tuesday, May 6, 2014

Discovering or Reports on the items permissions for SharePoint Library or List PowerShell script

 

Clear-Host

if((Get-PSSnapin | where{$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell;
Write-Host "Snap In Added";
}
$SiteUrl = "<site url>/sites/ags/spg/pit/pitsite";

Function GetFiles($folder)
{
# Use recursion to loop through all subfolders.
foreach ($spFolder in $folder.SubFolders)
{

if($spFolder.Name -ne 'Forms')
{
if($spFolder.Item.HasUniqueRoleAssignments)
{
$roleAssignments = $spFolder.Item.RoleAssignments;
if ($roleAssignments.Count -gt 0) {
foreach ($role in $roleAssignments){
$eachRole = $role.Member.ToString();
$allRoles += $eachRole + ";";
}
$url=$SiteUrl+"/"+$spFolder.Url
#$link = "<a href=" + $url + ">" +$url + "</a>";
$OutInfo = $spFolder.Name + "," + $url + "," + "No" + "," + $allRoles;
Add-Content -Value $OutInfo -Path $OutFile

}
}
else
{
$url=$SiteUrl+"/"+$spFolder.Url
$OutInfo = $spFolder.Name + "," + $url + "," + "Yes" + "," + "";
Add-Content -Value $OutInfo -Path $OutFile
}
}
GetFiles($spFolder)
}
}
 
$OutFile = "D:\test\Permissions.csv"
$Header = "Folder Name,Folder URL,IsInherited,Permissions"
Del $OutFile

Add-Content -Value $Header -Path $OutFile



$web = Get-SPWeb -Identity "<site url>sites/ags/spg/pit/pitsite"

$list = $web.GetList("<siteurl>/sites/ags/spg/pit/pitsite/Shared Documents")

GetFiles($list.RootFolder)

Tuesday, May 22, 2012

Restore a deleted site collection in SharePoint Server 2010 powershell script


Restore a deleted site collection in SharePoint Server 2010

In prior versions of SharePoint Server 2010, if a site collection (that is, an SPSite object) was accidentally deleted, there was not a direct way of restoring a specific deleted site collection. The only method to restore a deleted site collection was to restore the entire farm from a backup. This was costly and time consuming, and typically was not performed.
When a site collection is accidentally deleted in SharePoint Server 2010 with SP1, the deleted site collection is stored in the SPDeletedSite object, not the SPSiteobject. Therefore, to restore a deleted site collection, you must use the Restore-SPDeletedSite Windows PowerShell cmdlet or programmatically access the object model.

PowerShell script

This will restore all the deleted site the same location, if site already exists it wont, use -force to restore


if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null){

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

}


foreach($deletedSite in Get-SPDeletedSite)
{
Write-Host $ deletedSite  ;
Restore-SPDeletedSite -Identity $deletedSite 
}

happy Coding ,
Power of PowerShell