Loop through WPF controls in Powershell

Hi,

I am attempting to loop through all the controls used in PS via a WPF form. At this point I am just trying to get all the controls recognised but it it does not seem to be seeing all the children

 

I have a stackpanel - WPF code here - not the complete code of the project

<StackPanel x:Name=“spBasicDRVSTART” Grid.Row=“0” Orientation=“Horizontal” >
<Label x:Name=“lblPullService” Grid.Row=“0” Margin=“3”>
<CheckBox x:Name=“chkPullService” Content=“Pull Service” VerticalContentAlignment=“Center” />
</Label>
<Label x:Name=“lblAudit” Grid.Row=“0” Margin=“3”>
<CheckBox x:Name=“chkAudit” Content=“Audit” VerticalContentAlignment=“Center” />
</Label>
<Grid x:Name=“grdAuditHostname” Visibility=“Collapsed” Width=“167” Margin=“3,0”>
<Grid.RowDefinitions>
<RowDefinition Height=“25.6”/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content=“Audit Hostname” Height=“26” Grid.RowSpan=“2” VerticalAlignment=“Top” HorizontalAlignment=“Left” Margin=“3” />
<TextBox Grid.Row=“1” Name=“AuditHostname” BorderBrush=“Black” />
</Grid>

<Grid x:Name=“grdAuditInterval” Visibility=“Collapsed” Width=“167” Margin=“3,0”>
<Grid.RowDefinitions>
<RowDefinition Height=“25.6”/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content=“Audit Interval (Hours)” Height=“26” Grid.RowSpan=“2” VerticalAlignment=“Top” HorizontalAlignment=“Left” Margin=“3” />
<TextBox Grid.Row=“1” Name=“AuditInterval” BorderBrush=“Black” HorizontalAlignment=“Center” />
</Grid>
<Label x:Name=“lblDirectSecureRelease”>
<CheckBox Content=“Direct Secure Release” VerticalContentAlignment=“Center” Margin=“3”/>
</Label>
<Label x:Name=“lblPrintPolicy” >
<CheckBox Content=“Print Policy” VerticalContentAlignment=“Center” Margin=“3”/>
</Label>
<Label x:Name=“lblUpdate” >
<CheckBox x:Name=“chkUpdate” Content=“Update” VerticalContentAlignment=“Center” Margin=“3” />
</Label>
<Grid x:Name=“grdUpdateInterval” Visibility=“Visible” Width=“167” Margin=“3,0”>
<Grid.RowDefinitions>
<RowDefinition Height=“25.6”/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content=“Update Interval (Minutes)” Height=“26” Grid.RowSpan=“2” VerticalAlignment=“Top” HorizontalAlignment=“Left” Margin=“3” />
<TextBox Grid.Row=“1” Name=“UpdateInterval” BorderBrush=“Black” Text=“360” VerticalAlignment=“Center” Margin=“10,4.4,32,3.6” Width=“125” HorizontalAlignment=“Center” />
</Grid>
</StackPanel>

 

I have a function that I call using Recursive $spBasicDRVSTART

Function Recursive ($Control)
{
$Controls = $Control.Children
foreach ($ctl in $Controls)
{
Write-Host $ctl.Name
if ($ctl.HasChildren)
{
Recursive $ctl
}
}
}

The function outputs the direct children of the stackpanel but not the children of the children.

e.g. <Label x:Name=“lblPullService” Grid.Row=“0” Margin=“3”>
<CheckBox x:Name=“chkPullService” Content=“Pull Service” VerticalContentAlignment=“Center” />
</Label>

lblPullService is written to the console but chkPullService is not.

I need to be able to do some checks on the control e.g. is it a checkbox control but I do not seem to be getting to it.

Any assistance on what I am doing wrong is appreciated.

This is more of a .NET question than Powershell, so you may want to start with searching along the lines of ‘.Net Controls Enumerate’ or something like that. A portion of your post appears to be missing, but the question is what are passing to the function as Control? The below is using an entire page or form and then emumerating the controls under the ‘global’ controls:

https://forums.asp.net/t/1282048.aspx?How+to+programmatically+loop+through+all+controls+on+a+page+

One thing that does not appear to be correct is:

$Controls = $Control.Children

This is taking the control you are passing and getting the Children, not enumerating the controls directly under the object you are passing. The Recursion should be doing this, you shouldn’t need to reference .Children. Something like this:

Function Get-Control ($Control) {
    foreach ($ctl in $Control.Controls) {
        Write-Host $ctl.Name
        if ($ctl.HasChildren){
            Get-Control -Control $ctl
        }
    }
}