xs4eyes

Get-TimeDuration

Jul 26th, 2016
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Much help from Foxdeploy.com to get this working.
  2. # It now works on older systems like vista
  3. # https://foxdeploy.com/2015/04/16/part-ii-deploying-powershell-guis-in-minutes-using-visual-studio/
  4. # https://foxdeploy.com/2015/04/10/part-i-creating-powershell-guis-in-minutes-using-visual-studio-a-new-hope/
  5. #
  6. # In visual studio
  7. # You’ll want to click New Project on the left side of your screen
  8. # Next, follow the numbers in the image below as you will 1. type WPF in the search box
  9. # 2. click WPF Application in the main window, and then 3. customize the project name (if you’d like) and click OK.
  10. #
  11. # Paste your XML in between the @" "@
  12.  
  13. $inputXML = @"
  14. <Window
  15.   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  16.   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  17.   MinHeight="350"
  18.   Width="525"
  19.   SizeToContent="Height"
  20.   Title="Time Duration"
  21.   Topmost="True">
  22.    <Grid>
  23.        <Grid.ColumnDefinitions>
  24.            <ColumnDefinition Width="181*"/>
  25.            <ColumnDefinition Width="157*"/>
  26.            <ColumnDefinition Width="179*"/>
  27.        </Grid.ColumnDefinitions>
  28.        <Grid.RowDefinitions>
  29.            <RowDefinition Height="20*"/>
  30.            <RowDefinition Height="Auto"/>
  31.            <RowDefinition Height="20*"/>
  32.        </Grid.RowDefinitions>
  33.        <TextBlock
  34.         Name="TextMessage"
  35.         Grid.Column="1"
  36.         HorizontalAlignment="Center"
  37.         VerticalAlignment="Center"
  38.         FontFamily="Consolas"
  39.         FontSize="20"
  40.         FontWeight="Bold"
  41.         Foreground="Blue" Margin="0,1,3,220" Width="154" Text="Time Duration" Height="24"/>
  42.        <Button
  43.         Name="OK"
  44.         Width="67"
  45.         Height="25"
  46.         HorizontalAlignment="Right"
  47.         Margin="0,0,90,10"
  48.         VerticalAlignment="Bottom" Grid.Column="2" Grid.Row="2">OK
  49.        </Button>
  50.        <TextBlock Name="textBlock" HorizontalAlignment="Left" Margin="63,2,0,0" TextWrapping="Wrap" Text="Start Date " VerticalAlignment="Top" Width="58" Height="16"/>
  51.        <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"/>
  52.        <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>
  53.        <TextBox Name="Hours_textBox" HorizontalAlignment="Left" Height="23" Margin="12,213,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="56" Grid.Column="1"/>
  54.        <TextBox Name="Minutes_textBox" HorizontalAlignment="Left" Height="23" Margin="86,213,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="57" Grid.Column="1"/>
  55.        <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"/>
  56.        <TextBlock Name="textBlock2_Copy1" HorizontalAlignment="Left" Margin="82,184,0,0" TextWrapping="Wrap" Text="   Minutes" VerticalAlignment="Top" Width="65" Grid.Column="1" Height="24"/>
  57.        <Button
  58.             x:Name="Clear"
  59.             Width="67"
  60.             Height="25"
  61.             Grid.Column="2"
  62.             HorizontalAlignment="Right"
  63.             Margin="0,0,10,10"
  64.             VerticalAlignment="Bottom" Content="Clear" Grid.Row="2"/>
  65.        <TextBlock x:Name="textBlock_Copy1" HorizontalAlignment="Left" Margin="63,80,0,0" TextWrapping="Wrap" Text="Start Time" VerticalAlignment="Top" Width="58" Height="16"/>
  66.        <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"/>
  67.        <TextBox x:Name="StartTime_Time" HorizontalAlignment="Left" Height="23" Margin="35,96,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
  68.        <TextBox x:Name="EndTime_Time" HorizontalAlignment="Left" Height="23" Margin="31,96,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Grid.Column="2"/>
  69.        <DatePicker x:Name="StartDatePicker" HorizontalAlignment="Left" Margin="43,29,0,0" VerticalAlignment="Top" SelectedDateFormat="Short" IsTodayHighlighted="False"/>
  70.        <DatePicker x:Name="EndDatePicker" Grid.Column="2" HorizontalAlignment="Left" Margin="40,29,0,0" VerticalAlignment="Top"/>
  71.        <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"/>
  72.    </Grid>
  73. </Window>
  74. "@      
  75.  
  76. $inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N'  -replace '^<Win.*', '<Window'
  77.  
  78. [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
  79. [xml]$XAML = $inputXML
  80. #Read XAML
  81.  
  82.     $reader=(New-Object System.Xml.XmlNodeReader $xaml)
  83.   try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
  84. catch{Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."}
  85.  
  86. #===========================================================================
  87. # Load XAML Objects In PowerShell
  88. #===========================================================================
  89.  
  90. $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}
  91.  
  92. Function Get-FormVariables{
  93. if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
  94. write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
  95. get-variable WPF*
  96. }
  97.  
  98. Get-FormVariables
  99.  
  100. #===========================================================================
  101. # Actually make the objects work
  102. #===========================================================================
  103.  
  104. #Sample entry of how to add data to a field
  105.  
  106. #$vmpicklistView.items.Add([pscustomobject]@{'VMName'=($_).Name;Status=$_.Status;Other="Yes"})
  107.  
  108. #===========================================================================
  109. # Shows the form
  110. #===========================================================================
  111. write-host "To show the form, run the following" -ForegroundColor Cyan
  112. '$Form.ShowDialog() | out-null'
  113.  
  114. $wpfok.add_click({
  115.  
  116.         $SDpicker = $WPFStartDatePicker.SelectedDate.Date.ToShortDateString()
  117.         $EDpicker = $WPFEndDatePicker.SelectedDate.Date.ToShortDateString()
  118.         [string]$stt = $WPFStartTime_Time.Text  
  119.         [string]$ett = $WPFEndTime_Time.Text
  120.         $StartDate = "$SDpicker $stt"
  121.         $EndDate = "$EDpicker $ett"
  122.         $Timespan = NEW-TIMESPAN –Start $StartDate –End $EndDate | Select-Object days, TotalHours, hours, minutes, seconds
  123.         #$WPFDays_textBox.Text = $Timespan.days
  124.         $WPFHours_textBox.Text = $Timespan.TotalHours
  125.         $WPFHours_textBox.Text = $WPFHours_textBox.Text.Split('.')[0]
  126.         $WPFMinutes_textBox.Text = $Timespan.minutes
  127.         #$WPFSeconds_textBox.Text = $Timespan.seconds
  128.     [System.Object]$sender = $args[0]
  129.     [System.Windows.RoutedEventArgs]$e = $args[1]
  130.     })
  131.  
  132. $WPFClear.add_click({
  133.  
  134.  
  135.         $WPFStart_Time.Text = $null
  136.         $WPFEnd_Time.Text = $null
  137.         #$WPFDays_textBox.Text = $null
  138.         $WPFHours_textBox.Text = $null
  139.         $WPFMinutes_textBox.Text = $null
  140.         #$WPFSeconds_textBox.Text = $null
  141.     [System.Object]$sender = $args[0]
  142.     [System.Windows.RoutedEventArgs]$e = $args[1]
  143. })
  144.  
  145.  
  146.  
  147. $Form.ShowDialog() | out-null
Advertisement
Add Comment
Please, Sign In to add comment