[英]Running C# code with referenced DLL in PowerShell
我知道如何在 PowerShell 中运行 C# 代码。
但在我的C#
代码中,我想使用引用的 DLL(特别是NtApiDotNet )。
我在 Visual Studio 中使用 NuGet 安装它并且它工作正常,因为引用的 DLL 被 Visual Studio 引用。
当我在 PowerShell 中使用代码时,我没有看到将C#
代码引用到此 DLL 的选项。
我试图用[Reflection.Assembly]::LoadFile("c:\\path\\file.dll") | Out-Null
加载 DLL [Reflection.Assembly]::LoadFile("c:\\path\\file.dll") | Out-Null
[Reflection.Assembly]::LoadFile("c:\\path\\file.dll") | Out-Null
但它需要加载到C#
代码中,因此它对我没有帮助。
我也尝试像这样加载它:
Add-Type -Path "C:\Users\myuser\Downloads\NtApiDotNet.dll"
但它没有用。
这是powershell代码:
$code = @"
using System;
using NtApiDotNet;
namespace ConsoleApplication2
{
public class Program
{
public static void Main()
{
NtSection section = null;
Console.WriteLine("C# code from PowerShell");
}
}
}
"@
$location = [PsObject].Assembly.Location
$compileParams = New-Object System.CodeDom.Compiler.CompilerParameters
$assemblyRange = @("System.dll", $location)
$compileParams.ReferencedAssemblies.AddRange($assemblyRange)
$compileParams.GenerateInMemory = $True
Add-Type -TypeDefinition $code -CompilerParameters $compileParams -passthru | Out-Null
[ConsoleApplication2.Program]::Main()
因为我没有引用 DLL,所以出现以下错误:
Add-Type : c:\Users\myuser\AppData\Local\Temp\s1fssauz.0.cs(2) : The type or namespace name 'NtApiDotNet' could not
be found (are you missing a using directive or an assembly reference?)
c:\Users\myuser\AppData\Local\Temp\s1fssauz.0.cs(1) : using System;
c:\Users\myuser\AppData\Local\Temp\s1fssauz.0.cs(2) : >>> using NtApiDotNet;
c:\Users\myuser\AppData\Local\Temp\s1fssauz.0.cs(3) :
At line:24 char:1
+ Add-Type -TypeDefinition $code -CompilerParameters $compileParams -pa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Except
ion
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : Cannot add type. Compilation errors occurred.
At line:24 char:1
+ Add-Type -TypeDefinition $code -CompilerParameters $compileParams -pa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
Unable to find type [ConsoleApplication2.Program].
At line:25 char:1
+ [ConsoleApplication2.Program]::Main()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (ConsoleApplication2.Program:TypeName) [], RuntimeException
如何引用 DLL 以便能够使用引用的 DLL NtApiDotNet
?
可以在此处找到与我的问题类似的问题。
我只需要将 DLL 添加到ReferencedAssemblies
。
这是固定线路:
$assemblyRange = @("System.dll", $location, "C:\Users\myuser\Downloads\NtApiDotNet.dll")
我使用了发行版中的NtApiDotNet.dll
。
当我根据自述文件编译它时:
dotnet build NtApiDotNet\NtApiDotNet.Core.csproj -c Release
DLL 无法正常工作。
我也刚遇到这个问题,幸好我解决了。 要解决它,您需要使用Install-Module "NtObjectManager" -Scope CurrentUser
安装 NtObjectManager 模块。 如果这不起作用,请确保您已导入 NtObjectManager。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.