2

所以我绕着圈子试图让这个工作,我已经尝试了两天,我就是想不通。

我有以下 vb 函数,它需要一个创建的 powershell 脚本,并且应该在 powershell 中运行它。一切正常,直到调用命令管道为止。此时,没有命令运行。

如您所见,我尝试将 Microsoft.Exchange.Management.PowerShell.E2010 管理单元添加到运行空间,它根本不喜欢这样说明管理单元不存在(确实存在),并且同样,当我运行如图所示的代码时,没有任何命令被识别为有效。我什至添加了特定命令“Add-PSSnapin”来尝试加载任何 Exchange 管理单元,但它指出“Add-PSSnapin”未被识别为有效命令。

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

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

编辑:我也尝试添加“Add-PSSnapin Ex ”行(Ex 的每一侧都有一个星号 - 我无法弄清楚这个格式,抱歉)

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

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
4

1 回答 1

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()

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

于 2012-11-16T14:33:16.963 回答