Solved

Errors when running PowerShell script

Posted on 2011-02-11
9
4,871 Views
Last Modified: 2012-05-11
I am on a Windows 2003 Standard Edition SP2 server. I have a WSS 3.0 site. I found this script to cycle through document libraries and turn versioning on and set other versioning settings.  
When running the script with PowerShell, I see these errors:

The term 'Get-SPSite' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\backups\version.ps1:2 char:19
+ $site = Get-SPSite <<<<  http://v1
    + CategoryInfo          : ObjectNotFound: (Get-SPSite:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'Get-SPWeb' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the s
pelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\backups\version.ps1:6 char:18
+ $site | Get-SPWeb <<<<  |
    + CategoryInfo          : ObjectNotFound: (Get-SPWeb:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

You cannot call a method on a null-valued expression.
At C:\backups\version.ps1:32 char:14
+ $site.Dispose <<<< ()
    + CategoryInfo          : InvalidOperation: (Dispose:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

I am very new working with WSS 3.0 with PowerShell. Any suggestions would be much appreciated.
$site = Get-SPSite http://site
$listName = "Shared Documents"

#Walk through each site in the site collection
$site | Get-SPWeb | 
ForEach-Object {

#Get the list in this site
$list = $_.Lists[$listName]

#Create a version each time you edit an item in this list (lists)
#Create major versions (document libraries)
$list.EnableVersioning = $true

#Create major and minor (draft) versions (document libraries only)
$list.EnableMinorVersions = $true

#Keep the following number of versions (lists)
#Keep the following number of major versions (document libraries)
$list.MajorVersionLimit = 7

#Keep drafts for the following number of approved versions (lists)
#Keep drafts for the following number of major versions (document libraries)
$list.MajorWithMinorVersionsLimit = 5

#Update the list
$list.Update()
}

#Dispose of the site object
$site.Dispose()

Open in new window

0
Comment
Question by:gtrapp
9 Comments
 
LVL 8

Expert Comment

by:jwarnken
ID: 34876557
get-SpSite is not a built in command for powershell

it looks like the script you are working with was based off of the functions found here http://powershell.com/cs/media/p/3329.aspx
0
 
LVL 8

Expert Comment

by:jwarnken
ID: 34876568
try this
function Get-SPSite([string]$url) {

	New-Object Microsoft.SharePoint.SPSite($url)
}

function Get-SPWeb([string]$url) {

	$SPSite = Get-SPSite $url
	return $SPSite.OpenWeb()
	$SPSite.Dispose()
}


$site = Get-SPSite http://site
$listName = "Shared Documents"

#Walk through each site in the site collection
$site | Get-SPWeb | 
ForEach-Object {

#Get the list in this site
$list = $_.Lists[$listName]

#Create a version each time you edit an item in this list (lists)
#Create major versions (document libraries)
$list.EnableVersioning = $true

#Create major and minor (draft) versions (document libraries only)
$list.EnableMinorVersions = $true

#Keep the following number of versions (lists)
#Keep the following number of major versions (document libraries)
$list.MajorVersionLimit = 7

#Keep drafts for the following number of approved versions (lists)
#Keep drafts for the following number of major versions (document libraries)
$list.MajorWithMinorVersionsLimit = 5

#Update the list
$list.Update()
}

#Dispose of the site object
$site.Dispose()

Open in new window

0
 
LVL 7

Expert Comment

by:foobarr
ID: 34891763
after your function name add this

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Add-PsSnapin Microsoft.SharePoint.PowerShell

e.g.
function RunThis()
{
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Add-PsSnapin Microsoft.SharePoint.PowerShell

...your code

}

also make sure you are running the script as an administrator (right click -> run as administrator)

what you can also do is create a batch file that will do this for you.  Make sure you run this batch file as an administrator.  The %~sp0 portion will be the path and folder of your powershell file, and the filename is the powershell filename


powershell.exe -command %~sp0filename.ps1




0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:gtrapp
ID: 34911389
I get the following error. I tried to add the Microsoft.SharePoint.PowerShell in the MMC, but its not listed. So, not sure how to add the snapin?

Error:

Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version
2.
At C:\Backups\2version.ps1:4 char:13
+ Add-PsSnapin <<<<  Microsoft.SharePoint.PowerShell
    + CategoryInfo          : InvalidArgument: (Microsoft.SharePoint.PowerShel
   l:String) [Add-PSSnapin], PSArgumentException
    + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.Ad
   dPSSnapinCommand
 

0
 

Author Comment

by:gtrapp
ID: 34920075
Do I need to register DLLs for this to work? I have WSS 3.0 and the Microsoft.SharePoint.PowerShell
snap-on works with SharePoint 2007 and MOSS 2010. Is that correct? How would I register the DLLS to make the script work?

Thank you.
0
 
LVL 16

Accepted Solution

by:
Dale Harris earned 500 total points
ID: 35027563
Gtrapp,

I'm getting the same error running with the same setup.  There's no snapin for Sharepoint that I'm aware of.  Not for 2007 that is.  2010 definitely uses the snapin.

Which also explains why everyone is telling you microsoft.sharepoint.spsite should work, but that would only be if you were running 2010.

I have tried to run Powershell on our Sharepoint server from time to time, and I've given up.  I think you should go with Sharepoint Foundation 2010.

Good luck :)

Dale Harris

0
 

Author Closing Comment

by:gtrapp
ID: 35040428
Thanks for you help. I need to go to MOSS 2010.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now