Wednesday, 28 May 2014

Set the memory limit on remotely invoked jobs to 4Gb

Powershell with WinRM

Windows 7 and Windows Server 2008 R2 should have these installed by default. For earlier versions of Windows, select the appropriate download from this article: http://support.microsoft.com/kb/968929.

WinRM requires at least PowerShell 2.0.

To find which version of PowerShell you are running, start a PowerShell window and type Get-Host – the resulting output includes a 'Version : #.#' line.

WinRM Configuration

Both the source and target machines must be configured for remote operations.
This requires the 'Windows Remote Management' service to be installed and started, for the WinRM listener to accept WS-Man requests and for the Firewall to include an exception for WinRM.
The simplest way to accomplish this is to get winrm to do it all for you.

Open a PowerShell window as an Administrator and run the command below on the target machine (enter ‘y’ at the prompts):
winrm quickconfig

Example script

Here's a simple script I use to set the memory limit on remotely invoked jobs to 4Gb.

[CmdletBinding(DefaultParameterSetName = 'Secret')]
param(
  [Parameter(mandatory=$true)]
  $username,
  
  [Parameter(Mandatory = $True, ParameterSetName = 'Secret')]
  [Security.SecureString]
  ${Type your password},
  
  [Parameter(Mandatory = $True, ParameterSetName = 'Plain')]
  [string]
  $password)

if ($Password) {
    $SecretPassword = $password | ConvertTo-SecureString -AsPlainText -Force
} else {
    $SecretPassword = ${Type your password}
}

$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,$secretPassword)

"server1", "server2" |% {
  $server=$_ + ".domain.com"
  
  Connect-WSMan -ComputerName $server -Credential $cred
  
  # Set the memory limit on remotely invoked jobs to 4Gb
  Set-Item wsman:$server\Shell\MaxMemoryPerShellMB 4096
  
  Disconnect-WSMan –ComputerName $server
}

No comments: