0

Here is the code:

static String checkBackUp()
{
    Runspace runspace = RunspaceFactory.CreateRunspace();

    runspace.Open();

    Pipeline pipeline = runspace.CreatePipeline();                     
    pipeline.Commands.Add("Get-WBSummary");
    pipeline.Commands.Add("Out-String");

    Collection<PSObject> results = new Collection<PSObject>();
    try
    {
        results = pipeline.Invoke();
    }
    catch (Exception ex)
    {
        results.Add(new PSObject((object)ex.Message));
    }

    runspace.Close();

    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject obj in results)
    {
        stringBuilder.AppendLine(obj.ToString());
    }

    return stringBuilder.ToString();
}

The problem is that this runs every cmdlet (like Get-Process for example) but when I try to verify if a backup has been made (Get-WBSummary), it spits out the following error:

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.

However when I put the command straight into PowerShell, it executes the command. I have already tried to add a SnapIn but this did not work.

What am I doing wrong here?

2 Answers 2

1

Get-WBSummary isn't a regular built-in Powershell cmdlet. You'll need to do

Add-PSSnapin Windows.ServerBackup

at some point in your code after the runspace is initialised.

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

1 Comment

Like i said in my question i did :) and it didnt help. i had it like this : Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.Add("Add-Pssnapin" Windows.serverbackup"); Command addSnapin = new Command("Add-PSSnapin"); addSnapin.Parameters.Add("Name", "Windows.serverbackup"); pipeline.Commands.Add(addSnapin);
0

You'll have to create an initial session state and add the snapin. Here is how to do it

initialSession = InitialSessionState.CreateDefault();
initialSession.ImportPSModule(new[] {"Path\to\module\here"});

Comments

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.