I created a powershell object via .net to invoke commands. When I invoke normal commands like 'Get-Process' I had no problems:
ps.AddCommand("Get-Process").AddParameter(...).Invoke()
but I'm not able to invoke a .net method with the syntax "[namespace.class]::method", just to make an example to invoke [System.IO.File]::Exists("c:\\boo.txt").
I tried with
ps.AddCommand("[System.IO.File]::Exists(\"c:\\boo.txt\")").Invoke()
ps.AddCommand("[System.IO.File]::Exists").AddArgument("c:\\boo.txt").Invoke()
and some others. It always throws an exception which says that the command specified is not recognized.
There is a way to invoke that type of command? Thanks
You need to add script
to the pipeline since calling out to .NET requires script ie .NET methods are not considered PowerShell commands
eg:
static void Main()
{
PowerShell ps = PowerShell.Create();
ps.AddScript(@"[IO.File]::Exists('C:\Users\Keith\foo.txt')");
foreach (PSObject result in ps.Invoke())
{
Console.WriteLine(result);
}
}
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.