tisdag 7 juli 2015

Using PowerShell to Remove a DLL from the GAC on Windows Server 2008 R2 / Windows Server 2012

In my previous post (Using PowerShell to deploy DLLs to the GAC...) I showed a script on how to deploy a DLL to the GAC. In some cases we also want to remove DLL's from the GAC we do this we use the same assembly System.EnterpriseServices, as we used to deploy the DLL.

In the assembly  System.EnterpriseServices in the namespace System.EnterpriseServices.Internal  we have the class Publish. The Publish class contains a method named GacRemove which takes a string as an input parameter. This string is full path to the location where the original DLL is located. If you deleted the DLL you will have to copy it from the GAC to the original location and then execute GacRemove. Reference to MSDN

The following script is the absolute bare bone script needed to remove a DLL from the GAC. Remember to replace the <FULLPATH_TO_YOUR_ORIGINAL_DLL> with the actual literal path to the original DLL.
$AssemblyFilePath = "<FULLPATH_TO_YOUR_ORIGINAL_DLL>"
[System.Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacRemove($AssemblyFilePath)
Write-Host “Assembly has been removed from the GAC”

Using PowerShell to deploy DLLs to the GAC on Windows Server 2008 R2 / Windows Server 2012

If you have a local development machine they you can usually drag and drop DLL's to the Assembly folder or if .Net Framework is installed use GacUtil to deploy DLL's. However these options are not normally available in a production environment. Best practices also dictates that GacUtil should not be located on a production environment as it is part of development tools.

One workaround is to turn off UAC for the current user so they can do a drag and drop to the Assembly folder in windows. I don't think turning off UAC in a production environment is a good option as it requires a server restart and it also open up the server for possible attacks even if it is only for a short while.

So if you have DLL's that needs to be deployed to the GAC and it is not handled by an installation package. Then I would suggest that you use PowerShell to call the built in functionality in the .Net Framwork to deploy the DLL.
.Net contains the assembly System.EnterpriseServices, in this assembly in the namespace System.EnterpriseServices.Internal  we have the class Publish. The Publish class contains a method named GacInstall that takes an input parameter of type string that should be the full path to the DLL-file that you want to install in the GAC. Reference to MSDN

The following script is the absolute bare bone script needed to deploy a DLL to the GAC. Remember to replace the <FULLPATH_TO_YOUR_DLL> with the actual literal path to your DLL.
$AssemblyFilePath = "<FULLPATH_TO_YOUR_DLL>"
[System.Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall($AssemblyFilePath)
Write-Host “Assembly installed in the GAC”