none
PowerShell XAML - Change button text depending on a variable RRS feed

  • Question

  • Hey guys,

    I'm trying to build a GUI using PowerShell and XAML (I'm pretty new to this) and I wondered 'if and how' you can change the text on a button, depending on a variable.

    What I'm trying to do is build a little GUI with a Search text field, Search button and an Enable/Disable button ;
    where the text on the button changes, depending on the status of the account and if pressed it enables or disabled the account.

    function SearchUser
    {
        $global:user = Get-ADUser $SearchText.Text
    }
    
    function Enable-Disable
    {
        if (($user.Enabled).ToString() -eq "True") 
            {
                # {Change Content EnableDisableButton to 'Disable'}
                # Disable-ADAccount $user
            }
    
        if (($user.Enabled).ToString() -eq "False") 
            {
                # {Change Content of EnableDisableButton to 'Enable'}
                # Enable-ADAccount $user
            }      
    }
    
    
    
    Add-Type -AssemblyName PresentationFramework
    
    [xml]$xaml = @"
    <Window Title="Test" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Window">
        <Grid>
            <Label HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchLabel" Content="Search (sAMAccountName):" Width="300" Height="25" Margin="10,10,0,0" />
            <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchTextBox" Width="300" Height="30" Margin="10,35,0,0" />
            <Button HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchButton" Content="SEARCH" Width="300" Height="50" Margin="10,75,0,0" />
    
            <Label HorizontalAlignment="Left" VerticalAlignment="Top" Name="EnableDisableLabel" Content="Enable or Disable a User:" Width="300" Height="25" Margin="10,175,0,0" />
            <Button HorizontalAlignment="Left" VerticalAlignment="Top" Name="EnableDisableButton" Content="ENABLE OR DISABLE" Width="300" Height="50" Margin="10,200,0,0" />
        </Grid>
    </Window>
    "@
    
    $reader = (New-Object System.Xml.XmlNodeReader $xaml)
    $window = [Windows.Markup.XamlReader]::Load($reader)
    
    $global:SearchText = $window.FindName("SearchTextBox")
    
    $Search = $window.FindName("SearchButton")
    $Search.Add_Click({SearchUser})
    
    $EnableDisable = $window.FindName("EnableDisableButton")
    $EnableDisable.Add_Click({EnableDisable})
    
    $window.ShowDialog()


    Wednesday, October 23, 2019 4:57 PM

Answers

  • I think you need to learn a bit more PowerShell before trying to use WPF. WPF is a challenge even for experienced programmers and there is very little good documentation for using WPF with PowerShell. C# examples will not help you if you do not know both WPF and PowerShell.

    Add-Type -AssemblyName PresentationFramework
    
    [xml]$xaml = @'
    <Window Title="Test" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Window">
        <Grid>
            <Label HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchLabel" Content="Search (sAMAccountName):" Width="300" Height="25" Margin="10,10,0,0" />
            <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchTextBox" Width="300" Height="30" Margin="10,35,0,0" />
            <Button HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchButton" Content="SEARCH" Width="300" Height="50" Margin="10,75,0,0" />
    
            <Label HorizontalAlignment="Left" VerticalAlignment="Top" Name="EnableDisableLabel" Content="Enable or Disable a User:" Width="300" Height="25" Margin="10,175,0,0" />
            <Button HorizontalAlignment="Left" VerticalAlignment="Top" Name="EnableDisableButton" Content="ENABLE OR DISABLE" Width="300" Height="50" Margin="10,200,0,0" />
        </Grid>
    </Window>
    '@
    
    $reader = New-Object System.Xml.XmlNodeReader($xaml)
    $window = [Windows.Markup.XamlReader]::Load($reader)
    
    $searchUser = $window.FindName('SearchButton')
    $searchUser.add_Click({
    	$script:user = Get-AdUser $this.FindName('SearchTextBox').Text
    	if ($user.Enabled){
                $this.FindName('EnableDisableButton').Content = 'Disable'
            }else{
                $this.FindName('EnableDisableButton').Content = 'Enable'
            }      
    })
    
    $enableDisable = $window.FindName('EnableDisableButton')
    $enableDisable.Add_Click({
    	if ($user.Enabled){
                Disable-ADAccount $user
            }else{
                Enable-ADAccount $user
            }      
    })
    
    $window.ShowDialog()


    \_(ツ)_/




    • Marked as answer by RedDevil BE Thursday, October 24, 2019 4:42 AM
    • Edited by jrv Thursday, October 24, 2019 5:06 AM
    Wednesday, October 23, 2019 7:50 PM

All replies

  • I think you need to learn a bit more PowerShell before trying to use WPF. WPF is a challenge even for experienced programmers and there is very little good documentation for using WPF with PowerShell. C# examples will not help you if you do not know both WPF and PowerShell.

    Add-Type -AssemblyName PresentationFramework
    
    [xml]$xaml = @'
    <Window Title="Test" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Window">
        <Grid>
            <Label HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchLabel" Content="Search (sAMAccountName):" Width="300" Height="25" Margin="10,10,0,0" />
            <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchTextBox" Width="300" Height="30" Margin="10,35,0,0" />
            <Button HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchButton" Content="SEARCH" Width="300" Height="50" Margin="10,75,0,0" />
    
            <Label HorizontalAlignment="Left" VerticalAlignment="Top" Name="EnableDisableLabel" Content="Enable or Disable a User:" Width="300" Height="25" Margin="10,175,0,0" />
            <Button HorizontalAlignment="Left" VerticalAlignment="Top" Name="EnableDisableButton" Content="ENABLE OR DISABLE" Width="300" Height="50" Margin="10,200,0,0" />
        </Grid>
    </Window>
    '@
    
    $reader = New-Object System.Xml.XmlNodeReader($xaml)
    $window = [Windows.Markup.XamlReader]::Load($reader)
    
    $searchUser = $window.FindName('SearchButton')
    $searchUser.add_Click({
    	$script:user = Get-AdUser $this.FindName('SearchTextBox').Text
    	if ($user.Enabled){
                $this.FindName('EnableDisableButton').Content = 'Disable'
            }else{
                $this.FindName('EnableDisableButton').Content = 'Enable'
            }      
    })
    
    $enableDisable = $window.FindName('EnableDisableButton')
    $enableDisable.Add_Click({
    	if ($user.Enabled){
                Disable-ADAccount $user
            }else{
                Enable-ADAccount $user
            }      
    })
    
    $window.ShowDialog()


    \_(ツ)_/




    • Marked as answer by RedDevil BE Thursday, October 24, 2019 4:42 AM
    • Edited by jrv Thursday, October 24, 2019 5:06 AM
    Wednesday, October 23, 2019 7:50 PM
  • Thank you @jrv !

    $this.FindName('').Content=''   exactly gave the result I wanted!

    I was thinking in a totally wrong direction (eg: "How do I fit this in the xaml code?")

    You are very correct stating WPF is no joke and documentation is quite poor.
    But you got to start somewhere right =)





    • Edited by RedDevil BE Thursday, October 24, 2019 4:54 AM
    Thursday, October 24, 2019 4:50 AM
  • Look at the code I posted. That is how to use it. It does exactly what you were trying to do.


    \_(ツ)_/

    Thursday, October 24, 2019 4:53 AM
  • Here is a slightly better example of WPF to toggle a user with error checking and anticipation.

    Add-Type -AssemblyName PresentationFramework
    
    [xml]$xaml = @'
    <Window 
    	Title="Test"
    	
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    	x:Name="Window">
        <Grid>
            <Label HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchLabel" Content="Search (sAMAccountName):" Width="300" Height="25" Margin="10,10,0,0" />
            <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchTextBox" Width="300" Height="30" Margin="10,35,0,0" />
            <Button HorizontalAlignment="Left" VerticalAlignment="Top" Name="SearchButton" Content="SEARCH" Width="300" Height="50" Margin="10,75,0,0" />
            <Label HorizontalAlignment="Left" VerticalAlignment="Top" Name="EnableDisableLabel" Content="Enable or Disable a User:" Width="300" Height="25" Margin="10,175,0,0" />
            <Button HorizontalAlignment="Left" VerticalAlignment="Top" Name="EnableDisableButton" IsEnabled="False" Content="ENABLE OR DISABLE" Width="300" Height="50" Margin="10,200,0,0" />
        </Grid>
    </Window>
    '@
    
    $reader = New-Object System.Xml.XmlNodeReader($xaml)
    $window = [Windows.Markup.XamlReader]::Load($reader)
    $window.WindowStartupLocation = [System.Windows.WindowStartupLocation]::CenterScreen
    
    $searchUser = $window.FindName('SearchButton')
    $searchUser.add_Click({
    	if($script:user = Get-AdUser -Filter "Name -eq '$($this.FindName('SearchTextBox').Text)'"){
    		if ($user.Enabled){
    			$this.FindName('EnableDisableButton').Content = 'Disable'
    		}else{
    			$this.FindName('EnableDisableButton').Content = 'Enable'
    		}
    		$this.FindName('EnableDisableButton').IsEnabled = $true
        }else{
    		[System.Windows.MessageBox]::Show('User not found!')
    		$this.FindName('EnableDisableButton').IsEnabled = $false
        }
    })
    
    $enableDisable = $window.FindName('EnableDisableButton')
    $enableDisable.Add_Click({
    	Try{
    		if ($user.Enabled){
    			Disable-ADAccount $user
    			$this.FindName('EnableDisableButton').Content = 'Enable'
    		}else{
    			Enable-ADAccount $user
    			$this.FindName('EnableDisableButton').Content = 'Disable'
    		}
        }
        Catch{
    		[System.Windows.MessageBox]::Show($_)
        }
        $script:user = Get-AdUser $user		
    })
    
    $window.ShowDialog()


    \_(ツ)_/



    • Edited by jrv Thursday, October 24, 2019 5:30 AM
    Thursday, October 24, 2019 5:25 AM