Solved

Errors when running PowerShell script

Posted on 2011-02-11
9
4,947 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Issue: One Windows 2008 R2 64bit server on the network unable to connect to a buffalo Device (Linkstation) with firmware version 1.56. There are a total of four servers on the network this being one of them. Troubleshooting Steps: Connect via h…
This article will help you understand what HashTables are and how to use them in PowerShell.
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

815 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

8 Experts available now in Live!

Get 1:1 Help Now