0

I have a custom PowerShell script that accept some parameters as input.

However, one of the parameters is of a type that is defined in an external assembly. That means that when I run the script from a command line with PowerShell.exe, the assembly is not loaded and the script fails.

Here is relevant parts of the script:

[CmdLetBinding()]
param(
    [Parameter(Mandatory=$true, Position=0)]
    [Microsoft.SharePoint.PowerShell.SPWebPipeBind]$Web
)

# The SharePoint snapin will load 'Microsoft.SharePoint.PowerShell'
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA 0
Write-Host "Let's dance"

And here how I fire the script (actually from a post-deployment event of a SharePoint project):

"%WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe" -file "c:\pathto\fixeditablefields.ps1" "http://myapp"

Is there a way to tell PowerShell.exe to load an assembly before running the script?

[Edit] As I'm working with SharePoint, I'm sticked to PowerShell V2

2 Answers 2

1

I'd say, no. One thing you can do is prevent the script from running if the the assembly is not present by using the The #Requires statement:

#Requires -PSSnapin Microsoft.SharePoint.PowerShell

If you don't have access to the script or cannot control it any way then i suggest to change the type of the parameter and add validations inside the function.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you help. As I said in my edit, I'm sticked to PS V2. I actually have access to the script, but I can't hard code parameters, they must come from Visual Studio macro (look at my anwser).
0

Ok, I manage to make this works.

I launch my script using :

"%WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe" -command "& {Add-PSSnapin Microsoft.SharePoint.PowerShell -EA 0; & 'c:\pathto\fixeditablefields.ps1' -Web 'http://myapp'}"

Basically, instead of running directly my script file, I run a script block which consists in running two commands : Add-PSSnapin, which loads my missing assembly, then execute the script with the & c:\pathto\script.ps1 construct.

For future readers, my PostDeployment event is exactly :

"%WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe" -command "& {Add-PSSnapin Microsoft.SharePoint.PowerShell -EA 0; & '$(SolutionDir)fixeditablefields.ps1' -Web '$(SharePointSiteUrl)'}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.