8 Replies - 3117 Views - Last Post: 30 July 2015 - 02:57 PM Rate Topic: -----

#1 ejji09   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-May 15

How to Open PowerCli and run commands through c#

Posted 29 July 2015 - 02:42 AM

Am using the following code to Open PowerCli from C# and run commands on PowerCli through a button click.. after clicking the button in the debugger output am getting "The thread 0x104c has exited with code 259 (0x103)." which means my code was not succesfull . i dont know whats going wrong in my code can you please suggest me whats wrong am doing in my code?

Here is my code:

private void Powercli_Click(object sender, EventArgs e)
        {
            RunspaceConfiguration config = RunspaceConfiguration.Create();
            Runspace runspace = RunspaceFactory.CreateRunspace(config);
            // open it
            runspace.Open();
            try
            {
                // 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 access = new Command("Connect-VIServer");
                access.Parameters.Add("Server", "192.168.123.108");
                access.Parameters.Add("Protocol", "https");
                access.Parameters.Add("User", "root");
                access.Parameters.Add("Password", "assign");
              // Add Command to Pipeline 
                pipeline.Commands.Add(access);
                //Execute by invoking
                pipeline.Invoke();
            }
            finally
            {
                runspace.Close();
            }


Is This A Good Question/Topic? 0
  • +

Replies To: How to Open PowerCli and run commands through c#

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How to Open PowerCli and run commands through c#

Posted 29 July 2015 - 04:10 AM

This looks like a very straight-forward debugging situation.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.
Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute, check the condition of each of variable's run-time value, and watch the logic unfold right before your eyes.
Visualizing what your code does will let you see why it behaves the way it does.
It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

In addition to FAQ 2 in the list below, also check my signature block for a link on how to look at your variables' values at run-time.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2013
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 ejji09   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-May 15

Re: How to Open PowerCli and run commands through c#

Posted 30 July 2015 - 02:32 AM

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

pipeline.Invoke();
line no: 37


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 my code:

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();
            }
 
        }

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,619
  • Joined: 12-December 12

Re: How to Open PowerCli and run commands through c#

Posted 30 July 2015 - 02:35 AM

Questions merged. Do not start a new topic for the same question.
Was This Post Helpful? 0
  • +
  • -

#5 ejji09   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-May 15

Re: How to Open PowerCli and run commands through c#

Posted 30 July 2015 - 02:47 AM

Can you suggest me what's going wrong in my code
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is online

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,619
  • Joined: 12-December 12

Re: How to Open PowerCli and run commands through c#

Posted 30 July 2015 - 02:58 AM

I know nothing about PowerCli but

Quote

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

this seems a very clear error message, even suggesting a solution.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6293
  • View blog
  • Posts: 21,632
  • Joined: 05-May 12

Re: How to Open PowerCli and run commands through c#

Posted 30 July 2015 - 05:58 AM

Be aware that path, "C:\script\resource.ps1" is expected to exist on the VM that you are sending commands to. Does it exist there, or does it exist on your host machine?
Was This Post Helpful? 1
  • +
  • -

#8 ejji09   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-May 15

Re: How to Open PowerCli and run commands through c#

Posted 30 July 2015 - 06:24 AM

I have chnaged the line to

Command mycommand = new Command("C:\\Program Files (x86)\\VMware\\Infrastructure\\vSphere PowerCLI\\resource.ps1");


but still the error is same

This post has been edited by ejji09: 30 July 2015 - 06:27 AM

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6293
  • View blog
  • Posts: 21,632
  • Joined: 05-May 12

Re: How to Open PowerCli and run commands through c#

Posted 30 July 2015 - 02:57 PM

All you did is change the path. You have yet to confirm to us that the file exists at the paths you have been specifying.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1