因此,我正在圍繞着試圖讓這個工作,我已經嘗試了兩天,我只是無法弄清楚。無法通過vb.net應用程序運行Exchange Powershell
我有以下vb函數需要一個創建的PowerShell腳本,並應在PowerShell中運行它。一切工作正常,直到命令管道被調用的點。此時,沒有命令運行。你可以看到,我試圖將Microsoft.Exchange.Management.PowerShell.E2010管理單元添加到運行空間,它根本不喜歡那種說明沿着管理單元行的東西不存在(它它)以及當我如圖所示運行代碼時,沒有命令被識別爲有效。我甚至添加了特定命令「Add-PSSnapin」來嘗試加載任何Exchange管理單元,但它聲明「Add-PSSnapin」未被識別爲有效的命令。
如果在命令參與之前暫停程序,我可以看到管道中的每個命令都是正確的格式。如果我將管道中的命令文本直接複製並粘貼到PowerShell窗口中,它運行良好。
我的代碼如下,歡迎提供任何建議。
編輯:我也嘗試添加行「添加-PSSnapin 防爆」(帶星號EX的各項一面 - 我無法弄清楚了這個格式化了,不好意思)
嘗試加載交易所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