2016-10-03 45 views
1

请帮助我从PowerShell脚本中引用.Net .dll?我正在使用powershell ISE编写/调试脚本。我有一些.net代码引用了Nuget包,我想在PowerShell脚本中嵌入代码。从powershell脚本引用.Net .dll

如果我在C:\ WINDOWS \ system32 \ WindowsPowerShell \ v1.0和脚本的根目录(C:\ TestProjects \ UpdateLocalNugetRepository)路径中复制所需的.dll,我不想在生产中这样做,我们不能将.dll复制到system32文件夹。我知道我做错了什么。能否请你帮忙? 下面是我的PowerShell脚本 -

$path = "C:\TestProjects\UpdateLocalNugetRepository" 
 

 
$Assem [email protected](
 
     "$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 
 
    { 
 
    \t public static async Task<bool> MyMethod(string packageName, string p1, string p2) 
 
     { 
 
     \t \t //Here I use above mentioned .dll(Nuget.Configuration). 
 
\t } 
 
    } 
 

 
    
 
} 
 

 
“@ 
 

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

 

 

 
$result = [NuGetPSTest.Utility]::MyMethod(param1,param2,param3).GetAwaiter().GetResult() 
 
$result

+0

的[添加在参考的powershell 2.0对DLL]可能的复制(http://stackoverflow.com/questions/3360867/add-reference-to-dll-in-powershell-2 -0) –

+0

我经历了提供的链接,但略有不同。 .Net软件包可以通过PowerShell访问,但如果我在两个位置都没有,则无法访问nuget .dll。我想只将这个.dll保存在根文件夹中,而不是在sys32中。 – Sham

+0

此链接可能对您有用:https://blogs.technet.microsoft.com/heyscriptingguy/2010/11/11/use-powershell-to-work-with-the-net-framework-classes/ –

回答

1

可以使用Add-Type片段来加载DLL的:

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

NET的DLL的可以加入这样的:

Add-Type -AssemblyName System.ServiceProcess 

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

+0

Sure ..让我试试看。 – Sham

+0

您可能想使用一些环境变量来解决路径:https://technet.microsoft.com/en-us/library/ff730964.aspx –

+0

它给我下面的错误 - 类型或命名空间名称'配置'不存在于 命名空间'NuGet'中(您是否缺少程序集引用?) – Sham

0

我发现问题的解决方案,在引用程序集之前需要做Add-Type来注册另一个类型。以下是我更新的代码。

$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 [email protected](
 
     "$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 
 
    { 
 
    \t public static async Task<bool> MyMethod(string packageName, string p1, string p2) 
 
     { 
 
     \t \t //Here I use above mentioned .dll(Nuget.Configuration). 
 
\t } 
 
    } 
 

 
    
 
} 
 

 
“@ 
 

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

 

 

 
$result = [NuGetPSTest.Utility]::MyMethod(param1,param2,param3).GetAwaiter().GetResult() 
 
$result