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

3
  • Possible duplicate of Add reference to dll in powershell 2.0 Commented Oct 3, 2016 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. Commented Oct 3, 2016 at 10:43
  • This link might be of use to you: blogs.technet.microsoft.com/heyscriptingguy/2010/11/11/… Commented Oct 3, 2016 at 10:44

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

5 Comments

Sure..Let me give it a try.
You might want to use some environment variables to solve paths: technet.microsoft.com/en-us/library/ff730964.aspx
It's giving me below error - The type or namespace name 'Configuration' does not exist in the namespace 'NuGet' (are you missing an assembly reference?)
Can be a host of things... this is a more generic error.
Only issue, I want to fix is that I don't want to copy .dlls to system32(C:\WINDOWS\system32\WindowsPowerShell\v1.0) folder.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.