Skip to main content

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.








Comments

Popular posts from this blog

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the mapped drive. If the