Monday, 18 January 2016

Add to GAC (Global Assembly Cache) with Poweshell Remoting

Script made available here, by me allows the files to be added to GAC (Global Assembly Cache) in a remote machine, using poweshell (This script was created on a great sample script available in this article, which explaines how to copy files using PowerShell remoting). Script can run on Server A and It can copy required assemblies from Server A, to Server B and then add to GAC in Server B. This can be used in a standalone installer script created with poweshell and can be utilized in Release Management.

In order to run the script first you need to enable powershell remoting in Server B (target remote server). To do that run in Server B.

Enable-PSRemotingimage

Max size issue

When running the script you could run in to max file size default limit per file if your assembly is larger than 10MB.

WARNING: Command failed. Sending data to a remote command failed with the following error message: The current deserialized object size of the data received from remote client exceeded allowed maximum object size. The current deserialized object size is 1057
2800. Allowed maximum object size is 10485760. For more information, see the about_Remote_Troubleshooting Help topic.
image

As explained in this stackoverflow Q&A, run below script in remote target server (Server B), to enable larger files.

Register-PSSessionConfiguration -Name AsmNoLimits #or the name you like.image
Set the max file size to 500MB. this is run in Server B (Target remote server).
Set-PSSessionConfiguration -Name AsmNoLimits ` -MaximumReceivedDataSizePerCommandMB 500 -MaximumReceivedObjectSizeMB 500image
In the script this configuration “AsmNoLimits” used, when creating the remote PowerShell session.image

With this change large files (upto 500MB) can be copied .image

image

Adding to GAC

Below script segment allows adding to GAC in the remote computer (Server B).image

Once executed in Server A, file copied from Server A to B and getting added to GAC in Server B.

dir C:\temp\GAC\*.* | .\AddAssembliesToGac-Remote.ps1 -Computername "dolphintfsa.domainx.local" -Credential "domainx\chamindac" -Passthru -Verboseimage

image

You can download the script from technet gallery.

A sample script to call this script would look like below.image

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<#
need to execute below in remote computer(Admin PS Window) to enable PS Remote

 PS C:\Windows\system32> Enable-PSRemoting

 Then execute the below to allow file size more than 10MB
 
 PS C:\Windows\system32> Register-PSSessionConfiguration -Name AsmNoLimits #or the name you like.
 
 PS C:\Windows\system32> Set-PSSessionConfiguration -Name AsmNoLimits ` -MaximumReceivedDataSizePerCommandMB 500 -MaximumReceivedObjectSizeMB 500

 AsmNoLimits is used in AddAssembliesToGac-Remote.ps1

#>
$username = "yourdomain\yourdomainuser"
$password = ConvertTo-SecureString "yourPassword" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $password

dir C:\temp\GAC\*.dll | .\AddAssembliesToGac-Remote.ps1 -Computername "YourRemoteMachine" -Credential $cred -Passthru -Verbose

No comments:

Popular Posts