< Back
Invoke-CSharpMethod
Post
NAME Invoke-CSharpMethod
SYNOPSIS
Run a C# static method without loading the type in the current AppDomain or
starting a new process. This ensures the type is not persisted in the
PowerShell AppDomain allowing a user to modify the C# code without creating
a new process.
SYNTAX
Invoke-CSharpMethod [-Code] <String> [-Class] <String> [-Method] <String> [[-ReferencedAssemblies] <HashSet`1>]
[[-Arguments] <Object>] [-IgnoreWarnings] [<CommonParameters>]
DESCRIPTION
Will invoke the C# method defined by the user in a separate AppDomain and
will output the return value back to the user. Because the code is run in
a separate AppDomain, the types are not loaded in the current PowerShell
AppDomain meaning a different piece of code using the same types can be
run without creating a new process.
PARAMETERS
-Code <String>
[String] The C# code to compile and run
Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Class <String>
[String] The full name of the class that Method is located in
Required? true
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Method <String>
[String] The static method to invoke on the class
Required? true
Position? 3
Default value
Accept pipeline input? false
Accept wildcard characters? false
-ReferencedAssemblies <HashSet`1>
[System.Collections.Generic.HashSet`1[String]] A list of assembly locations
for assemblies that are referenced in the code. By default this will
include System.dll and System.Management.Automation.dll.
Required? false
Position? 4
Default value @()
Accept pipeline input? false
Accept wildcard characters? false
-Arguments <Object>
[Object] Any arguments (if any) to pass in as the method params. This can
be difficult when passing in arrays due to how PowerShell handles them. The
safest way would be to define the value for Arguments as @(,$arg1) where
$arg1 may be any value (including an array).
Use [Type]::Missing when dealing with parameters with default values and
you want to use the default parameter.
A params parameter (params string[] args) is treated as a singular
argument, set the argument in this position as an array of the defined
type.
Required? false
Position? 5
Default value
Accept pipeline input? false
Accept wildcard characters? false
-IgnoreWarnings [<SwitchParameter>]
[Switch] Whether to ignore any compiler warnings when compiling the code
Required? false
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
This cmdlet will return whatever is returned from the C# method.
NOTES
This is mostly a proof of concept to prove that it is possible to invoke C#
code and ensure the types are not persisted in the current PowerShell
session. Due to the AppDomain boundary there are restrictions on the types
that can be used are arguments as well as the types that can be returned
from the C# method. Supported types are the ones that have the
SerializableAttribute set
https://docs.microsoft.com/en-us/dotnet ... work-4.7.2.
-------------------------- EXAMPLE 1 --------------------------
PS C:\\>$code = @'
using System;
using System.Web.Script.Serialization;
namespace PSCSharpInvoker
{
public class Example
{
public static string Run(string[] args)
{
string currentDomain = System.AppDomain.CurrentDomain.FriendlyName;
Console.WriteLine("Invoked in {0}. Arguments: '{1}'", currentDomain, String.Join(", ", args));
return "finished";
}
}
}
'@
$arguments = [String[]]@("arg 1", "arg 2")
Invoke-CSharpMethod -Code $code -Class PSCSharpInvoker.Example -Method Run `
-ReferencedAssemblies "System.Web.Extensions.dll" -Arguments @(,$arguments)
RELATED LINKS
SYNOPSIS
Run a C# static method without loading the type in the current AppDomain or
starting a new process. This ensures the type is not persisted in the
PowerShell AppDomain allowing a user to modify the C# code without creating
a new process.
SYNTAX
Invoke-CSharpMethod [-Code] <String> [-Class] <String> [-Method] <String> [[-ReferencedAssemblies] <HashSet`1>]
[[-Arguments] <Object>] [-IgnoreWarnings] [<CommonParameters>]
DESCRIPTION
Will invoke the C# method defined by the user in a separate AppDomain and
will output the return value back to the user. Because the code is run in
a separate AppDomain, the types are not loaded in the current PowerShell
AppDomain meaning a different piece of code using the same types can be
run without creating a new process.
PARAMETERS
-Code <String>
[String] The C# code to compile and run
Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Class <String>
[String] The full name of the class that Method is located in
Required? true
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Method <String>
[String] The static method to invoke on the class
Required? true
Position? 3
Default value
Accept pipeline input? false
Accept wildcard characters? false
-ReferencedAssemblies <HashSet`1>
[System.Collections.Generic.HashSet`1[String]] A list of assembly locations
for assemblies that are referenced in the code. By default this will
include System.dll and System.Management.Automation.dll.
Required? false
Position? 4
Default value @()
Accept pipeline input? false
Accept wildcard characters? false
-Arguments <Object>
[Object] Any arguments (if any) to pass in as the method params. This can
be difficult when passing in arrays due to how PowerShell handles them. The
safest way would be to define the value for Arguments as @(,$arg1) where
$arg1 may be any value (including an array).
Use [Type]::Missing when dealing with parameters with default values and
you want to use the default parameter.
A params parameter (params string[] args) is treated as a singular
argument, set the argument in this position as an array of the defined
type.
Required? false
Position? 5
Default value
Accept pipeline input? false
Accept wildcard characters? false
-IgnoreWarnings [<SwitchParameter>]
[Switch] Whether to ignore any compiler warnings when compiling the code
Required? false
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
This cmdlet will return whatever is returned from the C# method.
NOTES
This is mostly a proof of concept to prove that it is possible to invoke C#
code and ensure the types are not persisted in the current PowerShell
session. Due to the AppDomain boundary there are restrictions on the types
that can be used are arguments as well as the types that can be returned
from the C# method. Supported types are the ones that have the
SerializableAttribute set
https://docs.microsoft.com/en-us/dotnet ... work-4.7.2.
-------------------------- EXAMPLE 1 --------------------------
PS C:\\>$code = @'
using System;
using System.Web.Script.Serialization;
namespace PSCSharpInvoker
{
public class Example
{
public static string Run(string[] args)
{
string currentDomain = System.AppDomain.CurrentDomain.FriendlyName;
Console.WriteLine("Invoked in {0}. Arguments: '{1}'", currentDomain, String.Join(", ", args));
return "finished";
}
}
}
'@
$arguments = [String[]]@("arg 1", "arg 2")
Invoke-CSharpMethod -Code $code -Class PSCSharpInvoker.Example -Method Run `
-ReferencedAssemblies "System.Web.Extensions.dll" -Arguments @(,$arguments)
RELATED LINKS