Hosted PowerShell cannot see Cmdlets in the same Assembly

前端 未结 3 1217
暖寄归人
暖寄归人 2020-12-17 22:21

I\'m trying to run PowerShell scripts from my C# code, that will use custom Cmdlets from the assembly that runs them. Here is the code:



        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-17 23:02

    You do need to register your cmdlet before you can use it in a Powershell session. You'll generally do that by way of a Powershell Snap-In. Here's a high-level overview of the process:

    • Create custom cmdlets (you've already done this part)
    • Create a Powershell Snap-in which contains and describes your cmdlets
    • Install the new Snap-in on your system using Installutil
    • Add the Snap-in to a specific Powershell session using Add-PSSnapin

    There are a couple useful articles on MSDN that explain the process pretty thoroughly:

    • Developing with Windows PowerShell
    • How to Create a Windows PowerShell Snap-in

    There's also a 2-part series on ByteBlocks that discusses writing custom cmdlets. That series may be your best bet, since you seem to have done the equivalent of part 1 already. You may be able to just use part 2 as a quick reference and be good to go.

    • How to Write a Custom Cmdlet - Part 1
    • How to Write a Custom Cmdlet - Part 2

提交回复
热议问题