[PowerShell] Import-PSSession returns System.OutOfMemoryException

Summary

Import-PSSession returns System.OutOfMemoryException when attempting to import a PowerShell remote session

Issue

When attemtping to import Citrix PowerShell modules from a delivery controller using the following commands (to ensure load on ddc’s was kept to a minimum){

$strController = ""

if($objUser.count -le 0){
	$objUser = Get-Credential
}

$objRemoteSession = New-PSSession -ComputerName $strController -Credential $objUser
Invoke-Command -ScriptBlock {add-pssnapin Citrix.*} -Session $objRemoteSession

Import-PSSession -Session $objRemoteSession -Module Citrix.* -Prefix RM

When attempting to run Import-PSSession you get the following error:

Import-PSSession : Proxy creation has been skipped for the '%' command, because Windows PowerShell could not verify the safety of the command name.
At line:1 char:1
+ Import-PSSession -Session $RemoteSession
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Import-PSSession], InvalidOperationException
    + FullyQualifiedErrorId : ErrorSkippedUnsafeCommandName,Microsoft.PowerShell.Commands.ImportPSSessionCommand
Import-PSSession : Proxy creation has been skipped for the '?' command, because Windows PowerShell could not verify the safety of the command name.
At line:1 char:1
+ Import-PSSession -Session $RemoteSession
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Import-PSSession], InvalidOperationException
    + FullyQualifiedErrorId : ErrorSkippedUnsafeCommandName,Microsoft.PowerShell.Commands.ImportPSSessionCommand
Import-PSSession : Running the Get-Command command in a remote session reported the following error: Exception of type 'System.OutOfMemoryException' was 
thrown..
At line:1 char:1
+ Import-PSSession -Session $RemoteSession
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidResult: (:) [Import-PSSession], RuntimeException
    + FullyQualifiedErrorId : ErrorFromRemoteCommand,Microsoft.PowerShell.Commands.ImportPSSessionCommand

Resolution:

It turns out there is a limit set to the size of the buffer used by the PowerShell Shell (session). Which by default is set to 150 Mb.

While this is ok for basic commands when working with servers in a large environment this is woefully small, and with PowerShell v5 on the horizon it will likely be inadequate for doing anything of any real complexity.

The solution, increase the limit to 2048 Mb.

I stumbled across the solution in the following blog:
http://www.gsx.com/blog/bid/83018/Troubleshooting-unknown-PowerShell-error-messages

Implementation

You can check the current limit by running the following commands in an elevated PowerShell prompt:

cd WSMan:\localhost\Shell
dir

This should return something similar to the following:

   WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Shell

Type            Name                           SourceOfValue   Value
----            ----                           -------------   -----
System.String   AllowRemoteShellAccess                         true
System.String   IdleTimeout                                    7200000
System.String   MaxConcurrentUsers                             10
System.String   MaxShellRunTime                                2147483647
System.String   MaxProcessesPerShell                           25
System.String   MaxMemoryPerShellMB                            150
System.String   MaxShellsPerUser                               30

In order to increase this limit you need to type the following commands

cd WSMan:\localhost\Shell
dir
Set-Item .\MaxMemoryPerShellMB 2048

Now running

dir

should return the following

   WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Shell

Type            Name                           SourceOfValue   Value
----            ----                           -------------   -----
System.String   AllowRemoteShellAccess                         true
System.String   IdleTimeout                                    7200000
System.String   MaxConcurrentUsers                             10
System.String   MaxShellRunTime                                2147483647
System.String   MaxProcessesPerShell                           25
System.String   MaxMemoryPerShellMB                            2048
System.String   MaxShellsPerUser                               30
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s