Solved

Errors when running PowerShell script

Posted on 2011-02-11
9
5,171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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