.net - 使用 PowerShell 时出现内存泄漏

标签 .net powershell powershell-2.0 runspace

我正在 VB.net 中使用电源 shell 连接到 Office 365 实例。这工作正常,但运行该服务的服务的内存使用量不断增加。我使用以下代码来启动和关闭电源shell.似乎 close 方法无法正常工作。

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

最佳答案

Microsoft 在 import-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

最后给微软打了电话,他们说如果没有必要就不要关机。尽可能长时间地重用相同的运行空间...不过,如果您在程序中动态使用并且它闲置一段时间(例如在执行 Exchange 2010 powershell 管理命令的 Web 应用程序中),powershell session 将会超时。

如果您找到解决方法,请告诉我们。

关于.net - 使用 PowerShell 时出现内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12309310/

相关文章:

Powershell - 循环脚本,直到用户选择退出

.net - 单击一次更新而不覆盖配置

c# - 带有 Rfc2898DeriveBytes 的 PBKDF2 为相同的输入产生不同的输出?

没有 Blazor 的 C# WASM

.net - 如何在传输级别压缩来自 WCF .NET 的 HTTP 请求?

powershell - 重定向附加到同一日志文件的标准和错误输出

events - 在 Register-ObjectEvent cmdlet 的 Action 脚本 block 中访问事件参数

powershell - 在Powershell中运行一个exe,并在执行该exe后关闭cmd提示符

powershell - SSRS 和 PowerShell : Get report as Excel

Powershell - Invoke-Command 脚本 block 中的 "Copy-item"命令