windows server 2012- copy to Gac power shell

In Windows server 2008 R2, to copy DLL to CAG you can drag an drop the dll to ‘Assembly folder’.. but Windows server 2012 make the difference between the 32 compiled dll and the 64 compiled dll.. then if you drag and drop the dll to Assembly Folder, Windows Server do not copy the dll.

This solution is very important to the SharePoint Developer. In fact, some time you need to deploy dll to sharepoint without deploying all the WSP file. Then you rebuild the project and you copy the Dll to CAG.
We will create an external Tools in Visual Studio to quick deploy the DLL to GAC. We will create two binary files “copy to cag.bat” and “copy to cag.ps1”, and we will configure Visual Studio to easly using this tools.

The two files must have the same folder path.

to gac.ps1

#args[1]: the binary folder passed in the batch file
#args[2]: the dll name passed by the batch file
$dllFile= "{0}{1}.dll" -f $args[1], $args[2]
$location =$args[1]
$msgDll= "copy to GAC:=> {0} " -f $dllFile
$msgLocation= "Set location :=> {0} " -f $args[1]

write-host $msgLocation -foregroundcolor "green"
write-host $msgDll -foregroundcolor "green"

#set location to depoly
Set-location $location
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
#install to CAG
#reset iis

write-host "Finish" -foregroundcolor "green"

The batch file called by visual studio get the deployment folder and dll Name from Visual and pass this parameters to power shell file.

 to cag.bat

powershell -noexit -file "to gac.ps1" "%CD%" %1 %2

%1: value passed by Visual Studio contain the binary folder
%2: value passed by Visual studion contain tne Dll name.

The two files is ready to copy Dll to CAG, it remains to configure Visual studio and adding the external tools

Visual Studio: Tools –> External tools


Click “Add” ro add new externals tools add set these parameters:

  1. Title : Copy To GAC
  2. Command : link to file  < to Gac.bat>
  3. Arguments : $(BinDir) $(TargetName)
  4. Initial Directory : set the direct to batch file
  5. Close on exit: true

add external tools

Now you must give permission in Power shell to the current user, to execute script power shell.

SharePoint Management shell:
 Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

All is ready 😉

To copy dll from projetc: rebuild project, Tools –> Copy to CAG

Capture 3

This entry was posted in Administration. Bookmark the permalink.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s