2

Could you please help me in referencing .Net .dll from powershell script? I'm using powershell ISE to write/debug script. I've some .net code which is referencing Nuget package in it and I want to embedded that code in powershell script.

It works well if I do copy required .dlls in C:\WINDOWS\system32\WindowsPowerShell\v1.0 and script's root(C:\TestProjects\UpdateLocalNugetRepository) path. I don't want to do that beacuse in production we can not copy .dlls to system32 folder.I know that I'm doing something wrong. Could you please help? Below is my powershell script -

$path = "C:\TestProjects\UpdateLocalNugetRepository"

$Assem =@(
        "$path\NuGet.Configuration.dll",
        "$path\System.Core.dll",
        "$path\System.dll"
         ) 

         
$Source = @” 
using NuGet.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace NuGetPSTest
{
    public class Utility
    {
  public static async Task<bool> MyMethod(string packageName, string p1, string p2)
        {
         //Here I use above mentioned .dll(Nuget.Configuration).
 }
    }

    
}

“@ 

Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp  



$result = [NuGetPSTest.Utility]::MyMethod(param1,param2,param3).GetAwaiter().GetResult()
$result
Sham
  • 910
  • 8
  • 15
  • Possible duplicate of [Add reference to dll in powershell 2.0](http://stackoverflow.com/questions/3360867/add-reference-to-dll-in-powershell-2-0) – Kees C. Bakker Oct 03 '16 at 10:40
  • I went through provided link, but it's slightly different. .Net packages are accessible to powershell but the nuget .dll is not accessible if I don't have on both location. I want to keep this .dll in root folder only and not in sys32. – Sham Oct 03 '16 at 10:43
  • This link might be of use to you: https://blogs.technet.microsoft.com/heyscriptingguy/2010/11/11/use-powershell-to-work-with-the-net-framework-classes/ – Kees C. Bakker Oct 03 '16 at 10:44

2 Answers2

1

You can use the Add-Type snippet to load DLL's:

Add-Type -Path "$path\NuGet.Configuration.dll"
Add-Type -Path "$path\System.Core.dll"
Add-Type -Path "$path\System.dll"

.Net DLL's can be added like this:

Add-Type -AssemblyName System.ServiceProcess

Check: https://blogs.technet.microsoft.com/heyscriptingguy/2010/11/11/use-powershell-to-work-with-the-net-framework-classes/

Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
1

I've found the solution for issue, need to do Add-Type before referring in assemblies to register another type. Below is my updated code.

$path = "C:\TestProjects\UpdateLocalNugetRepository"

Add-Type -Path "$path\NuGet.Configuration.dll"
Add-Type -Path "$path\System.Core.dll"
Add-Type -Path "$path\System.dll"

$Assem =@(
        "$path\NuGet.Configuration.dll",
        "$path\System.Core.dll",
        "$path\System.dll"
         ) 

         
$Source = @” 
using NuGet.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace NuGetPSTest
{
    public class Utility
    {
  public static async Task<bool> MyMethod(string packageName, string p1, string p2)
        {
         //Here I use above mentioned .dll(Nuget.Configuration).
 }
    }

    
}

“@ 

Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp  



$result = [NuGetPSTest.Utility]::MyMethod(param1,param2,param3).GetAwaiter().GetResult()
$result
Sham
  • 910
  • 8
  • 15