Advertisement
1RedOne

FixedVariableNotAssigning

Mar 2nd, 2017
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ERASE ALL THIS AND PUT XAML BELOW between the @" "@
  2. $inputXML = @"
  3. <Window x:Class="UserCreation.MainWindow"
  4.        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5.        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6.        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7.        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8.        xmlns:local="clr-namespace:UserCreation"
  9.        mc:Ignorable="d"
  10.        Title="User Creation Tool" Height="445" Width="320">
  11.    <Grid Margin="0,0,2,0">
  12.        <TextBlock x:Name="textBlockTitle" HorizontalAlignment="Left" Height="20" Margin="10,10,0,0" TextWrapping="Wrap" Text="Use this tool to create new users!" VerticalAlignment="Top" Width="290" FontSize="16" TextAlignment="Center"/>
  13.        <Button x:Name="buttonCreate" Content="Create!" HorizontalAlignment="Left" Height="35" Margin="125,350,0,0" VerticalAlignment="Top" Width="75" FontSize="16"/>
  14.        <TextBox x:Name="textBoxLocationCode" HorizontalAlignment="Left" Height="25" Margin="125,90,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="150"/>
  15.        <TextBox x:Name="textBoxFirstName" HorizontalAlignment="Left" Height="25" Margin="125,135,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="150"/>
  16.        <TextBox x:Name="textBoxLastName" HorizontalAlignment="Left" Height="25" Margin="125,180,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="150"/>
  17.        <TextBox x:Name="textBoxJobTitle" HorizontalAlignment="Left" Height="25" Margin="125,225,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="150"/>
  18.        <TextBlock x:Name="textBlockLocationCode" HorizontalAlignment="Left" Height="25" Margin="25,90,0,0" TextWrapping="Wrap" Text="Location Code:" VerticalAlignment="Top"/>
  19.        <TextBlock x:Name="textBlockFirstName" HorizontalAlignment="Left" Height="25" Margin="25,135,0,0" TextWrapping="Wrap" Text="First Name:" VerticalAlignment="Top"/>
  20.        <TextBlock x:Name="textBlockLastName" HorizontalAlignment="Left" Height="25" Margin="25,180,0,0" TextWrapping="Wrap" Text="Last Name:" VerticalAlignment="Top"/>
  21.        <TextBlock x:Name="textBlockJobTitle" HorizontalAlignment="Left" Height="25" Margin="25,225,0,0" TextWrapping="Wrap" Text="Job Title:" VerticalAlignment="Top"/>
  22.        <Button x:Name="buttonVerify" Content="All Info Correct!" HorizontalAlignment="Left" Margin="112,295,0,0" VerticalAlignment="Top" Width="100" Height="25"/>
  23.  
  24.    </Grid>
  25. </Window>
  26. "@      
  27.  
  28. $inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N'  -replace '^<Win.*', '<Window'
  29.  
  30. [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
  31. [xml]$XAML = $inputXML
  32. #Read XAML
  33.  
  34.     $reader=(New-Object System.Xml.XmlNodeReader $XAML)
  35.   try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
  36. catch{Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."}
  37.  
  38. #===========================================================================
  39. # Load XAML Objects In PowerShell
  40. #===========================================================================
  41.  
  42. $XAML.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}
  43.  
  44. Function Get-FormVariables{
  45. if ($global:ReadmeDisplay -ne $true){Write-Host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
  46. Write-Host "Found the following interactable elements from our form" -ForegroundColor Cyan
  47. Get-Variable WPF*
  48. }
  49.  
  50. Get-FormVariables
  51.  
  52. #===========================================================================
  53. # Actually make the objects work
  54. #===========================================================================
  55.  
  56. $WPFbuttonVerify.Add_Click(
  57.     {
  58.         If ($WPFbuttonCreate.Visibility -ne 'Visible'){$WPFbuttonCreate.Visibility = 'Visible'}
  59.         Else {$WPFbuttonCreate.Visibility = 'Hidden'}
  60.     }
  61. )
  62.  
  63. $WPFbuttonCreate.Visibility = 'Hidden'
  64.  
  65. #Test Variables
  66. #$LocationCode = "100"
  67. #$FirstName = "Test"
  68. #$LastName = "User"
  69. #$JobTitle = "Manager"
  70.  
  71. $WPFbuttonCreate.Add_Click({
  72.         Write-Host $LocationCode
  73.     })
  74.  
  75. #Sample entry of how to add data to a field
  76.  
  77. #$vmpicklistView.items.Add([pscustomobject]@{'VMName'=($_).Name;Status=$_.Status;Other="Yes"})
  78.  
  79. #===========================================================================
  80. # Shows the form
  81. #===========================================================================
  82.  
  83. Function Show-Form{
  84. $Form.ShowDialog() | out-null
  85. }
  86.  
  87. Show-Form
  88.  
  89.  
  90. $LocationCode = $WPFtextBoxLocationCode.Text
  91. $FirstName = $WPFtextBoxFirstName.Text
  92. $LastName = $WPFtextBoxLastName.Text
  93. $JobTitle = $WPFtextBoxJobTitle.Text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement