Click here to Skip to main content
15,851,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Am using the below code to connect to powercli and run a powershell script in it and when i try to do so am getting an error at
C#
pipeline.Invoke();


error message as : 'System.Management.Automation.PSSecurity' occured in System.Management.Automation.dll

Additional information: File 'C:\script\resource.ps1' is not recognised as the name of cmdlet, function, script, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again
, I have no idea how to solve it

Here is the code:

C#
private void Powercli_Click(object sender, EventArgs e)
        {
            RunspaceConfiguration config = RunspaceConfiguration.Create();
            Runspace runspace = RunspaceFactory.CreateRunspace(config);
            
            // open it  
            runspace.Open();
            
            try
            {
                RunspaceInvoke scriptinvoker = new RunspaceInvoke(runspace);
                // import PowerCLI ViCore snapin  
                PSSnapInException warning;
                config.AddPSSnapIn("VMware.VimAutomation.Core", out warning);
                if (warning != null)
                {
                    throw warning;
                }
                // create a pipeline and feed it the script text  
                Pipeline pipeline = runspace.CreatePipeline();
                // Create Command to Set Na Option  
               
                var connectVI = new Command("Connect-VIServer");
                connectVI.Parameters.Add("Server", "192.168.123.108");
                connectVI.Parameters.Add("Protocol", "https");
                connectVI.Parameters.Add("User", "root");
                connectVI.Parameters.Add("Password", "assign");
                Command mycommand = new Command("C:\\script\\resource.ps1");
                
                  // Add Command to Pipeline  
                     pipeline.Commands.Add(connectVI);
                    
                    pipeline.Commands.Add(mycommand);
                     //pipeline.Commands.Add(mycommand1);
                   //Execute by invoking  

                pipeline.Invoke();

                if (pipeline.HadErrors)
                {
                    MessageBox.Show(pipeline.Error.ToString(), "Error occurred");
                }
            }
            finally
            {
                runspace.Close();
            }

        }
Posted
Comments
khan_aamir47 2-Mar-20 11:55am    
Hi Shaik,
Did u find the solution for this, iam also stuck in a similar issue. seeking for help.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900