PowerShell Remoting: Access Denied

Problem :

Getting access denied error when try to access $site.RootWeb.Lists over powershell remoting. User gets this error even if user has sufficient privileges to access the list.

Solution:

Run following command for the account which is used to authenticate over PS remoting to get it running:

$w = Get-SPWebApplication -Identity http://portal
$w.GrantAccessToProcessIdentity(“domain\username”)

Reference Link here.

Advertisements

Recover deleted site collection using powershell

  • Unfortunately, there is no user interface for restoring deleted site collections. Managing deleted site collections requires using PowerShell. SharePoint 2010 Service Pack 1 introduces 3 new PowerShell CmdLets for managing the Recycle Bin for site collections.

    • Get-SPDeletedSite
    • Restore-SPDeletedSite
    • Remove-SPDeletedSite

    The easiest way to get a list of all the deleted site collections is to just call Get-SPDeleteSite without any parameters. In the following example, for brevity, I have formatted the output as a table and filtered the columns displayed. In the second sample, I have added a wildcard filter to the command that can be used to easily filter sites based on relative paths.

    1178_5Once you have identified the site you want to restore, you call the Restore-SPDeletedSite. To restore one or more sites, it is easiest to just pipe the results from Get-SPDeletedSite. By default, the command will ask you to confirm the restore. To eliminate the confirmation, just add “Confirm:$true” to the end of the command.

    1178_6

    Ref: http://mssharepointtips.com/tip.asp?id=1178&page=3

     

Stringbuilder object returning result while using Append function

Problem

For appending content to stringbuilder object we use Append() as

$sb = New-Object -TypeName “System.Text.StringBuilder”;
 $sb.Append(“This is a demo”);
expected the result is
This is a demo
However the result is
                  Capacity                MaxCapacity                    Length
                  ——–                ———–                    ——
                        16                 2147483647                         11
This is a demo
Reason
Several of the methods on StringBuilder like Append IIRC, return the StringBuilder so you can call more StringBuilder methods. However the way PowerShell works is that it outputs all results (return values in the case of .NET method calls).
Solution
In this case, cast the result to [void] to ignore the return value e.g.:
[void]$sb.Append(“This is a demo”)

Powershell Script Block To List ALL/Unused/Used Content Database

===================================================
clear
#Import Localized Data
#Import-LocalizedData -BindingVariable Message
Add-PsSnapin Microsoft.SharePoint.PowerShell
#This function is used to get standard content database list for Get-OSCContentDatabase
function Export-OSCContentDatabase
{
PARAM
(
$ContentDatabases
)

$ContentDatabases | Select-Object -Property `
@{Name=”Url”;Expression={
$_.WebApplication.Url
}},`
@{Name=”ID”;Expression={
$_.Id
}},`
@{Name=”Name”;Expression={
$_.Name
}},`
@{Name=”WebApplication”;Expression={
$_.WebApplication
}},`
@{Name=”Server”;Expression={
$_.Server
}},`
@{Name=”CurrentSiteCount”;Expression={
$_.CurrentSiteCount
}},`
@{Name=”Status”;Expression={
$_.Status
}} | Sort-Object -Property Name
}

function Get-OSCContentDatabase
{
<#
.SYNOPSIS
Get-OSCContentDatabase is an advanced function which can be used to get unused,used or all content databases.
.DESCRIPTION
Get-OSCContentDatabase is an advanced function which can be used to get unused,used or all content databases.
.PARAMETER <UsedDatabase>
List the content databases which are in use currently.
.PARAMETER <UnUsedDatabase>
List the content databases which are not in use currently.
.EXAMPLE
PS C:\Users\TestUser> Get-OSCContentDatabase
List all content databases.
Url : http://testserver:5000/
ID : caecf4a5-a618-447f-8d82-91a39bc45956
Name : TestContentDB1
WebApplication : SPWebApplication Name=ClaimWebApp – 5000
Server : TestServer\SharePoint
CurrentSiteCount : 0
Status : Online
Url : http://testserver:5000/
ID : 5361df6c-a487-4f0f-ba26-52a82dba75a6
Name : TestContentDB2
WebApplication : SPWebApplication Name=ClaimWebApp – 5000
Server : TestServer\SharePoint
CurrentSiteCount : 0
Status : Disabled
Url : http://testserver:21379/
ID : dca43cae-3dc5-4fd3-9ee6-90e1686514b3
Name : TestContentDB3
WebApplication : SPAdministrationWebApplication
Server : TestServer\SharePoint
CurrentSiteCount : 2
Status : Online
Url : http://testserver:21379/
ID : 55e0878d-2862-4f26-9481-c07aa012f9fa
Name : TestContentDB4
WebApplication : SPAdministrationWebApplication
Server : TestServer\SharePoint
CurrentSiteCount : 0
Status : Disabled
.EXAMPLE
PS C:\Users\TestUser> Get-OSCContentDatabase -UsedDatabase
List the content databases which are in use currently.
Url : http://testserver:5000/
ID : caecf4a5-a618-447f-8d82-91a39bc45956
Name : TestContentDB1
WebApplication : SPWebApplication Name=ClaimWebApp – 5000
Server : TestServer\SharePoint
CurrentSiteCount : 0
Status : Online
Url : http://testserver:21379/
ID : dca43cae-3dc5-4fd3-9ee6-90e1686514b3
Name : TestContentDB3
WebApplication : SPAdministrationWebApplication
Server : TestServer\SharePoint
CurrentSiteCount : 2
Status : Online
.EXAMPLE
PS C:\Users\TestUser> Get-OSCContentDatabase -UnUsedDatabase
List the content databases which are not in use currently.
Url : http://testserver:5000/
ID : 5361df6c-a487-4f0f-ba26-52a82dba75a6
Name : TestContentDB2
WebApplication : SPWebApplication Name=ClaimWebApp – 5000
Server : TestServer\SharePoint
CurrentSiteCount : 0
Status : Disabled
Url : http://testserver:21379/
ID : 55e0878d-2862-4f26-9481-c07aa012f9fa
Name : TestContentDB4
WebApplication : SPAdministrationWebApplication
Server : TestServer\SharePoint
CurrentSiteCount : 0
Status : Disabled
#>

[CmdletBinding(DefaultParameterSetName=”UnUsedDatabase”)]
PARAM
(
[Parameter(Mandatory=$false,Position=0,ParameterSetName=’UsedDatabase’)]
[switch]$UsedDatabase,
[Parameter(Mandatory=$false,Position=0,ParameterSetName=’UnUsedDatabase’)]
[switch]$UnUsedDatabase
)

[array]$arrContentDB = @()
try
{
Get-SPWebApplication -IncludeCentralAdministration | ForEach-Object{
$arrContentDB += $_.ContentDatabases
}
}
catch [Exception]
{
#Catch and throw the terminating exception
throw $Error[0].Exception.Message
}

#Check if content databases exist
if($arrContentDB.Count -eq 0)
{
Write-Error $Message.NoContentDB
return $null
}

$scriptContentDBOutput = @()
$scriptContentDBOutput += Export-OSCContentDatabase $arrContentDB

#List the content databases which are in use currently
if($UsedDatabase)
{
Write-Host $Message.UsedDatabase
$scriptContentDBOutput = $scriptContentDBOutput | Where-Object{$_.Status -eq “Online”}
if($scriptContentDBOutput.Count -eq 0)
{
Write-Host $Message.ZeroUsedContentDB
return $null
}
}
#List the content databases which are not in use currently
elseif($UnUsedDatabase)
{
Write-Host $Message.UnUsedDatabase
$scriptContentDBOutput = $scriptContentDBOutput | Where-Object{$_.Status -ne “Online”}
if($scriptContentDBOutput.Count -eq 0)
{
Write-Host $Message.ZeroUnusedContentDB
return $null
}
}
#List all content databases
else
{
Write-Host $Message.AllDatabase
if($scriptContentDBOutput.Count -eq 0)
{
Write-Host $Message.ZeroContentDB
return $null
}
}

$scriptContentDBOutput
}

##List Out All Content Database
Get-OSCContentDatabase
##List the content databases which are in use currently.
Get-OSCContentDatabase -UsedDatabase
##List the content databases which are not in use currently.
Get-OSCContentDatabase -UnUsedDatabase
=================================================