Quantcast
Channel: Virtual Machine Manager – General forum
Viewing all articles
Browse latest Browse all 2770

Invoke-SCScript in Action Add-In | Unknown Error

$
0
0

Hi,

I am creating an add-in for the virtual machine manager that will backup the selected VM(s) on the host.
I've come so far, but my script is not working.
I allways get the error message box.

What am I doing wrong? Is "Host" the right context in this case?

using Microsoft.SystemCenter.VirtualMachineManager;
using Microsoft.SystemCenter.VirtualMachineManager.UIAddIns;
using Microsoft.SystemCenter.VirtualMachineManager.UIAddIns.ContextTypes;
using Microsoft.VirtualManager.Remoting;
using System;
using System.AddIn;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Microsoft.VirtualManager.UI.AddIns.BackupAddIn
{
    [AddIn("Make Backup")]
    public class BackupAddIn : ActionAddInBase
    {
        public override bool CheckIfEnabledFor(IList<ContextObject> contextObjects)
        {
            if (contextObjects != null && contextObjects.Count > 0)
            {
                foreach (var host in contextObjects.OfType<HostContext>())
                {
                    if (host.ComputerState != ComputerState.Responding)
                    {
                        return false;
                    }
                }
                return true;
            }

            return false;
        }

        public override void PerformAction(IList<ContextObject> contextObjects)
        {
            if (contextObjects != null)
            {
                VMContext VMs = contextObjects.OfType<VMContext>().FirstOrDefault();
                if (VMs != null)
                {
                    String expDir = Microsoft.VisualBasic.Interaction.InputBox("Enter the export path for the selected virtual machine(s) on the host.", "Export path", "C:\\ClusterStorage\\Volume2\\");
                    MessageBox.Show(expDir); //Debug only
                    if (!expDir.Equals("")) //Aborting the InputBox returns an empty string. In this case we simply skip the add-in code.
                    {
                        foreach (var vm in contextObjects.OfType<VMContext>())
                        {
                            exportVM(vm, vm.VMHostId.ToString(), expDir);
                        }
                    }
                }
            }
        }

        public void execPSScript(String nScr, String hostID)
        {
            System.Windows.Forms.MessageBox.Show("execPSScript(" + nScr + "," + hostID + ") was called"); //Debug only
            this.PowerShellContext.ExecuteScript<Host>(//Maybe it has to be another context
                        scvmmPsCommand(nScr, hostID),
                        (error, inf) =>
                        {
                            if (error == null)
                            {
                                MessageBox.Show("Backup successfully created."); //only show this when the final command was executed
                            }
                            else
                            {
                                MessageBox.Show("An error occured.\nSomething must be wrong with this script: \n" + scvmmPsCommand(nScr, hostID));
                            }
                        });
        }

        public void exportVM(VMContext VM, String hostID, String nExpPath)
        {
            string hostPsCommand = string.Format("export-vm -name {0} -path {1}", VM.Name, nExpPath); //Not shure if this will work either
            execPSScript(hostPsCommand, hostID);
        }



        public String scvmmPsCommand (String nCmnd, String hostID) {
            return string.Format("Invoke-SCScriptCommand -VMHost {0} -Executable \"C:\\Windows\\System32\\WindowsPowershell\\v1.0\\powershell.exe\" -CommandParameters \"{1}\" -TimeoutSeconds 60", hostID, nCmnd);
            //return string.Format("Invoke-SCScriptCommand -VMHost {0} -Executable \"C:\\Windows\\System32\\WindowsPowershell\\v1.0\\powershell.exe\" -TimeoutSeconds 60", hostID);
            //return string.Format("Invoke-SCScriptCommand -VMHost {0} -Executable \"cmd.exe\" -TimeoutSeconds 60", hostID);
        }
    }
}

Thank you in advance. :)


Viewing all articles
Browse latest Browse all 2770

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>