无法通过vb.net应用程序运行Exchange Powershell

时间:2012-11-15 12:36:09

标签: vb.net powershell exchange-server

所以我要绕圈试图让它发挥作用,我已经尝试了两天,我只是想不出来。

我有以下vb函数,它接受一个已创建的powershell脚本,并应在powershell中运行它。一切正常,直到调用命令管道的点。此时,没有命令运行。

正如您所看到的,我已经尝试将Microsoft.Exchange.Management.PowerShell.E2010管理单元添加到运行空间中,它根本不喜欢那些说明管理单元不存在的东西(它确实存在) ),当我运行如图所示的代码时,没有命令被识别为有效。我甚至添加了特定的命令“Add-PSSnapin”来尝试加载任何Exchange管理单元,但它声明“Add-PSSnapin”不被识别为有效命令。

如果我在命令被驱散之前暂停程序,我可以以正确的格式查看管道中的每个命令。如果我将管道中的命令文本直接复制并粘贴到PowerShell窗口中,它运行正常。

我的代码如下,欢迎任何建议。

编辑:我也尝试添加“Add-PSSnapin Ex ”这一行(Ex的每一边都有一个星号 - 我不能在此格式化,抱歉)

尝试加载Exchange PS Snapins作为脚本运行的第一件事(反对在运行空间中设置它)但没有运气

Private Function scriptRunner(ByVal scripttorun As String) As String

    Dim initial As InitialSessionState = InitialSessionState.CreateDefault()
    Dim result As String = ""
    Dim lineFromScript As String = ""
    Dim reader As New StreamReader(tempScript)

    Dim rsConfig As RunspaceConfiguration = RunspaceConfiguration.Create()
    Dim snapInException As New PSSnapInException

    Dim strUserName As String = "DOMAIN\USER"
    Dim strPassword As String = "PASSWORD"
    Dim SecuredPSWD As New System.Security.SecureString()
    For Each character As Char In strPassword
        SecuredPSWD.AppendChar(character)
    Next

    Dim wsmConnectionInfo As WSManConnectionInfo
    Dim strSystemURI As String = "http://SERVER.DOMAIN/powershell?serializationLevel=Full"
    Dim strShellURI As String = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"
    Dim powerShellCredentials As PSCredential = New PSCredential(strUserName, SecuredPSWD)
    wsmConnectionInfo = New WSManConnectionInfo(New Uri(strSystemURI), strShellURI, powerShellCredentials)
    Dim runspace As Runspace = RunspaceFactory.CreateRunspace(wsmConnectionInfo)
    Runspace.Open()
    ' runspace.RunspaceConfiguration.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", snapInException)


    Dim pipeLine As Pipeline = runspace.CreatePipeline()

    Dim command As Command = New Command("")
    ' TEST >> pipeLine.Commands.Add("Add-PSSnapin *Ex*")
    Do While reader.Peek() <> -1
        lineFromScript = Nothing
        lineFromScript = reader.ReadLine()
        pipeLine.Commands.Add(lineFromScript)
        'command.Parameters.Add(lineFromScript)
        'pipeLine.Commands.Add(command)

    Loop

    '' Run the contents of the pipeline
    Dim psObjCollection As Collection(Of PSObject) = pipeLine.Invoke()

    runspace.Close()
    runspace.Dispose()

    Return ""
End Function

1 个答案:

答案 0 :(得分:0)

我最终解决了这个问题,而不是修复它。

我将脚本代码移动到vb.net应用程序中,并将每行写入文件,即

        writer.WriteLine("Add-PSSnapin *Ex*")

然后我通过PowerShell将脚本作为应用程序加载;

            Dim exeStartInfo As System.Diagnostics.ProcessStartInfo
        Dim exeStart As New System.Diagnostics.Process

        exeStartInfo = New System.Diagnostics.ProcessStartInfo("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe")
        exeStartInfo.Arguments = ("-command work\scriptbuilder.ps1")
        exeStartInfo.WorkingDirectory = "C:\ExchangeManager\"
        exeStartInfo.UseShellExecute = False
        exeStart.StartInfo = exeStartInfo
        exeStart.Start()
        exeStart.Close()

不太理想,但它完成了工作。