c# - Powershell-string to listview -
i have created c# application that, among other things, should installed programs on remote computer using powershell code. can run code adds items single column in single row , i'm not able scroll in it.
can tell me how add each program new row, headers are: program, vendor, version ? here ps1 code:
invoke-command -computername $computername -scriptblock { $swinstalled = get-wmiobject win32_product | select @{label="program";expression={$_.name}}, version, vendor | sort-object program $swinstalled | ft }
here relevant c# code:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes; using system.collections.objectmodel; using system.management.automation; using system.management.automation.runspaces; using system.io; namespace clientcheck { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { private string runscript(string scripttext) { // create powershell runspace runspace runspace = runspacefactory.createrunspace(); // open runspace.open(); runspace.sessionstateproxy.setvariable("computername", compnameinput.text); runspace.sessionstateproxy.setvariable("user", usernameinput.text); // create pipeline , feed script text pipeline pipeline = runspace.createpipeline(); pipeline.commands.addscript(scripttext); // add command transform script output objects nicely formatted strings // remove line actual objects script returns. example, script // "get-process" returns collection of system.diagnostics.process instances. pipeline.commands.add("out-string"); // execute script collection<psobject> results = pipeline.invoke(); // close runspace runspace.close(); // convert script result single string stringbuilder stringbuilder = new stringbuilder(); foreach (psobject obj in results) { stringbuilder.appendline(obj.tostring()); } // return results of script has // been converted text return stringbuilder.tostring(); } // helper method takes script path, loads script // variable, , passes variable runscript method // execute contents private string loadscript(string filename) { try { // create instance of streamreader read our file. // using statement closes streamreader. using (streamreader sr = new streamreader(filename)) { // use string builder our lines file stringbuilder filecontents = new stringbuilder(); // string hold current line string curline; // loop through our file , read each line our // stringbuilder go along while ((curline = sr.readline()) != null) { // read each line , make sure add // linefeed readline() method strips off filecontents.append(curline + "\n"); } // call runscript , pass in our file contents // converted string return filecontents.tostring(); } } catch (exception e) { // let user know went wrong. string errortext = "the file not read:"; errortext += e.message + "\n"; return errortext; } private void getswbutton_click(object sender, routedeventargs e) { swlist.items.clear(); swlist.items.add((runscript(loadscript(@"c:\get_installed_software.ps1")))); } } }
also, here xaml code:
<listview x:name="swlist" height="404" margin="36,169,36,51" verticalalignment="center" rendertransformorigin="0.5,0.5" scrollviewer.horizontalscrollbarvisibility="disabled" borderthickness="0" visibility="hidden" grid.row="2"> <listview.rendertransform> <transformgroup> <scaletransform/> <skewtransform anglex="-0.0"/> <rotatetransform/> <translatetransform x="0.346"/> </transformgroup> </listview.rendertransform> <listview.view> <gridview> <gridviewcolumn header="program" displaymemberbinding ="{binding 'program'}" width="440"/> <gridviewcolumn header="vendor" displaymemberbinding="{binding 'vendor'}" width="281"/> <gridviewcolumn header="version" displaymemberbinding ="{binding 'version'}" width="150"/> </gridview> </listview.view> </listview>
it looks you're looping through psobject results , appending them single stringbuilder. when you're adding results runscript swlist items, you're adding single string. that's why it's showing in 1 row.
and mike indicated in comment, using format-table cmdlet @ end of script undoing nice object properties generated powershell; causing show in single column.
here's example of updated runscript method returns collection of objects instead of single string. note how changed method signature return collection of psobjects instead of string. commented out line adds out-string end of pipeline. means you'll raw psobjects returned function. depending on script run, baseobject of these psobjects of different type. can access baseobject property of these psobjects values need.
private collection<psobject> runscript(string scripttext) { // create powershell runspace runspace runspace = runspacefactory.createrunspace(); // open runspace.open(); runspace.sessionstateproxy.setvariable("computername", compnameinput.text); runspace.sessionstateproxy.setvariable("user", usernameinput.text); // create pipeline , feed script text pipeline pipeline = runspace.createpipeline(); pipeline.commands.addscript(scripttext); // add command transform script output objects nicely formatted strings // remove line actual objects script returns. example, script // "get-process" returns collection of system.diagnostics.process instances. // pipeline.commands.add("out-string"); // execute script collection<psobject> results = pipeline.invoke(); // close runspace runspace.close(); return results;
}
Comments
Post a Comment