从WCF服务运行交换命令时出错

时间:2019-08-29 17:01:20

标签: wpf vb.net powershell

因此,我制作了此应用程序,该应用程序运行交换命令并将结果以服务器维护的方式发送到SQL Server。我运行了作为类在本地运行交换命令的函数,为了在多台机器上部署,应用程序应连接到调用类中的函数的WCF服务。我尝试将本地函数移到WCF服务文件中,并将该类移到WCF服务文件引用的较大类中。现在,我收到一个错误,尽管它在本地运行时仍无法识别交换命令。运行空间和Powershell命令执行是否不能在WCF服务上运行,或者我将项目设置错了吗?

我正在将我的应用程序部署到64位计算机上。 WCF服务引用的类也正在64位计算机上构建。

Public Sub GetExchangedata(strComp As String, strServ As String)
  Dim strMailboxes() As String = {"DiscoveryMailbox", "EquipmentMailbox", "GroupMailbox", "LinkedMailbox", "LegacyMailbox", "RoomMailbox", "SharedMailbox", "SchedulingMailbox", "TeamMailbox", "UserMailbox"}
  Dim MyStringBuiler As New StringBuilder()
  Dim rsConfig As RunspaceConfiguration = RunspaceConfiguration.Create()
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", New PSSnapInException)
  For Each strMailboxtype As String In strMailboxes
    Using myRunSpace As Runspace = RunspaceFactory.CreateRunspace(rsConfig)
      myRunSpace.Open()
      Dim mypipeline As Pipeline = myRunSpace.CreatePipeline()
      Dim rsInvoker As RunspaceInvoke = New RunspaceInvoke(myRunSpace)
      rsInvoker.Invoke("Set-ExecutionPolicy Bypass")
      Dim exchCommand As String = "Add-PSSnapin 
      Microsoft.Exchange.Management.PowerShell.E2010;(get-mailbox - 
      recipienttypedetails " & strMailboxtype & " ).count "
      Dim strProc As String = "(get-process).count" 'this command works 
      mypipeline.Commands.AddScript(exchCommand)
      'mypipeline.Commands.AddScript(strProc)
      mypipeline.Commands.Add("Out-String")
      Dim outputs As Collection(Of PSObject) = mypipeline.Invoke()
      For Each result As PSObject In outputs
        MyStringBuiler.AppendLine(strMailboxtype & " : " & result.ToString())
        dsExchangeAcounts.Tables(0).Rows.Add(strComp, strServ, strMailboxtype, result.ToString)
      Next
      rsInvoker.Invoke("Set-ExecutionPolicy Default")
      myRunSpace.Close()
    End Using
  Next
End Sub

此代码可以在我的本地计算机上完美运行,但是从WCF服务客户端运行时会出错。

1 个答案:

答案 0 :(得分:0)

因此错误被证明是我的WCF服务应用程序未在64位模式下运行,并且Powershell不喜欢该32位模式。点击了此链接:WCF 64 bit not working

将其配置为仅以64位运行后,我的代码执行得很好。很棒