2016-10-03 71 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