2012-09-06 108 views
2

我在VB.net中使用Power Shell連接到Office 365實例。此工作正常,但運行此服務的服務的內存使用量不斷增加。我正在使用以下代碼啓動和關閉電源shell。close方法等工作不正常。使用PowerShell時發生內存泄漏

Private Function initRunSpace(ByVal user_name As String, ByVal password_user As String) As String 
    Dim StringBuilder As New StringBuilder 
    Dim liveIdconnectionUri As String = "https://ps.outlook.com/powershell" 
    Dim password As New SecureString() 
    Dim str_password As String = password_user 
    Dim username As String = user_name 
    Dim credential As New PSCredential(username, password) 
    Dim iss As InitialSessionState = Nothing 
    Dim psSession As New PSCommand() 
    Dim setVar As New PSCommand() 
    Dim importSession As New PSCommand() 

    Try 
     For Each x As Char In str_password 
      password.AppendChar(x) 
     Next 

     iss = InitialSessionState.CreateDefault 
     iss.ImportPSModule(New String() {"MSOnline"}) 

     runspace = RunspaceFactory.CreateRunspace(iss) 
     runspace.Open() 
      powershell = System.Management.Automation.PowerShell.Create() 
     powershell.Runspace = runspace 
     REM connect to exchange server in the cloud 

     psSession.AddCommand("New-PSSession") 
     psSession.AddParameter("ConfigurationName", "Microsoft.Exchange") 
     psSession.AddParameter("ConnectionUri", New Uri(liveIdconnectionUri)) 
     psSession.AddParameter("Credential", credential) 
     psSession.AddParameter("Authentication", "Basic") 
     psSession.AddParameter("AllowRedirection") 
     powershell.Commands = psSession 
     Dim result = powershell.Invoke() 
     Dim errors As Collection(Of ErrorRecord) = Nothing 
     errors = powershell.Streams.Error.ReadAll() 

     If errors.Count > 0 Then 
      REM Save the order or action for later execution 
      For Each obj As Object In errors 
       If obj IsNot Nothing Then StringBuilder.AppendLine(obj.ToString) 
      Next 
      powershell.Stop() 
      powershell.Dispose() 
      runspace.Close() 
      runspace.Dispose() 

      Return "Import-PSSession:" & StringBuilder.ToString 
      End If 
      Catch ex As Exception 
      Throw ex 
      Finally 
      StringBuilder = Nothing 
      liveIdconnectionUri = Nothing 
      password = Nothing 
      str_password = Nothing 
      username = Nothing 
      credential = Nothing 
      iss = Nothing 
      psSession = Nothing 
      setVar = Nothing 
      importSession = Nothing 
      End Try 
      Return "" 
     End Function 




    Public Sub CloseRunspace() 

    Dim removesession As New PSCommand() 
    Try 

     If powershell IsNot Nothing Then 
      removesession.Commands.AddScript("Remove-PSSession $sa;Remove-Variable $sa;Remove-Module -Name MSOnline;") 
      powershell.Commands = removesession 
      powershell.Runspace = runspace 
      Dim results As Collection(Of PSObject) = powershell.Invoke() 

     End If 
    Catch ex As Exception 
    Finally 
     removesession = Nothing 
     powershell.Stop() 
     powershell.Dispose() 

     runspace.Close() 
     runspace.Dispose() 
     GC.Collect() 
     Thread.Sleep(100) 
    End Try 
    End Sub 
+0

你曾經調用'CloseRunspace()'嗎?我在這段代碼中沒有看到它。如果你有*錯誤,你會處置所有的事情,但如果沒有錯誤,你就不會這樣做。 – alroc

+0

是的,我打電話給它。問題是,即使調用它,它也不會完全釋放內存,每次調用initrunspace()時都會繼續增長。 – agupta

回答

3

Microsoft有一個已知問題,雖然未發佈,但導入-PSsession命令存在問題。它咀嚼了很多內存,甚至在關閉和處理運行空間,powershell和管道後也不會釋放它。我甚至嘗試過顯式調用GC.Collect ...但沒有幫助。我在網絡上的一些地方發現了它的一些提示。

http://social.msdn.microsoft.com/Forums/en-US/sbappdev/thread/3c143111-eb14-4235-8084-5edec8202f8b http://go4answers.webhost4life.com/Example/re-establish-failed-remote-powershell-80988.aspx

最後發出的呼叫到MS和他們說不要關機,如果你不就得了。儘可能重複使用相同的運行空間......儘管如果您在程序中動態使用PowerShell會話並且空閒一段時間,比如在製作Exchange 2010 PowerShell管理命令的Web應用程序中,PowerShell會話會超時。

讓我們知道您是否找到解決方法。

+0

根據您的需要,您可能可以使用[RunspaceFactory.CreateOutOfProcessRunspace()](http://msdn.microsoft.com/en-us/library/hh470829(v = vs.85).aspx)。它在一個全新的PowerShell進程中創建一個運行空間,然後使用看起來像PowerShell遠程處理的東西來回傳給調用進程。所以,當你使用遠程PowerShell(複雜對象以反序列化的PSObjects等形式回來)時,你必須處理同樣的問題,但是任何可能泄漏內存的東西都會在該過程中泄漏它,當你丟棄時'重做。 –

0

我會建議儘可能不要使用「Import-PSSession」。相反,請使用「Invoke-Command」遠程執行powershell腳本而不導入它們。

導入是一個代價高昂的操作,它可以避免。

相關問題