Errors when running PowerShell script

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

gtrappAsked:
Who is Participating?
 
Dale HarrisProfessional Services EngineerCommented:
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
 
jwarnkenCommented:
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
 
jwarnkenCommented:
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
How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

 
foobarrCommented:
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
 
gtrappAuthor Commented:
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
 
gtrappAuthor Commented:
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
 
gtrappAuthor Commented:
Thanks for you help. I need to go to MOSS 2010.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.