Powershell can't invoke .net class' method
Date : March 29 2020, 07:55 AM
it fixes the issue Are you sure that all involved computers are with .net 4.0 installed? And that Powershell is version 3.0 or at least 2.0 with powershell.exe.config customized to use .Net 4.0 ? File.ReadLines starts only from .Net 4.0.
|
Powershell Invoke method neither throwing exception nor returning result
Date : March 29 2020, 07:55 AM
it should still fix some issue PowerShell.Create() does not create new PowerShell process. If you does not specify Runspace for it, then it will create new in-process Runspace. Since it run in your process, it will match your process bitness and you can not change that. To create Runspace with different bitness you need to create out of process Runspace. Here is a sample console application, which demonstrate how you can do that: using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
public static class TestApplication {
public static void Main() {
Console.WriteLine(Environment.Is64BitProcess);
using(PowerShellProcessInstance pspi = new PowerShellProcessInstance()) {
string psfn = pspi.Process.StartInfo.FileName;
psfn=psfn.ToLowerInvariant().Replace("\\syswow64\\", "\\sysnative\\");
pspi.Process.StartInfo.FileName=psfn;
using(Runspace r = RunspaceFactory.CreateOutOfProcessRunspace(null, pspi)) {
r.Open();
using(PowerShell ps = PowerShell.Create()) {
ps.Runspace=r;
ps.AddScript("[Environment]::Is64BitProcess");
foreach(PSObject pso in ps.Invoke()) {
Console.WriteLine(pso);
}
}
}
}
}
}
|
How to invoke C# method from PowerShell whose signature has parameters marked with 'params'
Tag : chash , By : Cowtung
Date : March 29 2020, 07:55 AM
I hope this helps you . I'm trying to invoke a C# library method from PowerShell that has the following signature: , It should be shortest solution in your case. $code =
@'
using System;
using System.Linq.Expressions;
public class Entity
{
public string Property1 { get; set; }
public int Property2 { get; set; }
}
public class TestClass
{
public void Load<T>(T thing, params Expression<Func<T, object>>[] retrievals)
{
foreach (var retrieval in retrievals)
{
System.Console.WriteLine("Retrieval: " + retrieval);
}
}
private static Expression<Func<T, object>> FuncToExpression<T>(Func<T, object> f)
{
return x => f(x);
}
public void Load2<T>(T thing, params Func<T, object>[] retrievals)
{
var funcs = new Expression<Func<T, object>>[retrievals.Length];
for (int i = 0; i < retrievals.Length; i++)
{
funcs[i] = FuncToExpression(retrievals[i]);
}
this.Load<T>(thing, funcs);
}
public void Test1()
{
Entity entity = new Entity();
this.Load<Entity>(entity, e => e.Property1, e => e.Property2);
}
}
'@
Add-Type -TypeDefinition $code -Language CSharp
$o = New-Object TestClass
$o.Test1()
$entity = New-Object Entity
$func1 = [Func`2[Entity, object]]{ param($e) $e.Property1 }
$func2 = [Func`2[Entity, object]]{ param($e) $e.Property2 }
$o.Load2($entity, [Func`2[Entity, object][]]@($func, $func2))
|
Why can't i return all cimsession functions from Powershell.Invoke() method in c#
Tag : chash , By : sayuki288
Date : March 29 2020, 07:55 AM
like below fixes the issue Add Reference to Microsoft.Management.Infrastructure Namespace Add `using Microsoft.Management.Infrastructure; static void Main(string [] args)
{
var csession = CimSession.Create("");
csession.TestConnection();
}
|
PowerShell invoke-sqlcmd Get-ChildItem : Cannot call method. The provider does not support the use of filters
Date : March 29 2020, 07:55 AM
|