0
votes

Afternoon all,

Having read a few guides online about PowerShell and XAML, I'm trying my hands at using Visual Studio Community to create a GUI for me, then Visual Studio Code to add my PS code.

For reasons beyond me I cannot get VSC to launch the form, yet the same code works perfectly fine in ISE.

Am I missing something? The below isn't the first tool I've began working on - only to have to fallback on ISE to complete.

Thanks in advance - and apologies if it looks naff!

[void][System.Reflection.Assembly]::LoadWithPartialName( 'presentationframework' )
[xml]$XAML = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"


Title="Service Desk Toolbox" Height="650" Width="900" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" WindowStyle="ToolWindow">
<Grid Background="#FF373737">
    <TabControl HorizontalAlignment="Left" Height="600" Margin="10,10,10,0" VerticalAlignment="Top" Width="860" Background="#FF21793B" BorderBrush="#FF21793C">
        <TabItem Header="New User">
            <TabItem.Background>
                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#FFF0F0F0" Offset="0"/>
                    <GradientStop Color="#FF21793B" Offset="1"/>
                </LinearGradientBrush>
            </TabItem.Background>
            <Grid Background="#FF21793B">
                <Label Content="Template account search" HorizontalAlignment="Left" Height="25" Margin="26,25,0,0" VerticalAlignment="Top" 
                    Width="230" FontFamily="Open Sans" FontSize="11" FontWeight="Bold"
                 />
                <TextBox Name="TemplateSearch" HorizontalAlignment="Left" Height="20" Margin="26,55,0,0" TextWrapping="Wrap" Text="Search for user" 
                    VerticalAlignment="Top" Width="200" FontSize="10" FontFamily="Open Sans"
                 />
                <Button Name="Find" Content="Find" HorizontalAlignment="Left" Height="20" Margin="246,55,0,0" VerticalAlignment="Top" 
                    Width="30" FontSize="10" FontFamily="Open Sans"
                 />
                <TextBox Name="TemplateFN" HorizontalAlignment="Left" Height="20" Margin="26,90,0,0" TextWrapping="Wrap" Text="" 
                    VerticalAlignment="Top" Width="200" FontFamily="Open Sans" FontSize="10" IsReadOnly="True"
                 />
                <TextBox Name="TemplateUPN" HorizontalAlignment="Left" Height="20" Margin="246,90,0,0" TextWrapping="Wrap" Text="" 
                    VerticalAlignment="Top" Width="30" FontFamily="Open Sans" FontSize="10" IsReadOnly="True"
                 />
                <TextBox Name="TemplateOU"  HorizontalAlignment="Left" Height="20" Margin="26,125,0,0" TextWrapping="Wrap" Text="" 
                    VerticalAlignment="Top" Width="250" FontFamily="Open Sans" FontSize="10" IsReadOnly="True"
                 />
                <Label Content="Full name" HorizontalAlignment="Left" Height="25" Margin="26,155,0,0" VerticalAlignment="Top" 
                    Width="230" FontFamily="Open Sans" FontSize="11" FontWeight="Bold" 
                />
                <TextBox Name="NewFullName" HorizontalAlignment="Left" Height="20" Margin="26,180,0,0" TextWrapping="Wrap" Text="" 
                    VerticalAlignment="Top" Width="250" FontFamily="Open Sans" FontSize="10"
                 />
                <Label Content="Username" HorizontalAlignment="Left" Height="25" Margin="26,215,0,0" 
                    VerticalAlignment="Top" Width="230" FontFamily="Open Sans" FontSize="11" FontWeight="Bold"
                 />
                <TextBox Name="NewUsername" HorizontalAlignment="Left" Height="20" Margin="26,240,0,0" TextWrapping="Wrap" Text="" 
                    VerticalAlignment="Top" Width="250" FontFamily="Open Sans" FontSize="10"
                 />
                <Label Content="Email Address" HorizontalAlignment="Left" Height="25" Margin="26,275,0,0" 
                    VerticalAlignment="Top" Width="230" FontFamily="Open Sans" FontSize="11" FontWeight="Bold"
                 />
                <TextBox Name="NewEmail" HorizontalAlignment="Left" Height="20" Margin="26,300,0,0" TextWrapping="Wrap" Text="" 
                    VerticalAlignment="Top" Width="250" FontFamily="Open Sans" FontSize="10"
                 />
                <Label Content="Line Manager" HorizontalAlignment="Left" Height="25" Margin="26,335,0,0" 
                       VerticalAlignment="Top" Width="230" FontFamily="Open Sans" FontSize="11" FontWeight="Bold"
                 />
                <TextBox Name="NewManager" HorizontalAlignment="Left" Height="20" Margin="26,360,0,0" TextWrapping="Wrap" Text="" 
                         VerticalAlignment="Top" Width="250" FontFamily="Open Sans" FontSize="10" 
                 />
                <Label Content="Department" HorizontalAlignment="Left" Height="25" Margin="26,395,0,0" 
                       VerticalAlignment="Top" Width="230" FontFamily="Open Sans" FontSize="11" FontWeight="Bold"
                 />
                <TextBox Name="NewDepartment" HorizontalAlignment="Left" Height="20" Margin="26,420,0,0" TextWrapping="Wrap" Text="" 
                         VerticalAlignment="Top" Width="250" FontFamily="Open Sans" FontSize="10" 
                 />
                <Label Content="Job Title" HorizontalAlignment="Left" Height="25" Margin="26,455,0,0" 
                    VerticalAlignment="Top" Width="230" FontFamily="Open Sans" FontSize="11" FontWeight="Bold"
                 />
                <TextBox Name="NewJobTitle" HorizontalAlignment="Left" Height="20" Margin="26,480,0,0" TextWrapping="Wrap" Text="" 
                    VerticalAlignment="Top" Width="250" FontFamily="Open Sans" FontSize="10" 
                 />
            </Grid>
        </TabItem>
        <TabItem Header="TabItem">
            <TabItem.Background>
                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#FFF0F0F0" Offset="0"/>
                    <GradientStop Color="#FF21793B" Offset="1"/>
                </LinearGradientBrush>
            </TabItem.Background>
            <Grid Background="#FF21793B"/>
        </TabItem>
    </TabControl>

</Grid>
</Window>

'@
$READER = ( New-Object System.Xml.XmlNodeReader $XAML ) 
try{
$FORM   = [Windows.Markup.XamlReader]::Load( $READER )
}
catch{Write-Host "Unable to load Windows.Markup.XamlReader." }
$XAML.SelectNodes( "//*[@Name]" ) | %{ Set-Variable -Name ( $_.Name ) -Value     $FORM.FindName( $_.Name ) }
$FORM.ShowDialog() | Out-Null
1
Dropped your code into the ISE and got several errors about variable values: Set-Variable : Missing an argument for parameter 'Value'. Specify a parameter of type 'System.Object' and try again. Also, in VS Code, guessing you need to attach a debugger - trebleCode
@trebleCode - Sorry, the second to last line broke too early. Have edited and checked. Working for me - Graham Jordan

1 Answers

1
votes

You can just type ".\yourscript" in the terminal (it will execute it with powershell.exe). It will not work if you hit F5, I'm afraid VSCode can only debug console apps