簡體   English   中英

在 PowerShell 中使用引用的 DLL 運行 C# 代碼

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM