0

I am using XUnit test framework and have below Test case created to test my powershell cmndlets and there is no error on build. My PowerShell project is also built in same solution and has been referenced correctly in my test project.

below PowerShell command works locally and give me desired result(as shown below)

$rec = Get-Recording -Project 'Demo'
$rec.Count
64

I am trying to test the same in my unit test case and here is my code. I am not getting any result here, one message which I get in Invoke() line is "Cmdlets derived from PSCmdlet cannot be invoked directly.". can anyone help me in completing this test case and get me the result as I am getting in above Powershell window.

[Fact]
    public void TestGetRecordings()
        {
        // Arrange.
        
        var cmdlet = new GetRecordingsCmd()
        {
            Project = "Demo"
        };
        

        // Act.
        var result = cmdlet.Invoke().OfType<List<string>>();
       // Assert.True( result, 64 );
       

My Powershell cmndlet is defined like this. I am just mentioning the class here and how my actual class is inherited:

public class TorgCmd : PSCmdlet
public abstract class GetDocumentsBaseCmd : TorgCmd {}
public class GetRecordingsCmd
        : GetDocumentsBaseCmd
    {}
ZZZSharePoint
  • 1,163
  • 1
  • 19
  • 54
  • Have you tried creating an instance of PowerShell to run the cmdlet in? – Ash Jun 20 '21 at 19:21
  • By the way, you also need to use the full [Microsoft.PowerShell.SDK package](https://www.nuget.org/packages/Microsoft.PowerShell.SDK/) in your test project if you aren't already. A lot of people have issues with testing initially when they use the StandardLibrary. – Ash Jun 20 '21 at 19:32
  • I followed the blog and couldnt see any thing on creating instance.. can you elaborate what you want me to try https://github.com/deadlydog/PowerShellCmdletInCSharpExample/blob/master/src/PowerShellCmdletInCSharpExample.Tests/GetRepeatedPhraseCmdletTests.cs – ZZZSharePoint Jun 21 '21 at 03:51
  • on adding Powershell.SDk package, I get an error on Invoke as "The type 'PSCmdlet' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Management.Automation'." – ZZZSharePoint Jun 21 '21 at 03:57
  • Do you have a `using System.Management.Automation` reference at the top of the file? Are you using the relevant version of the SDK? Does your actual cmdlet inherit from `Cmdlet` or `PSCmdlet`? – Ash Jun 21 '21 at 08:23
  • its inherited from PSCmdlet and i do have System.Management.Automation at the top – ZZZSharePoint Jun 22 '21 at 08:30
  • not sure if there is difference between Cmdlet and PSCmdlet as I see PSCmdlet is derived from Cmdlet – ZZZSharePoint Jun 22 '21 at 08:31
  • I believe that [cmdlets that derive from PSCmdlet](https://learn.microsoft.com/en-us/powershell/scripting/developer/cmdlet/how-to-invoke-a-cmdlet-from-within-a-cmdlet?view=powershell-7.1) cannot be invoked directly. See [this answer about invoking it as a script](https://stackoverflow.com/a/47010365/9164015). PSCmdlet has extensive dependencies on the PowerShell runtime. – Ash Jun 22 '21 at 08:56
  • i have added how my cmdlets classes are actually derived. – ZZZSharePoint Jun 22 '21 at 09:13

0 Answers0