簡體   English   中英

從Powershell腳本引用.Net .dll

[英]Reference .Net .dll from powershell script

您能幫我從Powershell腳本中引用.Net .dll嗎? 我正在使用Powershell ISE編寫/調試腳本。 我有一些.net代碼引用了其中的Nuget包,我想將該代碼嵌入到powershell腳本中。

如果我確實在C:\\ WINDOWS \\ system32 \\ WindowsPowerShell \\ v1.0和腳本的根目錄(C:\\ TestProjects \\ UpdateLocalNugetRepository)路徑中復制所需的.dll,則效果很好。 我不想這樣做,因為在生產中我們無法將.dll復制到system32文件夾。我知道我做錯了。 能否請你幫忙? 以下是我的Powershell腳本-

 $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 

您可以使用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/

我已經找到問題的解決方案,需要在引用程序集之前進行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 =@( "$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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM