从powershell脚本引用.Net.dll

从powershell脚本引用.Net.dll,.net,powershell,nuget,powershell-3.0,.net,Powershell,Nuget,Powershell 3.0,您能帮我从powershell脚本中引用.Net.dll吗?我正在使用powershell ISE编写/调试脚本。我有一些.net代码引用其中的Nuget包,我想将这些代码嵌入到powershell脚本中 如果我在C:\WINDOWS\system32\WindowsPowerShell\v1.0和脚本的根目录(C:\TestProjects\UpdateLocalNugetRepository)路径中复制required.dll,它会工作得很好。我不想这样做,因为在生产中,我们无法将.dll复

您能帮我从powershell脚本中引用.Net.dll吗?我正在使用powershell ISE编写/调试脚本。我有一些.net代码引用其中的Nuget包,我想将这些代码嵌入到powershell脚本中

如果我在C:\WINDOWS\system32\WindowsPowerShell\v1.0和脚本的根目录(C:\TestProjects\UpdateLocalNugetRepository)路径中复制required.dll,它会工作得很好。我不想这样做,因为在生产中,我们无法将.dll复制到system32文件夹。我知道我做错了什么。你能帮忙吗? 下面是我的powershell脚本-

$path=“C:\TestProjects\UpdateLocalNugetRepository”
$Assem=@(
“$path\NuGet.Configuration.dll”,
“$path\System.Core.dll”,
“$path\System.dll”
) 
$Source=@“
使用NuGet.Configuration;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统线程;
使用System.Threading.Tasks;
名称空间NuGetpTest
{
公用事业
{
公共静态异步任务MyMethod(字符串packageName、字符串p1、字符串p2)
{
//这里我使用上面提到的.dll(Nuget.Configuration)。
}
}
}
“@ 
添加类型-ReferenceAssemblys$Assem-TypeDefinition$Source-Language CSharp
$result=[numgetpstest.Utility]::MyMethod(param1,param2,param3).GetAwaiter().GetResult()

$result
您可以使用
添加类型
代码段加载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

检查:

我找到了问题的解决方案,需要在引用程序集之前添加类型以注册另一个类型。下面是我的更新代码

$path=“C:\TestProjects\UpdateLocalNugetRepository”
添加类型-Path“$Path\NuGet.Configuration.dll”
添加类型-Path“$Path\System.Core.dll”
添加类型-路径“$Path\System.dll”
$Assem=@(
“$path\NuGet.Configuration.dll”,
“$path\System.Core.dll”,
“$path\System.dll”
) 
$Source=@“
使用NuGet.Configuration;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统线程;
使用System.Threading.Tasks;
名称空间NuGetpTest
{
公用事业
{
公共静态异步任务MyMethod(字符串packageName、字符串p1、字符串p2)
{
//这里我使用上面提到的.dll(Nuget.Configuration)。
}
}
}
“@ 
添加类型-ReferenceAssemblys$Assem-TypeDefinition$Source-Language CSharp
$result=[numgetpstest.Utility]::MyMethod(param1,param2,param3).GetAwaiter().GetResult()

$result
我通过提供的链接访问的可能重复项,但有点不同..Net软件包可访问powershell,但如果两个位置都没有,则无法访问nuget.dll。我只想将此.dll保存在根文件夹中,而不想保存在sys32中。此链接可能对您有用:当然..让我试试。您可能想这样做使用一些环境变量来解决路径:它给了我以下错误-名称空间“NuGet”中不存在类型或名称空间名称“Configuration”(是否缺少程序集引用?)可能有很多问题…这是一个更一般的错误。我要修复的唯一问题是我不想将.dll复制到system32(C:\WINDOWS\system32\WindowsPowerShell\v1.0)文件夹。