Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interoping With PowerShell CmdLets

I've been writing some utilities that make use of PowerShell Cmdlets for App-V. The interesting part is Microsoft seems to only document the cmdlets and not the .net assemblies used behind the Powershell modules.

Now I'm familiar with P/Invoke and COM Interop and I've learned how to use System.Management.Automation to create a powershell session and invoke the cmdlets.

But something doesn't smell right to me. I'm basically writing my own wrapper classes to hide the powershell invocations from the rest of my code. It seems like I should either a) bypass powershell and go straight for the managed library behind it or b) there should be better mechanism for generating interop libraries for powershell cmdlets.

It seems like Microsoft is making a lot of use of PS CmdLets these days that it's essentially becoming a new API to interop with.

Am I missing something? What's a good strategy to use in this scenario?

like image 286
Christopher Painter Avatar asked Sep 18 '13 15:09

Christopher Painter


People also ask

What are cmdlets in PowerShell and how to use them?

These functions are native functions and perform various tasks. A cmdlet is a lightweight command that is used in the Windows PowerShell environment. At the time of writing any automation scripts, these commands are useful. I will tell you some examples, suppose you have some files you wanted to calculate file size, you can use PowerShell cmdlets.

How to get all CIM cmdlets exposed by a PowerShell module?

The PowerShell module name for these cmdlets is CimCmdlets,and we can use the Get-Command cmdlet to see the cmdlets exposed by this module. PS :> # Get all CIM Cmdlets PS :> Get-Command –module CimCmdlets CommandType Name ModuleName

How do I run Configuration Manager cmdlets in PowerShell?

For more information, see Configuration Manager SDK. Run Configuration Manager cmdlets and scripts in PowerShell from the Configuration Manager console or from a Windows PowerShell session. When you run Configuration Manager cmdlets by using the Configuration Manager console, your session automatically runs in the context of the site.

How do I connect to Windows PowerShell?

The easiest method to open PowerShell is directly from the Configuration Manager console. Launch the Configuration Manager console. In the upper-left corner, there's a blue rectangle. Select the white arrow in the blue rectangle, and choose Connect via Windows PowerShell.


1 Answers

You are right about the "smell". Bypassing the powershell is not the best way to write your utility, because undocumented managed library behind it are more likely to change silently than cmdlets. Using this approach can get you into trouble.

You are trying to combine hardly combinable things. Solution is to introduce a proxy that allows to combine your code with ps cmdlets in more natural way.

In your situation the ps script can be creates that communicates with cmdlets in natural way and provides the necessary functonality. In your c# code you can simply call the powershell.exe. If that doesn't smell right for you as well, then just use System.Management.Automation to create powershell session and invoke your script. This approach is safer, because now you communicate with documented ps cmdlets in your powershell proxy(natural powershell way), and communicate with your ps script from c# code (that gives you more control over the code)

like image 81
Max Tkachenko Avatar answered Oct 19 '22 12:10

Max Tkachenko