I'm trying to learn how to incorporate PowerShell into a WPF/C# GUI I'm working on. I'd like the user to be able to click a button, have the PowerShell script execute, and then returns the information and have it write the output to a richtextbox
.
Here is the PowerShell:
Function Get-MappedPrinters {
[Cmdletbinding()]
Param(
[alias('dnsHostName')]
[Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
[string]$ComputerName = $Env:COMPUTERNAME
)
$id = Get-WmiObject -Class win32_computersystem -ComputerName $ComputerName |
Select-Object -ExpandProperty Username |
ForEach-Object { ([System.Security.Principal.NTAccount]$_).Translate([System.Security.Principal.SecurityIdentifier]).Value }
$path = "Registry::\HKEY_USERS\$id\Printers\Connections\"
Invoke-Command -Computername $ComputerName -ScriptBlock {param($path)(Get-Childitem $path | Select PSChildName)} -ArgumentList $path | Select -Property * -ExcludeProperty PSComputerName, RunspaceId, PSShowComputerName
}
And here is the C#
private void SystemTypeButton_Click(object sender, RoutedEventArgs e)
{
using (PowerShell ps = PowerShell.Create())
{
ps.AddScript(File.ReadAllText(@"..\..\Scripts\systemtype.ps1"), true).AddParameter("ComputerName", ComputerNameTextBox.Text).AddCommand("Out-String");
var results = ps.Invoke();
MainRichTextBox.AppendText(results.ToString());
}
}
However, it is only returning the object and not its properties. "System.Collections.ObjectModel.Collection1[System.Management.Automation.PSObject]"
.
Is there a way to iterate through the object?