您能帮我从 powershell 脚本引用 .Net .dll 吗?我正在使用 powershell ISE 编写/调试脚本。我有一些引用 Nuget 包的 .net 代码,我想将该代码嵌入到 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
Check: https://blogs.technet.microsoft.com/heyscriptingguy/2010/11/11/use-powershell-to-work-with-the-net-framework-classes/ https://blogs.technet.microsoft.com/heyscriptingguy/2010/11/11/use-powershell-to-work-with-the-net-framework-classes/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)