I know how to run C# code in PowerShell.
But in my C#
code I want to use a referenced DLL (specifically NtApiDotNet ).
I installed it with NuGet in Visual Studio and it works fine because the referenced DLL is referenced by Visual Studio.
When I use the code inside PowerShell I don't see an option to reference the C#
code to this DLL.
I tried to load the DLL with [Reflection.Assembly]::LoadFile("c:\\path\\file.dll") | Out-Null
[Reflection.Assembly]::LoadFile("c:\\path\\file.dll") | Out-Null
but it need to be loaded inside the C#
code and therefore it doesn't help me.
I also tried to load it like that:
Add-Type -Path "C:\Users\myuser\Downloads\NtApiDotNet.dll"
But it didn't work.
This is the powershell code:
$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()
Because I don't reference the DLL I am getting the following error:
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
How can I reference the DLL so I would be able to use the referenced DLL NtApiDotNet
?
Issue similar to mine can be found here .
I just needed to add the DLL to ReferencedAssemblies
.
This is the fixed line:
$assemblyRange = @("System.dll", $location, "C:\Users\myuser\Downloads\NtApiDotNet.dll")
I used the NtApiDotNet.dll
from the releases .
When I compiled it according to the readme file:
dotnet build NtApiDotNet\NtApiDotNet.Core.csproj -c Release
The DLL didn't work correctly.
I just ran into this problem as well, but fortunately I solved it. To address it, you need to install NtObjectManager module by using Install-Module "NtObjectManager" -Scope CurrentUser
. If this not work, make sure you have import NtObjectManager.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.