使用参数

时间:2015-07-08 20:33:40

标签: c# powershell datagridview

我正在使this solution适应Windows窗体解决方案。到目前为止,我已经能够毫无问题地执行Get-WUList命令。但它似乎与Hide-WUUpdate相吻合。这是我到目前为止所尝试的:

public class PowerShellController : IPowerShell
{

    //Created at a global scope so anyone can fetch it.
    InitialSessionState initial;
    RunspaceInvoke scriptInvoker;
    Runspace runspace;
    PowerShell ps;

    //The View to Control
    IView view;

    //The Helper GridViewProcessor class
    IGridViewProcessor gp;

    //Initializing the Controller - Loads the Module.
    public PowerShellController()
    {
        initial = InitialSessionState.CreateDefault();
        initial.ImportPSModule(new string[] { @"C:\Users\Jose\Documents\WindowsPowerShell\Modules\PSWindowsUpdate\PSWindowsUpdate.psd1" });
        scriptInvoker = new RunspaceInvoke();
        scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted -Scope Process");

        runspace = RunspaceFactory.CreateRunspace(initial);
        runspace.Open();

        using (ps = PowerShell.Create())
        {
            ps.Runspace = runspace;
        }

        //Console.WriteLine("Please Wait. This will take a while to load.");

    }


    public void SetView(IView view, IGridViewProcessor gp)
    {
        this.view = view;
        this.gp = gp;
    }


    public void GetAvailableUpdates()
    {

        MessageBox.Show("Ok. The program will kind of hang. This is normal." +
                        "This Means that it will start looking for updates "
            );

        IEnumerable<PSObject> WUList; //Placeholder for the PS Executed Command

        using (ps = PowerShell.Create())
        {
            //Adds the PowerShell Command
            ps.Commands.AddCommand("Get-WUList");
            //Executes the PowerShell command
            WUList = ps.Invoke();
        }

        //Loads the Model - Can be later on rewritten for Ninject Support.
        List<WindowsUpdate> model = new List<WindowsUpdate>();
        int id = 1;
        foreach (PSObject result in WUList)
        {
            WindowsUpdate item = new WindowsUpdate
            {
                Id = id,
                Name = result.Members["Title"].Value.ToString(),
                Size = result.Members["Size"].Value.ToString(),
                Type = UpdateType.Undefined,

            };
            model.Add(item);
            id++; //Icnrease ID count
            //Console.WriteLine("Update Name {0} --- Size: {1}", result.Members["Title"].Value.ToString(), result.Members["Size"].Value.ToString());
        }

        //Adds it to the view:
        view.AddUpdateToGrid(model);
    }

    public void HideSelectedUpdates(DataGridView grid)
    {
        //Gets SelectedUpdates to the WindowsUpdate model
        var SelectedUpdates = gp.GetSelectedUpdates(grid);
        using (ps = PowerShell.Create())
        {
            foreach (var update in SelectedUpdates)
            {
                ps.Commands.Clear();
                ps.Commands.AddCommand("Hide-WUUpdate").AddParameter("Title",update.Name).AddParameter("Confirm", false);

                //ps.Commands.AddCommand("Hide-WUUpdate -Title \""+update.Name+"\"");
               var result = ps.Invoke();

            }

        }

        MessageBox.Show("Updates Have been hidden");

    }

}

我似乎无法工作的方法是HideSelectedUpdates(DataGridView grid)

脚本被执行并且不会抛出任何异常,但它似乎根本不反映任何更改。

有什么建议吗?

0 个答案:

没有答案