1

I need to make an application which will run Power-Shell and run command Get-Dedupjob, but I received the following error:

the term "Get-Dedupjob" is not recognized as the name of a cmdlet, function, script file, or operable program.

Although mentioned command works properly when I run this in power-shell (show deduplication jobs).

I suppose that problem could be that program doesn't run command as administrator. Is there some solution ho to run this as admin? Or where could be the problem?

private void button5_Click(object sender, EventArgs e)
              {

                  // Get-Dedupjob


                  // create Powershell runspace
                  Runspace runspace = RunspaceFactory.CreateRunspace();

                  // open it
                  runspace.Open();

                  // create a pipeline and feed it the script text
                  Pipeline pipeline = runspace.CreatePipeline();
                  pipeline.Commands.AddScript("Get-Dedupjob");


                  // execute the script
                  Collection<PSObject> results = pipeline.Invoke();

                  // close the runspace
                  runspace.Close();

                  // convert the script result into a single string
                  StringBuilder stringBuilder = new StringBuilder();
                  foreach (PSObject obj in results)
                  {
                      stringBuilder.AppendLine(obj.ToString());
                  }
                  textBox1.Text = Convert.ToString(stringBuilder);
              }

2 Answers 2

3

You should import the corresponding psd1 file

Sign up to request clarification or add additional context in comments.

Comments

0
private void button5_Click(object sender, EventArgs e)
              {

                  // Get-Dedupjob


                  // create Powershell runspace
                  Runspace runspace = RunspaceFactory.CreateRunspace();

                  // open it
                  runspace.Open();

                  PowerShell ps = PowerShell.Create();
                  ps.Runspace = rs;
                  ps.AddCommand("Get-Dedupjob");


                  // execute the script
                  Collection<PSObject> results = ps.Invoke();



                  // convert the script result into a single string
                  StringBuilder stringBuilder = new StringBuilder();
                  foreach (PSObject obj in results)
                  {
                      stringBuilder.AppendLine(obj.ToString());
                  }
                  textBox1.Text = Convert.ToString(stringBuilder);

                  // close the runspace
                  runspace.Close();
              }

You will need to create powershell object to run the powershell commands. Following are the using statement imports you need to have for the above to work.

using System.Management.Automation;           // Windows PowerShell namespace. 
using System.Management.Automation.Runspaces; // Windows PowerShell namespace.

1 Comment

Thank you for your response but I already use these objects: using System.Management.Automation; using System.Management.Automation.Runspaces; using System.IO;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.