All About SharePoint

Liedong(Ken) Zheng,SharePoint Leader at SIMPLOT

Posts Tagged ‘PowerShell’

The ‘Microsoft.Jet.OLEDB.4.0′ provider is not registered on the local machine.

Posted by ken zheng on November 21, 2011

I am using Win 7 X64 bit operation system, when I run the PowerShell script, I got the error. In Fact, the OLEDB dll has been installed and if you search “Msjet40.dll”. Here is the link for how to determine the Jet 4.0.

Because Jet 4.0 only have 32 bit version, so to use the object. You need to run your program on X86 bit.

image

Posted in PowerShell | Tagged: | 1 Comment »

I Deleted A Site Collection in Production Sever

Posted by ken zheng on July 28, 2011

In one early morning, I am doing multi tasks. I think I am in the sub site but actually I am in the top level. When I deleted that site, some calls coming in.

Fortunately, there is PowerShell cmd you can restore deleted site collection.This cmdlet was introduced in Microsoft SharePoint Server 2010 with Service Pack 1 (SP1) and Microsoft SharePoint Foundation 2010 with Service Pack 1 (SP1).

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

Run Use Get-SPDeletedSite to find Identity (GUID) then

Restore-SPDeletedSite -Identity 610857cb-8414-4a89-8bf3-ad3628f6c86c

All deleted sub site can be retrieved from Site Recycle Bin!

Click the Site Collection Recycle Bin (Site Actions > Site Settings > Recycle Bin) under Site Collection Administration:

Select “Deleted from end user Recycle Bin”

Select the checkbox next to the site to restore and click “Restore Selection” from the menu above. Once completed, navigate to the newly restore site:

 

Thanks Microsoft for saving my job!

Posted in Sharepoint | Tagged: , , | Leave a Comment »

Import-SPWeb : Cannot import site.

Posted by ken zheng on June 28, 2011

Error:
FatalError: Cannot import site. The exported site is based on the template STS#0 but the destination site is based on the template SPS#0. You can import sites only into sites that are based on same template as the exported site.

Import-SPWeb : Cannot import site. The exported site is based on the template STS#0 but the destination site is based on the template STS#1. You can import sites only into sites that are based on same template as the exported site.

Issue:-
In SharePoint 2010 when we use Powershell Commands to Export / Import site collections we get Site Template mismatch errors as now SP 2010 needs to have the same template for the site while importing which was used on the site exported.
In such scenarios when you have migrated from SPS 2003 -> MOSS 2007 -> SP 2010 there will be many old Site templates which are now Obsolete in SP 2010 and not available, due to which it will be very difficult to Import the site.

Resolution / Workaround:-
1. Central Admin -> Application Management -> Create Site Collection
2. During Template selection go to ‘Custom’ Tab -> Select Template Later…..
3. Run the Import-SpWeb command now and it should import the site collection without any issues.
As we are not selecting any template while creating a Site Collection, it automatically pics up the template which is using while importing the Site Collection.

Posted in PowerShell, Sharepoint | Tagged: , , | 1 Comment »

The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered

Posted by ken zheng on June 2, 2011

Problem

When you run powershell on your server you get the following error:

The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered.

clip_image001

Solution

Make sure the logged in user has rights to the SharePoint_Configuration database

    • SharePoint_Shell_Access
    • DB_owner (try reducing the privileges after you get it working in order to support “least privileges”

clip_image002

Posted in PowerShell, Sharepoint | Tagged: | Leave a Comment »

Power Shell Script to Check HREF ULR in InfoPath form

Posted by ken zheng on December 8, 2010

We have a system which contains thousands of InfoPath forms, the template files are in the SharePoint site. And some forms cannot be opened after some users editing because of the wrong InfoPath Cache. So I wrote the little scrip to loop all the forms and get HREF string to check if the url is valid.

Param($Path = "D:\Temp",[switch]$verbose)

Write-Host

$fc = new-object -com scripting.filesystemobject
$folder = $fc.getfolder($path)
$strFinder = "http.*xsn"
$logfile = "D:\Temp\NPILog.txt"

function ProcessFiles([string]$folderpath) {

Write-host " + Processing all Forms from: $folderpath"
$Forms = dir $folderpath "*.xml"

		Write-host "  + $($Forms.count) Forms found"
		Write-host " + Processing all $($Forms.count) Forms located at: $folderpath" -fore White

			foreach($Form in $Forms)
			{
				Write-Host "  + +	Processing Form: $($Form.Name)" -fore White
				Find-String  $strFinder $Form.FullName
			}	
	
}

function Find-String($find, $path)
{
    $url_values = New-Object System.Collections.ArrayList
	$i = 0
	echo "Finding string `"$find`" in file contents and file names of path: $path"
	ls $path | select-string $find -list |% { echo "Processing contents of $($_.Path)"; (get-content $_.Path) |% { [regex]::matches($_, $find) |%{$url_values.Add($_.value); $i++}} }
    echo $url_values[0]
	if(Check-URL $url_values[0])
	{
		Write-Host "$($path) href is fine"
	}
	else
	{
		Write-Host "$($path) href is wrong" -foregroundcolor red -backgroundcolor yellow
		Add-Content $logfile "$($path) href is wrong, checked at $(Get-Date)"

	}
}

function Check-URL($url)
{
	$urlIsValid = $false
	try
	{
		$request = [System.Net.WebRequest]::Create($url)
		$request.Credentials = [System.Net.CredentialCache]::DefaultCredentials;
		$request.Method = 'HEAD'
		$response = $request.GetResponse()
		$httpStatus = $response.StatusCode
		$urlIsValid = ($httpStatus -eq 'OK')
		$tryError = $null
		$response.Close()
	}
	catch [System.Exception] {
		$httpStatus = $null
		$tryError = $_.Exception
		$urlIsValid = $false;
	}
    
	return $urlIsValid
}

Write-Host "$($folder.ShortName) "
Write-Host "  + Total Folders: $($folder.SubFolders.Count)"
foreach ($i in $folder.SubFolders) {
	Write-Host "  + Folder Name: $($i.ShortName)"
	ProcessFiles($i.Path)
}

Posted in PowerShell | Tagged: , | Leave a Comment »

PowerShell Replace Script

Posted by ken zheng on November 17, 2010

Below is the script that loop folder to replace the string in all XML files.

The string I need to place is the href link in InfoPath form. So it look like

href="http://ppp/ppp/template.xsn"

 

So I use the way to replace string in between the characters

'bleeeh' -replace '(bl).+(h)','$1cc$2' 

 

This will print out: blcch

The whole script is look like:

Param($Path = "D:\Temp",[switch]$verbose)

Write-Host

$fc = new-object -com scripting.filesystemobject
$folder = $fc.getfolder($path)
$strFinder = '(href="http://).+(template.xsn)'

function ProcessFiles([string]$folderpath) {

Write-host " + Processing all Forms from: $folderpath"
$Forms = dir $folderpath "*.xml"

Write-host "  + $($Forms.count) Forms found"
Write-host " + Processing all $($Forms.count) Forms located at: $folderpath" -fore White

	foreach($Form in $Forms)
	{
		$strReplace ="$1XXX$2"
		Write-Host "  + +	Processing Form: $($Form.Name)" -fore White
		if($Form.Name.Contains("IPP")){$strReplace ='$1IPP$2'}
		elseif($Form.Name.Contains("EDP")){$strReplace ='$1EDP$2'}
		Replace-String  $strFinder $strReplace $Form.FullName
	}	
}

function Replace-String($find, $replace, $path)
{
	echo "Replacing string `"$find`" with string `"$replace`" in file contents and file names of path: $path"
	ls $path | select-string $find -list |% { echo "Processing contents of $($_.Path)"; (get-content $_.Path) |% { $_ -replace $find, $replace } | set-content $_.Path -Force }
}

Write-Host "$($folder.ShortName) "
Write-Host "  + Total Folders: $($folder.SubFolders.Count)"
foreach ($i in $folder.SubFolders) {
	Write-Host "  + Folder Name: $($i.ShortName)"
	ProcessFiles($i.Path)
}

 

Love PowerShell

Posted in PowerShell | Tagged: | Leave a Comment »

Use PowerShell Script as Schedule Task

Posted by ken zheng on October 8, 2010

Just create a schedule task as usual and put below command in

C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe –File 
S:\chkzheng\PowerShellLibrary\Get-SPSite.ps1 -url http://chisps71

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Posted in PowerShell | Tagged: | Leave a Comment »

Deploy SharePoint 2010 Solution

Posted by ken zheng on September 15, 2010

If you use SharePoint template at visual studio 2010, it automaticlly create a .wsp package in the bin fold.
Also I found this article “Using Visual Studio 2010 SharePoint Templates to deploy a web part in SharePoint 2007

Below is some commands from Patrick’s SharePoint Blog

You could include this script in your scripts to be run first, or just include the command that registers the snapin:

Add-PSSnapin Microsoft.SharePoint.PowerShell

Working with Solutions

In the ‘old’ days (let us not forget that the stsadm is still there and we have a lot of SharePoint 2007 installations across the globe), the following stsadm command could be used to add a SharePoint solution to SharePoint:

stsadm –o addsolution –filename “D:\Deploy\MySharePointSolution.wsp“

We used the following command to deploy the solution once installed to a specific web application:

stsadm –o deploysolution –name MySharePointSolution.wsp –url http://myspwebappp –allowgacdeployment –immediate

If we would upgrade an existing solution, we would use the following:

stsadm –o upgradesolution –name MySharePointSolution.wsp –filename “D:\Deploy\MySharePointSolution.wsp” -immediate

And finally, we used the following commands to retract and delete a specific solution from the web application:

stsadm –o retractsolution –name MySharePointSolution.wsp –url http://myspwebapp –immediate
stsadm –o deletesolution –name MySharePointSolution.wsp

Now, let us see how we could do above operations with PowerShell. For this, we use the following PowerShell commands:

Add-SPSolution “D:\Deploy\MySharePointSolution.wsp“
Install-SPSolution –Identity MySharePointSolution.wsp –WebApplication http://myspwebapp –GACDeployment

If you would like to add the solution as sandboxed, you would use the Install-SPUserSolution command instead. To upgrade a solution, we specify which solution is to be updated and with which new solution file:

Update-SPSolution –Identity MySharePointSolution.wsp –LiteralPath “D:\Deploy\MySharePointSolution.wsp” –GacDeployment

To retract and remove a solution, we use the following commands:

Uninstall-SPSolution –Identity MySharePointSolution.wsp –WebApplication http://myspwebapp
Remove-SPSolution–Identity MySharePointSolution.wsp
Working with features

Similarly, commands exist for working with features. The stsadm equivalents:

stsadm –o activatefeature –name MyFeatureName –url http://myspwebapp
stsadm –o deactivatefeature –name MyFeatureName –url http://myspwebapp

Needless to say, there are easy equivalents in PowerShell:

Enable-SPFeature –Identity MyFeatureNameOrGuid –url http://myspwebapp
Disable-SPFeature –Identity MyFeatureNameOrGuid –url http://myspwebapp

Posted in PowerShell, Sharepoint | Tagged: , | 2 Comments »

Sharepoint 2010 Install Scripts‏

Posted by ken zheng on September 15, 2010

Here are the links for PowerShell scripts which install SharePoint 2010 Farm
Automated SharePoint 2010 Powershell-based installation script: http://autospinstaller.codeplex.com/

Install SharePoint Server 2010 by using Windows PowerShell

Scripted deployment reference (SharePoint Server 2010)

Posted in Sharepoint | Tagged: , | 1 Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 28 other followers