Archive

Archive for the ‘Powershell’ Category

Deploy .dll in assembly without gacutil in the server

November 2, 2012 Leave a comment
Recently I went in one customer office and I made custom claim based provider for them. I had to use their some external dll file to access web service. I added those dll file in my solution but it was not pushed in assembly when I deployed my solution. So I need to add those dll file in assembly folder in their server. They dont had SDK installed in the server and definitely it should not normally. I tried with drag and drop option but that was not working as well. So I had to use Powershell script to add those dll file in the assembly. The script is:
 
#
# This process must be run with Set-ExecutionPolicy Unrestricted
#
$assemblyDll = “mysolution.dll”
$assemblyPath = “some path like c:\” + “\” + $assemblyDll
# method for adding new assemblies to the GAC
function Add-GacItem([string]$assembly) {
Begin
{
# see if the Enterprise Services Namespace is registered
 
if ($null -eq ([AppDomain]::CurrentDomain.GetAssemblies() |? { $_.FullName -eq “System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” }) ) {
# register the Enterprise Service .NET library
[System.Reflection.Assembly]::Load(“System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”) | Out-Null
}
# create a reference to the publish class
$publish = New-Object System.EnterpriseServices.Internal.Publish
}
 
Process
{
# ensure the file that was provided exists
if ( -not (Test-Path $assembly -type Leaf) ) {
throw “The assembly ‘$assembly’ does not exist.”
}
# ensure the file is strongly signed before installing in the GAC
if ( [System.Reflection.Assembly]::LoadFile( $assembly ).GetName().GetPublicKey().Length -eq 0) {
throw “The assembly ‘$assembly’ must be strongly signed.”
}
# install the assembly in the GAC
Write-Output “Installing: $assembly”
$publish.GacInstall( $assembly )
}
}
# method for removing assemblies from the GAC
function Remove-GacItem([string]$assembly) {
Begin
{
# see if the Enterprise Services Namespace is registered
 
if ($null -eq ([AppDomain]::CurrentDomain.GetAssemblies() |? { $_.FullName -eq “System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” }) ) {
# register the Enterprise Service .NET library
[System.Reflection.Assembly]::Load(“System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”) | Out-Null
}
# create a reference to the publish class
$publish = New-Object System.EnterpriseServices.Internal.Publish
}
 
Process
{
# ensure the file that was provided exists
if ( -not (Test-Path $assembly -type Leaf) ) {
throw “The assembly ‘$assembly’ does not exist.”
}
# ensure the file is strongly signed before installing in the GAC
if ( [System.Reflection.Assembly]::LoadFile( $assembly ).GetName().GetPublicKey().Length -eq 0) {
throw “The assembly ‘$assembly’ must be strongly signed.”
}
# install the assembly in the GAC
Write-Output “UnInstalling: $assembly”
$publish.GacRemove( $assembly )
}
}
Write-Host “Registering the Assembly: ‘$assemblyPath’…” – ForegroundColor Green
Add-GacItem $assemblyPath
Write-Host “UnRegistering the Assembly: ‘$assemblyPath’…” – ForegroundColor Green
Remove-GacItem $assemblyPath
 
 
So its done. Remember to delete removing assembly part when you deploy 🙂
 
Categories: Powershell, SharePoint