0

I have my unit test cases written which tests the Powershell cmdlets built in same solution. All test cases works fine and passes successfully when running locally but once I push it in my Azure devops pipeline, the test cases fails. the error I get is

"CosmosScript.ps1 cannot be loaded because running scripts is disabled on this system.For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170."

. How can I fix this? what wrong am i doing ?

here is my unit test project code:

public class CosmosDataFixture : IDisposable
    {
        public static readonly string CosmosEndpoint = "https://localhost:8081";
        public static readonly string EmulatorKey = "testkey";
        public static readonly string DatabaseId = "testdb";
        public static readonly string RecordingCollection = "testcollection";
        public static string Root = Directory.GetParent( Directory.GetCurrentDirectory() ).Parent.Parent.FullName;
        public static DocumentClient client { get; private set; }

        public void ReadConfigAsync()
        {
            client = new DocumentClient( new Uri( CosmosEndpoint ), EmulatorKey,
                 new ConnectionPolicy
                 {
                     ConnectionMode = ConnectionMode.Direct,
                     ConnectionProtocol = Protocol.Tcp

                 } );
        }
       public void ReadAllData(Document client){}

        public void CreateDatabaseFromPowerShell()
        {
            string path = Path.Combine( Root, @"TestData\CosmosScript.ps1" );
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript( path );
            Collection<PSObject> results = pipeline.Invoke();
            runspace.Close();
            ReadAllData( client );

        }
 public CosmosDataFixture()
        {
            ReadConfigAsync();
            CreateDatabaseFromPowerShell();
            ReadAllData( client );

        }

        public void Dispose()
        {
            DeleteDatabaseFromPowerShell();
        }
    }   
    public class CosmosDataTests : IClassFixture<CosmosDataFixture>
    {
    [fact]
     ....test cases...
ZZZSharePoint
  • 1,163
  • 1
  • 19
  • 54
  • try run `Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine` first(you may need run as admin) – Lei Yang Jun 28 '21 at 09:42
  • I have Powershell script which I am calling in each test case. should I add the above command in the CosmosScript.ps1 and how do I run the command in pipeline as Admin – ZZZSharePoint Jun 28 '21 at 09:44
  • 1
    Azure devops pipeline should have built in mechanism to run powershell. so i think firstly you should avoid invoking powershell by C# code. – Lei Yang Jun 28 '21 at 09:48
  • right now I am invoking and it runs successfully locally . how to run this on devops pipeline without any error – ZZZSharePoint Jun 28 '21 at 10:05
  • no it doesnt..tried everything mentioned her, the scripts still fail in devops pipeline – ZZZSharePoint Jun 28 '21 at 10:38
  • mine is happening on azure devops pipeline. locally its works fine. The question should not be closed – ZZZSharePoint Jun 28 '21 at 11:12
  • 1
    @ZZZSharePoint, I suggest you create a new issue with enough details as PowerShell version, VM details, etc.,testing background (as what happens when just call the `.ps1` script, e.g. from CMD), in the question to rule any obvious/known issues related to the [PowerShell Execution Policies](https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_execution_policies). In other words, try to add enough content to the question to exclude *your* question from the suggested duplicate question. You might use this [how to ask](https://stackoverflow.com/help/how-to-ask) page. – iRon Jun 28 '21 at 11:45
  • [This answer](https://stackoverflow.com/a/67994020/45375) shows how you set the execution policy (for the current process) directly via the SDK. – mklement0 Jun 28 '21 at 13:29

0 Answers0