2013-07-05 12 views
0

我有一個自定義的PowerShell腳本,它接受一些參數作爲輸入。Powershell腳本參數和相關程序集

但是,其中一個參數是在外部程序集中定義的類型。這意味着當我使用PowerShell.exe從命令行運行腳本時,程序集未加載,腳本失敗。

這裏是腳本中的相關部分:

[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" 

在這裏,我怎麼火的腳本(實際上是從一個SharePoint項目的部署後事件):

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

是否有在運行腳本之前告訴PowerShell.exe加載程序集的方法?

[編輯]因爲我與SharePoint工作,我貼到PowerShell的V2

回答

0

好吧,我設法使這個作品。

我使用啓動我的腳本:

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

基本上,而不是直接運行我的腳本文件,我運行一個腳本塊,包括在運行兩個命令:Add-PSSnapin,它加載我的思念組裝,然後執行腳本與& c:\pathto\script.ps1構造。

對於未來的讀者,我的部署後事件正是:

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

我說,沒有。有一兩件事你可以做的是阻止腳本運行,如果大會不存在使用的#Requires聲明:

#Requires -PSSnapin Microsoft.SharePoint.PowerShell 

如果您沒有訪問腳本或者不能控制它的任何方式再我建議改變參數的類型並在函數內部添加驗證。

+0

感謝您幫助。正如我在編輯中所說的,我堅持使用PS V2。我實際上可以訪問腳本,但我無法硬編碼參數,它們必須來自Visual Studio宏(請看我的anwser)。 –