Hi All,
I am try to run a PowerShell script automatically when deploying a SharePoint Farm solution, but keep receiving error messages.
I first create a SharePoint folder called PowerShell Scripts and place my PowerShell script into it (SPDeploy.ps1).
*************
SPDeploy.ps1:
$snapin = get-pssnapin | where { $_.Name -eq "Microsoft.SharePoint.PowerShell" }
if($snapin -eq $null) {
Add-PsSnapin Microsoft.SharePoint.PowerShell
}
write-host "RESET IIS"
iisreset
write-host "DELETE DEMO SITE"
Remove-SPSite -Identity "http://mySite/Sites/Test1" -GradualDelete -Confirm:$False
write-host "CREATE DEMO SITE"
New-SPSite http://mySite/Sites/Test1 -OwnerAlias "Domain\Administrator" -Name "Contoso" -Template "SiteDefinition1#0"
write-host "FINISHED"
***************
I then add commands into the Visual Studio ‘Post-deployment Command Line’. I have tried three different commands, but I still received error messages.
Command 1)
%SystemRoot%\syswow64\WindowsPowerShell\v1.0\PowerShell.exe set-executionpolicy -executionpolicy unrestricted
%SystemRoot%\syswow64\WindowsPowerShell\v1.0\PowerShell.exe -file "$(ProjectDir)\PoweShellScripts\PSDeploy.ps1"
Command 2)
C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe set-executionpolicy -executionpolicy unrestricted
C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -file "$(ProjectDir)\PoweShellScripts\PSDeploy.ps1"
Command 3)
%windir%\System32\WindowsPowerShell\v1.0\powershell set-executionpolicy unrestricted -file "$(ProjectDir)\PoweShellScripts\PSDeploy.ps1"
%windir%\System32\WindowsPowerShell\v1.0\powershell -file "$(ProjectDir)\PoweShellScripts\PSDeploy.ps1"
While deploying the SharePoint solution I get the error message shown below:
*****************
Add-PsSnapin : The Windows PowerShell snap-in ‘Microsoft.SharePoint.PowerShell' is not installed on this computer.
At C:\Users\Administrator\Documents\Visual Studio 2013\Projects\SharePointSiteProj\SharePointSiteProj\PoweShellScripts\PSDeploy.ps1:7 char:1
+ Add-PsSnapin Microsoft.SharePoint.PowerShell
+ CategoryInfo : InvalidArgument: (Microsoft.SharePoint.PowerShell:String) [Add-PSSnapin], PSArgumentException
+ FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand
remove-spsite : The term 'remove-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:\Users\Administrator\Documents\Visual Studio 2013\Projects\SharePointSiteProj\SharePointSiteProj\PoweShellScripts\PSDeploy.ps1:21 char:1
+ remove-spsite -Identity "http://robin/Sites/Test2" -GradualDelete
-Confirm:$Fals ...
+ CategoryInfo : ObjectNotFound: (remove-spsite:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
…
*****************
I can run the same script manually from a SharePoint PowerShell console without any error message, but would like it to run automatically from the Visual Studio ‘Post-deployment Command Line’.
I hope you can help
Colin