PowerShell SharePoint User Access

Let me start this off by saying I’m new to PowerShell AND I’m sure someone could sum up my code in one line of code.

I was asked to provide a list of all users in a SharePoint Site Collection / Web App. The person asking this, was wanting to see each user, the site and list they had access to. More or less I needed to iterate through all the sites, loop on the user, loop again on the site, then loop on the lists.

So the layout I could provide was:

Site Collection
User Name
Site
List

I’m outputting the results to a file name UserAccess.txt on the C:\ drive.

[System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)

$spsite=[Microsoft.SharePoint.SPSite]("http://sharepoint/")

out-file c:\UserAccess.txt -append

foreach ($Site in $spsite.AllWebs)
{
    "Site Collection Name:  $Site" | out-file c:\UserAccess.txt -append
    foreach ($User in $Site.SiteUsers)
    {
        "--User Name: $User" | out-file c:\UserAccess.txt -append
        foreach ($Site in $spsite.AllWebs)
        {
					$SitePermissions = $Site.Permissions
					foreach($mySitePermission in $SitePermissions)
					{
						if($User.ID -eq $mySitePermission.Member.ID)
						{
                            "----Site Name: $Site" | out-file c:\UserAccess.txt -append
						}
					}

                    foreach($myList in $Site.lists)
        			{

        					$myListPermissions = $myList.Permissions
        					foreach($myListPermission in $myListPermissions)
        					{
        						if($User.ID -eq $myListPermission.Member.ID)
        						{
                                    "------List Name: $MyList" | out-file c:\UserAccess.txt -append
        						}
        					}
        			}

                 $Site.Dispose()
			}
            $Site.Dispose()
        }
    }

Let me know if you have any questions or need any help.

Client does not have permissions to send as this sender At

Error when trying to send an email from PowerShell on Windows Server 2008 R2.

Client does not have permissions to send as this sender
At …………….

FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

Make sure your IP address is added to the SMTP relay service. My overly friendly Exchange Admin was able to add this to the box.

Get Content Database Size

In SharePoint 2010 or 2007 I wasn’t able to find the size of the Content Databases.  For some reason I thought it was displayed in Central Administration, wrong.

Log into one of your SharePoint servers.
Open PowerShell and paste the below code into the PowerShell window.
Run.

[Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | out-null
$prop_Name = [Microsoft.SharePoint.Administration.SPContentDatabase].GetProperty("Name")
$prop_DiskSizeRequired = [Microsoft.SharePoint.Administration.SPContentDatabase].GetProperty("DiskSizeRequired")
$prop_Sites = [Microsoft.SharePoint.Administration.SPContentDatabase].GetProperty("Sites")
[Microsoft.SharePoint.Administration.SPFarm]::Local.Services |? {
$_.GetType().FullName -eq "Microsoft.SharePoint.Administration.SPWebService"
} |% {
$_.WebApplications |% {
$_.Name
$_.ContentDatabases |% {
$prop_Name.GetValue($_, $null)
$prop_DiskSizeRequired.GetValue($_, $null) / 1GB
}
}
}

The output will be:
Web App
Content Database Name
Content Database Size in Gigs.

You can change the output displayed size to MB by updating:
$prop_DiskSizeRequired.GetValue($_, $null) / 1GB
$prop_DiskSizeRequired.GetValue($_, $null) / 1MB

PowerShell Query Webpage SharePoint

Using PowerShell, I was looking for a way to query / search a webpage for a string / variable. If the string was found, do something.

When trying to access a SharePoint page, I needed to use the UseDefaultCredentials = $true function, otherwise I was seeing random errors.

Error example:
The remote server returned an error: (401) Unauthorized.
$web = New-Object System.Net.WebClient
$web.UseDefaultCredentials = $true

If ($web.DownloadString(“http://www.yoursite.com/taco.aspx”)| select-string “Taco”)
{
Write-Host “True”
}
Else
{
Write-Host “False”
}

Get Crawl Status Using Powershell

{Edit} I’ve found another way of doing this{/}

Way One:

I needed a way of knowing when a crawl status was set to paused.  Using a combination of Windows Task, Powershell, SharePoint List, and Workflow I was able to come up with a solution.

Process: Windows Task runs every hour (on the server with Central Administration).  This Task runs my Powershell command.  The command checks all my Content Sources for a crawl status of paused. If the status is paused, the command will write an Item to a SharePoint List.  Associated Workflow on the List then sends me an email.

Link to the completed Powershell command: Powershell Crawl Status (you will need to open the file and save it with a .ps1 extension.)

[void] [System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null

[void] [System.Reflection.Assembly]::Load("Microsoft.Office.Server, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null

[void] [System.Reflection.Assembly]::Load("Microsoft.Office.Server.Search, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null

$SITEURL = "http://win-severname"

$spsite = new-object Microsoft.SharePoint.SPSite ( $SITEURL )
$serverContext = [Microsoft.Office.Server.ServerContext]::Default
$context = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($serverContext)
$sspcontent = new-object Microsoft.Office.Server.Search.Administration.Content($context)
$sspContentSources = $sspcontent.ContentSources

[int]$count

$count=0

foreach ($cs in $sspContentSources)

{
if ($cs.CrawlStatus -eq [Microsoft.Office.Server.Search.Administration.CrawlStatus]::Paused)
{

$count++

}
}

if ($count -gt 0)

{

$spweb = $spsite.OpenWeb()
$splist = $spweb.Lists["ListName"];
$items=$splist.get_items() | where { $_.Title -like '*' }

if ($splist.ItemCount -gt 0)
{
$items | % { $_.Delete() }
}
$newItem = $splist.Items.Add()
$newItem["Title"] = "NewItem"
$newItem.Update()

$spweb.Dispose()
$spsite.Dispose()


}

——————————

How to run a Powershell command from a Scheduled Task.

To run a Powershell command you will need to do the following.

1.In the Actions tab:

a.Click New. The New Action dialog box appears.

2.In Settings, in Program/Script, type:

powershell.exe

3.In Add arguments, type the following:

-command “C:\Powershell Crawl Status.ps1”

4.Click OK.

http://technet.microsoft.com/en-us/library/ee649304(WS.10).aspx

Way Two and the EASY way:


$web = New-Object System.Net.WebClient
$web.UseDefaultCredentials = $true

If ($web.DownloadString(“http://craigslistSpam/ssp/admin/_layouts/listcontentsources.aspx”)| select-string “Paused” -CaseSensitive)
{
$emailFrom = "me@craigslistSpam.com"
$emailTo = "you@craigslistSpam.com"
$subject = "testing"
$body = "test email"
$smtpServer = "this can be found in Outlook"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}