Question Problem with PowerShell

Joined
Dec 2, 2016
Messages
1
Programming Experience
Beginner
Trying on different ways to execute the command "get-wbsummary" from PowerShell in c #.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;


namespace AgentBackupIoA
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MessageBox.Show(RunScript("Get-Wbsummary"));
}

private string RunScript(string scriptText)
{
// 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-module WindowsServerBackup");
pipeline.Commands.AddScript(scriptText);

// add an extra command to transform the script output objects into nicely formatted strings
// remove this line to get the actual objects that the script returns. For example, the script
// "Get-Process" returns a collection of System.Diagnostics.Process instances.
pipeline.Commands.Add("Out-String");

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

return stringBuilder.ToString();
}
}
}




But I do not want to work. What am I doing wrong ?
An exception of type 'System.Management.Automation.CommandNotFoundException' occurred in System.Management.Automation.dll but was not handled in user code

Additional information: The term 'Get-Wbsummary' is not recognized as the name of a cmdlet, function, script file, 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 looked through a lot of examples and I have no idea. Sorry for my bad English.:dispirited:
 
Back
Top Bottom