请帮助我从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/ –