Sup. I'm adapting this solution to a Windows Forms solution. So far I've been able to execute the `Get-WUList` command with no problems. But it doesn't seem to go well with the `Hide-WUUpdate`. This is what I've tried so far:
publicclassPowerShellController:IPowerShell{//Created at a global scope so anyone can fetch it.InitialSessionState initial;RunspaceInvoke scriptInvoker;Runspace runspace;PowerShell ps;//The View to ControlIView view;//The Helper GridViewProcessor classIGridViewProcessor gp;//Initializing the Controller - Loads the Module.publicPowerShellController(){
initial =InitialSessionState.CreateDefault();
initial.ImportPSModule(newstring[]{@"C:\Users\Jose\Documents\WindowsPowerShell\Modules\PSWindowsUpdate\PSWindowsUpdate.psd1"});
scriptInvoker =newRunspaceInvoke();
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.");}publicvoidSetView(IView view,IGridViewProcessor gp){this.view = view;this.gp = gp;}publicvoidGetAvailableUpdates(){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 Commandusing(ps =PowerShell.Create()){//Adds the PowerShell Command
ps.Commands.AddCommand("Get-WUList");//Executes the PowerShell commandWUList= ps.Invoke();}//Loads the Model - Can be later on rewritten for Ninject Support.List<WindowsUpdate> model =newList<WindowsUpdate>();int id =1;foreach(PSObject result inWUList){WindowsUpdate item =newWindowsUpdate{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);}publicvoidHideSelectedUpdates(DataGridView grid){//Gets SelectedUpdates to the WindowsUpdate modelvarSelectedUpdates= gp.GetSelectedUpdates(grid);using(ps =PowerShell.Create()){foreach(var update inSelectedUpdates){
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");}}
The method I can't seem to work is the `HideSelectedUpdates(DataGridView grid)`.
Script gets executed and no exceptions are thrown, but it doesn't seem to reflect any changes at all.
Question
Jose_49
Sup. I'm adapting this solution to a Windows Forms solution. So far I've been able to execute the `Get-WUList` command with no problems. But it doesn't seem to go well with the `Hide-WUUpdate`. This is what I've tried so far:
The method I can't seem to work is the `HideSelectedUpdates(DataGridView grid)`.
Script gets executed and no exceptions are thrown, but it doesn't seem to reflect any changes at all.
Any suggestions?
Link to comment
https://www.neowin.net/forum/topic/1263570-executing-a-powershell-script-from-c-with-parameters/Share on other sites
4 answers to this question
Recommended Posts