請幫助我從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
的[添加在參考的powershell 2.0對DLL]可能的複製(http://stackoverflow.com/questions/3360867/add-reference-to-dll-in-powershell-2 -0) –
我經歷了提供的鏈接,但略有不同。 .Net軟件包可以通過PowerShell訪問,但如果我在兩個位置都沒有,則無法訪問nuget .dll。我想只將這個.dll保存在根文件夾中,而不是在sys32中。 – Sham
此鏈接可能對您有用:https://blogs.technet.microsoft.com/heyscriptingguy/2010/11/11/use-powershell-to-work-with-the-net-framework-classes/ –