我正在從 C# 工具運行 PowerShell 腳本,如下所示:using (PowerShell pshell = PowerShell.Create()){ pshell.AddCommand(scriptFullPath); pshell.AddParameter("username", user); pshell.AddParameter("password", pass); PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>(); PSInvocationSettings settings = new PSInvocationSettings(); settings.ErrorActionPreference = ActionPreference.Stop; pshell.Invoke(null, outputCollection, settings);}在我需要來自其他程序集的特殊 Cmdlet 之前,腳本中的幾乎所有內容都運行良好。Add-PSSnapin 命令將始終失敗并出現:Exception: The Windows PowerShell snap-in 'Microsoft.SharePoint.Powershell' is not installed on this computer.Exception: Cannot bind parameter 'Path' to the target. Exception setting "Path": "Cannot find path 'D:\dev\tool\Microsoft.SharePoint.dll' because it does not exist."跑步時$snapin = Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.SharePoint.Powershell"}if ($snapin -eq $null){ Write-Host "Loading SharePoint Powershell Snapin" Add-PSSnapin "Microsoft.SharePoint.Powershell" Add-Type -Path "Microsoft.SharePoint.dll" Add-Type -Path "Microsoft.SharePoint.Runtime.dll"}直接在 PowerShell 窗口中運行腳本時一切正常,所以我猜它與未從 C# 工具轉發的 PATH 或范圍有關。使用 AddCommand 的參數 useLocalScope 或其他參數沒有產生任何結果(盡管我不確定這是否與路徑有關)。如何使腳本工作并找到外部程序集?
1 回答

繁花如伊
TA貢獻2012條經驗 獲得超12個贊
SharePoint PowerShell 管理單元僅在 64 位中可用。您的 C# 工具可能作為 x86 進程運行,因此會給您“未安裝”錯誤。此外,您可能必須以“管理員身份”運行該程序,因為某些命令需要它才能運行。
第二個錯誤是,您是對的,默認情況下沒有為 SharePoint 設置 PATH 變量。解決方法是指定 .dll 的完整路徑(并更改安裝的版本號),例如
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.dll"
- 1 回答
- 0 關注
- 165 瀏覽
添加回答
舉報
0/150
提交
取消