Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

As part of a new SharePoint 2010 project I am trying to automate the build of the SharePoint site on our staging system using NAnt, something we use a lot with our C# builds.

I have created a PowerShell script that I have tested successfully:-

#Load SharePoint cmdlets
& "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG POWERSHELL\Registration\sharepoint.ps1"

# Create the site based on blank site template (STS#1)
$template = Get-SPWebTemplate "STS#1"
$date = Get-Date -format "yyyy-MM-dd HHmm"
$siteURL = "http://MY-PC/SITE " + $date
$siteName = "SITE " + $date
New-SPWeb -Url $siteURL -Template $template -Name $siteName -AddToTopNav

I then created a NAnt file with the following exec call:-

    <exec failonerror="true" program="${powershell.exe}">   
    <arg value="-noprofile" />
    <arg value="-nologo" />
    <arg value="-noninteractive" />
    <arg value="-command" />    
    <arg value=" &quot; &amp; &apos;${createsite.ps1}&apos; &quot; " />
    </exec>

Where ${powershell.exe} -> C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe and ${createsite.ps1} is the full path of my powershell script.

But when I run this I get the following error:-

 [exec] Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version
 [exec] 2.
 [exec] At C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONF
 [exec] IG\POWERSHELL\Registration\sharepoint.ps1:3 char:13
 [exec] + Add-PsSnapin <<<<  Microsoft.SharePoint.PowerShell
 [exec]     + CategoryInfo          : InvalidArgument: (Microsoft.SharePoint.PowerShel
 [exec]    l:String) [Add-PSSnapin], PSArgumentException
 [exec]     + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.Ad
 [exec]    dPSSnapinCommand

I thought this was due to my not referencing the SharePoint PowerShell snap in correctly, so changed

& "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\sharepoint.ps1"

to this

Add-PSSnapin Microsoft.SharePoint.PowerShell

with no effect.

Does anybody have any experience of running PowerShell with NAnt and have any suggestions?

Thanks.

Update

As per Micks reply, the process was indeed calling the 32 bit version of PwerShell. Turns out Microsoft redirects you from the 64 bit folder when running a 32 bit process. For now, I've created an NTFS Junction that points to the 64 bit directory for PowerShell and pointed to that in my NAnt script.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

I think the script is actually calling the 32bit version of Powershell as opposed to the 64bit version. You may have to directly code the Powershell path into the nAnt scipt.

share|improve this answer
    
hmmm...yes I can see what you mean. However, I'm definately pointing to the 64 bit version (C:\Windows\System32\WindowsPowerShell\v1.0) rather than the 32 bit (C:\Windows\SysWOW64\WindowsPowerShell\v1.0) and it's strange that the process works fine if I run the command from a DOS window, but not through NAnt. I shall do some more digging around about this though. Thanks. –  grimorde Mar 10 '11 at 14:54

Thanks for leading me down the right track.

SysNative seems to work for me, e.g. <property name ="powershell.exe" value="C:\Windows\Sysnative\WindowsPowerShell\v1.0\PowerShell.exe" />

http://practicaladmin.wordpress.com/2010/07/16/powershell-x64-and-filesystem-redirection/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.