c# - Executing PowerShell Script with Parameters -
sup. i'm adapting this solution windows forms solution. far i've been able execute get-wulist
command no problems. doesn't seem go hide-wuupdate
. i've tried far:
public class powershellcontroller : ipowershell { //created @ global scope can fetch it. initialsessionstate initial; runspaceinvoke scriptinvoker; runspace runspace; powershell ps; //the view control iview view; //the helper gridviewprocessor class igridviewprocessor gp; //initializing controller - loads 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. take while load."); } public void setview(iview view, igridviewprocessor gp) { this.view = view; this.gp = gp; } public void getavailableupdates() { messagebox.show("ok. program kind of hang. normal." + "this means start looking updates " ); ienumerable<psobject> wulist; //placeholder ps executed command using (ps = powershell.create()) { //adds powershell command ps.commands.addcommand("get-wulist"); //executes powershell command wulist = ps.invoke(); } //loads model - can later on rewritten 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 view: view.addupdatetogrid(model); } public void hideselectedupdates(datagridview grid) { //gets selectedupdates 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"); } }
the method can't seem work hideselectedupdates(datagridview grid)
.
script gets executed , no exceptions thrown, doesn't seem reflect changes @ all.
any suggestions?
Comments
Post a Comment