i want to filter a Property from a PSObject and convert it to a string Value for further use. In my case the return value is a litte bit complex and does not allow .ToString() Convertion.
I have fount a solution, but this will only work in this case, and i can get a lot of errors if the input values are changing.
So ist there a more generic way to get this values?
using (PowerShell ps = PowerShell.Create())
{
ps.AddCommand("get-help").AddArgument("C:\\Users\\kritzinger\\Work\\test.ps1");
Collection<PSObject> PSOutput = ps.Invoke();
foreach (PSObject outputItem in PSOutput)
{
dynamic description = outputItem.Properties["description"].Value;
// not working
string foo = description.ToString();
// foo = "System.Management.Automation.PSObject[]"
foreach (dynamic item in description)
{
// not working
string bar = item.ToString();
// bar = ""
// working!
string moo = item.Text;
// moo = "here is the description of the poweshell script"
}
}
}
Use this
string foo = Convert.ToString(description).ToString();
It will do the trick!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.