Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Much help from Foxdeploy.com to get this working.
- # It now works on older systems like vista
- # https://foxdeploy.com/2015/04/16/part-ii-deploying-powershell-guis-in-minutes-using-visual-studio/
- # https://foxdeploy.com/2015/04/10/part-i-creating-powershell-guis-in-minutes-using-visual-studio-a-new-hope/
- #
- # In visual studio
- # You’ll want to click New Project on the left side of your screen
- # Next, follow the numbers in the image below as you will 1. type WPF in the search box
- # 2. click WPF Application in the main window, and then 3. customize the project name (if you’d like) and click OK.
- #
- # Paste your XML in between the @" "@
- $inputXML = @"
- <Window
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- MinHeight="350"
- Width="525"
- SizeToContent="Height"
- Title="Time Duration"
- Topmost="True">
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="181*"/>
- <ColumnDefinition Width="157*"/>
- <ColumnDefinition Width="179*"/>
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <RowDefinition Height="20*"/>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="20*"/>
- </Grid.RowDefinitions>
- <TextBlock
- Name="TextMessage"
- Grid.Column="1"
- HorizontalAlignment="Center"
- VerticalAlignment="Center"
- FontFamily="Consolas"
- FontSize="20"
- FontWeight="Bold"
- Foreground="Blue" Margin="0,1,3,220" Width="154" Text="Time Duration" Height="24"/>
- <Button
- Name="OK"
- Width="67"
- Height="25"
- HorizontalAlignment="Right"
- Margin="0,0,90,10"
- VerticalAlignment="Bottom" Grid.Column="2" Grid.Row="2">OK
- </Button>
- <TextBlock Name="textBlock" HorizontalAlignment="Left" Margin="63,2,0,0" TextWrapping="Wrap" Text="Start Date " VerticalAlignment="Top" Width="58" Height="16"/>
- <TextBlock Name="textBlock_Copy" HorizontalAlignment="Left" Margin="55,2,0,0" TextWrapping="Wrap" Text="End Date " VerticalAlignment="Top" Width="53" Grid.Column="2" Height="16"/>
- <TextBlock x:Name="textBlock1" Grid.Column="1" HorizontalAlignment="Left" Margin="9,25,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="154" Height="38" FontSize="11" Grid.ColumnSpan="2"><Run Text=" Enter start and end Date"/><LineBreak/><Run Text=" Ex: 07/20/2016 08:00am/pm"/></TextBlock>
- <TextBox Name="Hours_textBox" HorizontalAlignment="Left" Height="23" Margin="12,213,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="56" Grid.Column="1"/>
- <TextBox Name="Minutes_textBox" HorizontalAlignment="Left" Height="23" Margin="86,213,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="57" Grid.Column="1"/>
- <TextBlock x:Name="textBlock2_Copy" HorizontalAlignment="Left" Margin="11,184,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="70" Grid.Column="1" Height="24" Text="Total Hours"/>
- <TextBlock Name="textBlock2_Copy1" HorizontalAlignment="Left" Margin="82,184,0,0" TextWrapping="Wrap" Text=" Minutes" VerticalAlignment="Top" Width="65" Grid.Column="1" Height="24"/>
- <Button
- x:Name="Clear"
- Width="67"
- Height="25"
- Grid.Column="2"
- HorizontalAlignment="Right"
- Margin="0,0,10,10"
- VerticalAlignment="Bottom" Content="Clear" Grid.Row="2"/>
- <TextBlock x:Name="textBlock_Copy1" HorizontalAlignment="Left" Margin="63,80,0,0" TextWrapping="Wrap" Text="Start Time" VerticalAlignment="Top" Width="58" Height="16"/>
- <TextBlock x:Name="textBlock_Copy2" HorizontalAlignment="Left" Margin="55,80,0,0" TextWrapping="Wrap" Text=" End Time" VerticalAlignment="Top" Width="58" Height="16" Grid.Column="2"/>
- <TextBox x:Name="StartTime_Time" HorizontalAlignment="Left" Height="23" Margin="35,96,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
- <TextBox x:Name="EndTime_Time" HorizontalAlignment="Left" Height="23" Margin="31,96,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Grid.Column="2"/>
- <DatePicker x:Name="StartDatePicker" HorizontalAlignment="Left" Margin="43,29,0,0" VerticalAlignment="Top" SelectedDateFormat="Short" IsTodayHighlighted="False"/>
- <DatePicker x:Name="EndDatePicker" Grid.Column="2" HorizontalAlignment="Left" Margin="40,29,0,0" VerticalAlignment="Top"/>
- <Image x:Name="image" Grid.Column="1" HorizontalAlignment="Left" Height="100" Margin="27,68,0,0" VerticalAlignment="Top" Width="100" Source="C:\Users\jtira\Documents\back-in-time_318-10761.ico" RenderTransformOrigin="0.085,0.462"/>
- </Grid>
- </Window>
- "@
- $inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
- [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
- [xml]$XAML = $inputXML
- #Read XAML
- $reader=(New-Object System.Xml.XmlNodeReader $xaml)
- try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
- catch{Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."}
- #===========================================================================
- # Load XAML Objects In PowerShell
- #===========================================================================
- $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}
- Function Get-FormVariables{
- if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
- write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
- get-variable WPF*
- }
- Get-FormVariables
- #===========================================================================
- # Actually make the objects work
- #===========================================================================
- #Sample entry of how to add data to a field
- #$vmpicklistView.items.Add([pscustomobject]@{'VMName'=($_).Name;Status=$_.Status;Other="Yes"})
- #===========================================================================
- # Shows the form
- #===========================================================================
- write-host "To show the form, run the following" -ForegroundColor Cyan
- '$Form.ShowDialog() | out-null'
- $wpfok.add_click({
- $SDpicker = $WPFStartDatePicker.SelectedDate.Date.ToShortDateString()
- $EDpicker = $WPFEndDatePicker.SelectedDate.Date.ToShortDateString()
- [string]$stt = $WPFStartTime_Time.Text
- [string]$ett = $WPFEndTime_Time.Text
- $StartDate = "$SDpicker $stt"
- $EndDate = "$EDpicker $ett"
- $Timespan = NEW-TIMESPAN –Start $StartDate –End $EndDate | Select-Object days, TotalHours, hours, minutes, seconds
- #$WPFDays_textBox.Text = $Timespan.days
- $WPFHours_textBox.Text = $Timespan.TotalHours
- $WPFHours_textBox.Text = $WPFHours_textBox.Text.Split('.')[0]
- $WPFMinutes_textBox.Text = $Timespan.minutes
- #$WPFSeconds_textBox.Text = $Timespan.seconds
- [System.Object]$sender = $args[0]
- [System.Windows.RoutedEventArgs]$e = $args[1]
- })
- $WPFClear.add_click({
- $WPFStart_Time.Text = $null
- $WPFEnd_Time.Text = $null
- #$WPFDays_textBox.Text = $null
- $WPFHours_textBox.Text = $null
- $WPFMinutes_textBox.Text = $null
- #$WPFSeconds_textBox.Text = $null
- [System.Object]$sender = $args[0]
- [System.Windows.RoutedEventArgs]$e = $args[1]
- })
- $Form.ShowDialog() | out-null
Advertisement
Add Comment
Please, Sign In to add comment