How to Schedule a Powershell Script to Run on Windows 2008

It is a little bit tricky to configure a Powershell  script to run on the task scheduler on Windows 2008.

The separation between the powershell.exe command and the arguments makes a little confusion.

The way I did it was:

  • Create a new task.
  • Define a schedule.
  • Add an action:
    • Program/script: Powershell
    • Add arguments: -command &{E:\scripts\ClearIISLogs.ps1}

Now you just need to run your script and make sure it works as expected.

 

See you,

Amadeu.

Advertisements

Retrieve a List of All Items Checked Out in a Site using Powershell

Hi there!

This is a scripts to list files which are currently checked out on a site collection. Some times users check files out and forget to check them back in. It was based on a script found at http://www.thorntontechnical.com/tech/powershell-tech/powershell-tip-find-all-checked-out-files.

$webAppURL = "http://yourwebapplication.com" 

function ReportCheckedOutItems() {
	$webapp = Get-SPWebApplication $webAppURL

	foreach ($site in $webapp.Sites) 
	{
		write-host "Site Collection: $($site.Url)"
		$allwebs = $site.allwebs
		foreach($web in $allwebs)
		{
			$count = 0
			write-host "--Site: $($web.Url)"
			foreach ($list in ($web.Lists | ? {$_ -is [Microsoft.SharePoint.SPDocumentLibrary]})) {
				Write-Host "-----List: $($list.RootFolder.ServerRelativeUrl)..."
				foreach ($item in $list.CheckedOutFiles) 
				{
					if (!$item.Url.EndsWith(".aspx")) 
					{ 
						continue 
					}
					write-host "File: $($item.Url) - checked out by $($item.CheckedOutBy)" -ForeGroundColor Red
					$count++
				}
				foreach ($item in $list.Items) 
				{
					if ($item.File.CheckOutStatus -ne "None") 
					{
						if (($list.CheckedOutFiles | where {$_.ListItemId -eq $item.ID}) -ne $null) 
						{ 
							continue 
						}
						write-host "File: $($item.Url) - checked out by $($item.CheckedOutBy)" -ForeGroundColor Red
						$count++
					}
				}
			}
			if ($count -gt 0)
			{
				write-host "Found $count checked out files for site $web" -ForeGroundColor Red
			}
		}
	}
}

ReportCheckedOutItems

 

You can download the script file here

I hope it is useful for you as it is for me.

 

See you,

Amadeu.

Set IIS Redirect Settings using Powershell

Hi.

Today I had to change the redirect setting on IIS for a site and before it has been a kind of painful process.

I don’t like doing things manually because it is very error prone and mainly when I’m dealing with Production servers errors are not very welcome.

This powershell script can change these settings for you:

import-module webAdministration

$siteName = "Your Site"
$redirectPage = "http://yourredirectpage.com/destination.aspx"

# Set the redirect
Set-WebConfiguration system.webServer/httpRedirect "IIS:\sites\$siteName" -Value @{enabled="true";destination="$redirectPage";exactDestination="true";httpResponseStatus="Found"}

In order to disable redirect you can use this script:

<pre>import-module webAdministration

$siteName = "Your Site"
$redirectPage = "http://yourredirectpage.com/destination.aspx"

# Disable the redirect
Set-WebConfiguration system.webServer/httpRedirect "IIS:\sites\$siteName" -Value @{enabled="false"}

You can get the script file here.

See you.

Powershell Script to Change Page Layout on Publishing Pages

This is an useful script to change the Page Layout for all the publishing pages for a site.

$spWeb = Get-SPWeb("http://yourwebapplication.com/yoursite")
$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($spWeb)
$pSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($spWeb.Site);

$siteLayouts = $pSite.GetPageLayouts($false)
$myLayout = $siteLayouts["/_catalogs/masterpage/yourpagelayout.aspx"]
#$myLayout
$query = New-Object Microsoft.SharePoint.SPQuery
$query.ViewAttributes = "Scope='RecursiveAll'"

$pages = $pWeb.GetPublishingPages($query)

foreach ($page in $pages)
{
	if ($page.ContentType.Name -eq "Folder")
	{
		continue
	}

	$page.Layout = $myLayout
	$page.update()
	$page.ListItem.File.Publish("")
	$page.ListItem.File.Approve("")
}
$spWeb.Dispose()

Get the script file here.

Have fun!!!

See you,

Amadeu.

Set Access Request Email on All Sub-Site of a Web Application

Hi all!

Follow a Powershell script to change the Access Request email for all sites in a we b application:

$webapp = Get-SPWebApplication "http://yourwebapplication.com"
$currentEmail = "current.email@company.com";
$newEmail = "new.email@company.com";

foreach($site in $webapp.Sites)
{
   foreach($web in $site.AllWebs)
   {
     $url = $web.url
	 Write-host $url
     if (!$web.HasUniquePerm)
     {
            Write-Host "Access Request Settings is inherted from parent."
     }
       elseif($web.RequestAccessEnabled)
       {
			Write-Host "Access Request Settings is enabled."
			write-host $web.RequestAccessEmail
	        if ($web.RequestAccessEmail -eq $currentEmail)
			{
				Write-Host "Email needs to be updated."
				$web.RequestAccessEmail = $newEmail
				$web.Update()
				Write-Host "Email changed successfully!"
			}

       }
       else
      {
            Write-Host "Access Request Settings not enabled."
      }
   }
}

 

Hope this helps you!

 

See you,
Amadeu.

Removing a Web Part from Pages Using Powershell

Hi all!

Today I’m just posting this useful Powershell script to remove a web part from all the pages in a specific site.

It goes through all the libraries you define and looks for instances of the web part and removes to ones it finds.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.WebPartPages")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")

$wpName = "Your_Web_Part_Name";
$checkedOut = @()
$site = new-object Microsoft.SharePoint.SPSite -argumentList "http://yourwebapplicationurl.com";

$webApplication = $site.WebApplication;

foreach($spsite in $webApplication.Sites)
{
	$spweb = $spsite.OpenWeb("your web site name")
	$url = $spweb.Url;

	write-output "Processing $url"

	# libraries to check for the web part on pages
	$libraries = @("Pages");

	foreach($lib in $libraries)
	{
		$list=$spweb.Lists[$lib]
		$title = $list.Title
		foreach($item in $list.Items)
		{
			$WebPageUrl = $item.Url
			write-output "Processing page $WebPageUrl"

			$spWpManager = $spweb.GetLimitedWebPartManager($WebPageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
			$webparts = @()

			if ($item.File.CheckOutType -ne "None")
			{
				$checkedOut = $checkedOut + $WebPageUrl + ","
			}

			foreach($spwebpart in $spWpManager.Webparts)
			{
				if($spwebpart.Title -eq $wpName)
				{
					$webparts = $webparts + $spwebpart.ID
				}
			}

			foreach($webpartId in $webparts)
			{
				$spWpManager.DeleteWebPart($spWpManager.Webparts[$webpartId])
			}
		}
	}
	write-output "$url has been processed."
	write-output "The following pages were checked out and could not be processed: $checkedOut"

	$spweb.Update();
	$spweb.Dispose();

	if($spWpManager -ne $null)
	{
		$spWpManager.Dispose();
	}
	$spsite.Dispose();
}

The code file can be found here.

See you,

Amadeu.

Configure SharePoint Blocked File Types using PowerShell

A very nice set of Powershell scripts to manage blocked file type for a web application by Ryan Dennis.

Worth to have on your toolbox:

 

See you,
Amadeu.