saving dynamically created data + c# + powershell -
i have application runs powershell script , need save data powershell script returns.
an example of script might passed application is:
get-netadapter | select-object -property name, status here c# code running script , saving results:
powershell powershell = powershell.create(); powershell.runspace = runspacefactory.createrunspace(); powershell.runspace.open(); powershell.addscript(scripttext); var results = powershell.invoke(); i filter members of returned collection find i'm looking for
var firstobject = results[0].members; foreach(psmemberinfo memberinfo in firstobject) { if (!listofstrings.contains(memberinfo.name)) { //do -- stuck } } the goal not hard code anything, need dynamically create table or something, can hold return memberinfo (which can number of properties) , need return values of said memberinfo properties , save sql table in proper format.
am missing in powershell output or there framework format output it's easier use?
how save data not know about, other amount of items , name of properties?
i'm thinking of saving data sql table , there loading view of kind.
i'm trying find solution work number of properties appreciated.
try using dictionary object or create custom class , store each psmemberinfo object list. table of name,value stuff , other metadata. try commenting our other scripttext , has different property , still dictionary object gets properties. demo purposes using 1 object result set.
update: there no first in script type of object being returned doing powershell.invoke().first().members.
static void main(string[] args) { //var scripttext = "get-process | select *"; //var scripttext = "get-service"; var scripttext = "get-service | select servicename, status"; powershell powershell = powershell.create(); powershell.runspace = runspacefactory.createrunspace(); powershell.runspace.open(); powershell.addscript(scripttext); dictionary<string, object> dictprop = new dictionary<string, object>(); foreach (psmemberinfo item in powershell.invoke().first().members) { try { dictprop.add(item.name, item.value); console.writeline("name = {0}, value = {1}", item.name, item.value); } catch (exception ex) { //null value invocation exception. } } console.read(); }
Comments
Post a Comment