Variable Issues In Xaml Project

I’m very new to PowerShell and coding in general, but have a bad habit of diving into things and trying to use documentation to swim. I’ve been utilizing PowerShell to administrate Exchange for some of my clients and thought why not create a gui for my company to utilize to make the process super easy and all in one prompt. Needless to say I’ve run into several issues. I’m using PS1 code in combination with .xaml files to create a “Exchange Archive Tool”.

My issue is setting variables to return status, I’m sure there are better ways but below is what i have.

‘’’
[Void]
[System.Reflection.Assembly]::LoadWithPartialName(‘presentationframework’)

import xaml function used for all wpf forms

try {
[xml]$Global:Xaml = Get-Content -Path “$PSScriptRoot\archiveTool1.xaml”
$Reader = New-Object System.Xml.XmlNodeReader $Xaml
$Window = [Windows.Markup.XamlReader]::Load($Reader)
}
catch{
Write-Error “Error building Xaml data.n$_" exit } $Xaml.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name ($_.Name) -Value $Window.FindName($_.Name) -Scope Script } $adminName = $TextInput.Text $EnterButton.Add_Click({ Import-Module ExchangeOnlineManagement Connect-ExchangeOnline -UserPrincipalName $TextInput.Text try { [xml]$Global:Xaml2 = Get-Content -Path "$PSScriptRoot\archiveTool2.xaml" $Reader2 = New-Object System.Xml.XmlNodeReader $Xaml2 $Global:Window2 = [Windows.Markup.XamlReader]::Load($Reader2) } catch{ Write-Error "Error building Xaml data.n$"
exit
}
$Xaml2.SelectNodes(“//*[@Name]”) | ForEach-Object { Set-Variable -Name "WPF$($
.Name)” -Value $Window2.FindName($.Name)}
$Window.Close()
$WPFarchiveDisabled.Visibility = “Hidden”
$WPFarchiveEnabled.Visibility = “Hidden”
$WPFdisableArch.Visibility = “Hidden”
$WPFenableArch.Visibility = “Hidden”
$WPFarchiveToggle.Visibility = “Hidden”
$WPFprocessButton.Add_Click({
Write-Error “button Pressed”
Get-Mailbox -identity $WPFuserEmail.Text | Select @{Label=“ArchiveStatus”;Expression={ if ($
.ArchiveStatus -eq “Active”){$Global:archiveStatus= “Enabled” } else {$Global:archiveStatus= “Disabled” }}}
if ($archiveStatus -eq “Disabled”) {
$WPFarchiveDisabled.Visibility = “Visible”
$WPFarchiveEnabled.Visibility = “Hidden”
$WPFenableArch.Visibility = “Visible”
$WPFdisableArch.Visibility = “Hidden”
}else{
$WPFarchiveEnabled.Visibility = “Visible”
$WPFarchiveDisabled.Visibility = “Hidden”
$WPFdisableArch.Visibility = “Visible”
$WPFenableArch.Visibility = “Hidden”
}
})
$Window2.ShowDialog() | Out-Null
})
$CancelButton.Add_Click({$Window.Close()})
$Window.ShowDialog() | out-null
‘’’

archiveTool1.Xaml

‘’’
<Window xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:Controls0="http://sharpdevelop.net"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:sd="http://sharpdevelop.net"

    xmlns:Controls1="clr-namespace:System;assembly=mscorlib"

    xmlns:s="clr-namespace:System;assembly=mscorlib"

    Width="649"

    Height="161"

    Background="White"

    Title="Exchange Archive Tool">
<TextBox Name="DialogBox"

         Height="20"

         Margin="8,8,496,0"

         HorizontalAlignment="Stretch"

         VerticalAlignment="Top"

         FontSize="15"

         Text="Enter Username "

         Grid.Column="0"

         Grid.Row="0" />

<Button Name="EnterButton"

        Content="Enter"

        Width="75"

        Height="23"

        Margin="157,0,0,20"

        HorizontalAlignment="Left"

        VerticalAlignment="Bottom"

        Grid.Column="0"

        Grid.Row="0" />

<Button Name="CancelButton"

        Content="Cancel"

        Width="75"

        Height="23"

        Margin="0,0,218.5,20"

        HorizontalAlignment="Right"

        VerticalAlignment="Bottom"

        Grid.Column="0"

        Grid.Row="0" />

<TextBox Name="TextInput"

         Width="611"

         Height="20"

         Margin="11,46,0,0"

         HorizontalAlignment="Left"

         VerticalAlignment="Top"

         Grid.Column="0"

         Grid.Row="0" />
''' archiveTool2.xaml

‘’’
<Window xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    Width="638"

    Height="470"

    Background="White"

    Title="Mikel's Exchange Archive tool">
<TextBlock Name="Dialog"

           Margin="8,8,0,0"

           HorizontalAlignment="Left"

           VerticalAlignment="Top"

           FontWeight="Bold"

           Text="Enter Client Email"

           Grid.Column="0"

           Grid.Row="0" />

<TextBox Name="clientEmail"

         Width="614"

         Height="20"

         Margin="8,32,0,0"

         HorizontalAlignment="Left"

         VerticalAlignment="Top"

         Grid.Column="0"

         Grid.Row="0" />

<Button Name="processButton"

        Content="Process"

        Width="75"

        Height="23"

        Margin="277.5,66,0,0"

        HorizontalAlignment="Left"

        VerticalAlignment="Top"

        Visibility="Visible"

        Grid.Column="0"

        Grid.Row="0" />

<RadioButton Name="archiveToggle"

             Width="19"

             Height="18"

             Margin="477.75,128,0,0"

             HorizontalAlignment="Left"

             VerticalAlignment="Top"

             RenderTransformOrigin="0.5263,0.5278"

                 Visibility="Hidden"

             Grid.Column="0"

             Grid.Row="0" />

<TextBlock Name="archiveEnabled"

           Width="270"

           Height="22"

           Margin="8,107,0,0"

           HorizontalAlignment="Left"

           VerticalAlignment="Top"

           FontWeight="Bold"

           Text="Archive Enabled"

           TextAlignment="Center"

               Visibility="Hidden"

           Grid.Column="0"

           Grid.Row="0" />

<TextBlock Name="archiveDisabled"

           Width="270"

           Height="22"

           Margin="8,107,0,0"

           HorizontalAlignment="Left"

           VerticalAlignment="Top"

           FontWeight="Bold"

           Text="Archive Disabled"

           TextAlignment="Center"

           Visibility="Hidden"

           Grid.Column="0"

           Grid.Row="0" />

<TextBlock Name="disableArch"

           Width="269.5"

           Height="22"

           Margin="0,107,8,0"

           HorizontalAlignment="Right"

           VerticalAlignment="Top"

           FontWeight="Bold"

           Text="Disable Archiving"

           TextAlignment="Center"

               Visibility="Hidden"

           Grid.Column="0"

           Grid.Row="0" />

<TextBlock Name="enableArch"

           Width="269.5"

           Height="22"

           Margin="0,107,8,0"

           HorizontalAlignment="Right"

           VerticalAlignment="Top"

           FontWeight="Bold"

           Text="Enable Archiving"

           TextAlignment="Center"

               Visibility="Hidden"

           Grid.Column="0"

           Grid.Row="0" />
'''

For some reason the $Global:archiveStatus variable isn’t working properly when called by the if statement. All of the users I enter and process are returning “disabled” I’ve managed to go line by line using the same code in PowerShell manually and it works properly. For some reason its failing in this format. Any insight or recommendations would be greatly appreciated.

Hi,

Not sure if it’s just the awful formatting, but in your if statement you are missing the underscore after the $ :

if ($.ArchiveStatus -eq “Active”)

Please edit your post and reformat the code if you require further advice:

How to format code on PowerShell.org

1 Like