Memory Leaks while using PowerShell

By : agupta
Source: Stackoverflow.com
Question!

I am connecting to a Office 365 instance using power shell in VB.net.This works fine but the memory usage of the service in which this is running keeps on increasing.I am using the following code to initiate and close the power shell.Seems like the close method is not working correctly.

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
By : agupta


Answers

I would suggest not to use "Import-PSSession" as far as possible. Instead go with the "Invoke-Command" to remotely execute the powershell scripts without importing them.

Import is a costly operation and it could be avoided.

By : Mitra


This video can help you solving your question :)
By: admin