Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Monday, May 20, 2013

Get a US Navy Date Time Group (DTG) with PowerShell

This past week I’ve been at Naval Station Great Lakes training for a mission later this year.  As I’m training up for this mission, I’m seeing some procedures that may benefit from PowerShell.  Many of the procedures that I will be executing involves utilizing a Navy Date Time Group (DTG).  The problem with DTGs is that they are not exactly logical.  Here is the format.
2 digit day
2 digit hour
2 digit minute
Z for ZUL (GMT for the civilians out there)
3 Character month
2 digit year.
So, the procedure that I will be utilizing this code with will require that I either figure this out on my own under a high tempo/stress situation, or I simply ask PowerShell to do this.  Since we do not utilize PowerShell for any of our tasks, I also needed a way to copy and paste this information into an application.  The code below will generate the DTG and also place it on the clipboard.  Relying on a manual copy and paste or simply getting the DTG from PowerShell and manually typing it would have added up to a lot of wasted time and potential errors.  To send any PowerShell output to the clipboard, simply pipe it to Clip. For Example:
Get-Service | Clip
Since the default need for this code is to place it on the clip board and not the in the pipeline, A –PassThru parameter is provided that will suppress all screen output and send a System.DateTime object with the DTG property to the pipeline.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
Function Get-NAVYDTG
{
[CmdletBinding()]
Param([Switch]$NoClip,
      [Switch]$PassThru
)
    $Date = (Get-Date).ToUniversalTime()
 
    # Day Component
    Switch ($Date.day.ToString().length)
    {
        1 {$Day = "0$($Date.Day)"; BREAK}
        Default {$Day = "$($Date.Day)"}
    }

    # Hour Component
    Switch ($Date.Hour.ToString().length)
    {
        1 {$Hour = "0$($Date.Hour)"; BREAK}
        Default {$Hour = "$($Date.Hour)"}
    }

    # Minute Component
    Switch ($Date.Minute.ToString().length)
    {
        1 {$Minute = "0$($Date.Minute)"; BREAK}
        Default {$Minute = "$($Date.Minute)"}
    }

    # Month Component
    $Month = ((((get-date).
          GetDateTimeFormats())[6]).
                   remove("0","3")).
                          Remove(3).
                          ToUpper()

    # Year Component
    $Year = ((get-date).Year.toString().Remove("0","2"))
 
    If ((!$NoClip) -and (!$PassThru)
)
    {
        Write-Output "$($Day)$($Hour)$($Minute)Z$($Month)$($Year)" |
        Clip
        Write-Host "$($Day)$($Hour)$($Minute)Z$($Month)$($Year)"
        Write-Host "Sent to Clipboard"
    }
    ELSEIF ($PassThru)
    {
        $Date |
        Select-Object -Property *,
        @{Name="DTG";Expression={"$($Day)$($Hour)$($Minute)Z$($Month)$($Year)"}} |
        Write-Output
    }
    Else
    {
        Write-Output "$($Day)$($Hour)$($Minute)Z$($Month)$($Year)"
    }

<#
.SYNOPSIS
Returns a Navy Date Time Group (DTG)

.DESCRIPTION
Returns a Navy Date Group Group (DTG) and copies it to the clipboard.

.PARAMETER NoClip
Prevents the DTG value from being sent to the clipboard and only
displayed on the screen.

.PARAMETER PassThru
Suppresses all screen output and passes a full Systsem.DateTime object
to the pipeline with the DTG property added.

.EXAMPLE
Get-NAVYDTG
040219ZAUG13
Sent to Clipboard

Displays the DTG and sends it to the clipboard

.EXAMPLE
Get-NAVYDTG -NoClip
040220ZAUG13

Displays the DTG, but dose not copy it to the clipboard.

.EXAMPLE
Get-NAVYDTG -PassThru
 
DateTime    : Sunday, May 05, 2013 7:42:17 PM
Date        : 5/5/2013 12:00:00 AM
Day         : 5
DayOfWeek   : Sunday
DayOfYear   : 125
Hour        : 19
Kind        : Utc
Millisecond : 446
Minute      : 42
Month       : 5
Second      : 17
Ticks       : 635033797374465212
TimeOfDay   : 19:42:17.4465212
Year        : 2013
DTG         : 051942ZAUG13

Passes a System.DateTime object to the pipeline with the DTG
property added.
#>
} # End Function Get-NAVYDTG

We represent the DTG as ZULU or GMT time.  Line 7 converts the local into ZULU time.
Since a proper DTG must have 2 digit representation of the day,hour, and minute components, I needed a way to add a zero to any one of these components should they return a single digit. Lines 9 through 28 provides this assurance.
The month component must be given with only 3 characters and must be represented in upper case.  Line 31 provides this.
Line 38 provides the last 2 digits of the year.
Lines 40-47 send the properly formatted DTG to the clipboard if the –NoClip and –PassThru parameters are omitted. 
If –PassThru is provided, lines 48-54 send the System.DateTime object with a DTG property added to the pipeline.
If –NoClip is provided, line 57 simply writes the DTG to the pipeline.








No comments: