亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 C# 運行 PowerShell 腳本時,Add-PSSnapin 的路徑不正確

從 C# 運行 PowerShell 腳本時,Add-PSSnapin 的路徑不正確

C#
慕森卡 2022-11-22 15:28:52
我正在從 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"


查看完整回答
反對 回復 2022-11-22
  • 1 回答
  • 0 關注
  • 165 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號