Skip to main content

Update to my Navy Date Time Group Generator using SAPIEN PowerShell Studio

Good morning from Japan.  I’ve been in Japan for the last week and a half doing my duty as a Navy Reservist.  I am very much looking forward to heading home very soon.  Today I found a chance to introduce another fellow IT here in the Navy to PowerShell.  You could see the hamster wheel in his head turning fast. During some training, I discovered that I am not the only one who mixes up the numbers for a Navy Date Time Group (DTG).  Well, I decided to make an update to my original code.

I decided to pull out Sapien PowerShell Studio.  I needed to make this easy to use and since not all potential users are PowerShell experts, I put the code into a GUI.  Looking at the code below, I did nothing to the original code except to add 2 spaces in the output to make the string correct by Navy standards.  Here is what the finished product looks like.

image

The concept is the same.  This is just a live updating version.  If you need the current DTG on the clipboard, just click Send to Clipboard.

Now for the fun part, here is the code.  I am posting the code in its entirety so it can be pasted into the Shell and executed from there if necessary.

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

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

#------------------------------------------------------------------------

# Source File Information (DO NOT MODIFY)

# Source ID: 9c32e0ca-a84b-4920-be22-903aacc132a3

# Source File: E:\PowerShell\InDev\NavyDTG\NavyDTG.psf

#------------------------------------------------------------------------

 

<#

    .NOTES

    --------------------------------------------------------------------------------

     Code generated by:  SAPIEN Technologies, Inc., PowerShell Studio 2015 v4.2.86

     Generated on:       7/21/2015 3:26 AM

     Generated by:       

     Organization:       

    --------------------------------------------------------------------------------

    .DESCRIPTION

        GUI script generated by PowerShell Studio 2015

#>

#----------------------------------------------

#region Application Functions

#----------------------------------------------

 

#endregion Application Functions

 

#----------------------------------------------

# Generated Form Function

#----------------------------------------------

function Call-NavyDTG_psf {

 

       #----------------------------------------------

       #region Import the Assemblies

       #----------------------------------------------

       [void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')

       [void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')

       [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')

       [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')

       [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')

       [void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')

       [void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')

       [void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')

       [void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')

       #endregion Import Assemblies

 

       #----------------------------------------------

       #region Generated Form Objects

       #----------------------------------------------

       [System.Windows.Forms.Application]::EnableVisualStyles()

       $formNavyDTG = New-Object 'System.Windows.Forms.Form'

       $buttonExit = New-Object 'System.Windows.Forms.Button'

       $button_Clip = New-Object 'System.Windows.Forms.Button'

       $label1 = New-Object 'System.Windows.Forms.Label'

       $timer1 = New-Object 'System.Windows.Forms.Timer'

       $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'

       #endregion Generated Form Objects

 

       #----------------------------------------------

       # User Generated Script

       #----------------------------------------------

       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

      

      

       $formNavyDTG_Load={

              # get the intial DTG

              $label1.Text = Get-NAVYDTG -NoClip

             

              # Start the 15 second timer.

              $timer1.Interval = 15000

              $timer1.Start()

       }

      

       $buttonExit_Click={

              # Close the form.

              $FormNavyDTG.Close()

       }

      

       $timer1_Tick={

              # Update the DTG every 15 seconds.

              $label1.Text = Get-NAVYDTG -NoClip

       }

      

       $button_Clip_Click={

              # Send the DTG to the clipboard.

              Get-NAVYDTG

       }

      

       # --End User Generated Script--

       #----------------------------------------------

       #region Generated Events

       #----------------------------------------------

      

       $Form_StateCorrection_Load=

       {

              #Correct the initial state of the form to prevent the .Net maximized form issue

              $formNavyDTG.WindowState = $InitialFormWindowState

       }

      

       $Form_Cleanup_FormClosed=

       {

              #Remove all event handlers from the controls

              try

              {

                     $buttonExit.remove_Click($buttonExit_Click)

                     $button_Clip.remove_Click($button_Clip_Click)

                     $formNavyDTG.remove_Load($formNavyDTG_Load)

                     $timer1.remove_Tick($timer1_Tick)

                     $formNavyDTG.remove_Load($Form_StateCorrection_Load)

                     $formNavyDTG.remove_FormClosed($Form_Cleanup_FormClosed)

              }

              catch [Exception]

              { }

       }

       #endregion Generated Events

 

       #----------------------------------------------

       #region Generated Form Code

       #----------------------------------------------

       $formNavyDTG.SuspendLayout()

       #

       # formNavyDTG

       #

       $formNavyDTG.Controls.Add($buttonExit)

       $formNavyDTG.Controls.Add($button_Clip)

       $formNavyDTG.Controls.Add($label1)

       $formNavyDTG.ClientSize = '500, 96'

       $formNavyDTG.Name = "formNavyDTG"

       $formNavyDTG.Text = "Navy DTG"

       $formNavyDTG.add_Load($formNavyDTG_Load)

       #

       # buttonExit

       #

       $buttonExit.Location = '249, 58'

       $buttonExit.Name = "buttonExit"

       $buttonExit.Size = '244, 34'

       $buttonExit.TabIndex = 2

       $buttonExit.Text = "Exit"

       $buttonExit.UseVisualStyleBackColor = $True

       $buttonExit.add_Click($buttonExit_Click)

       #

       # button_Clip

       #

       $button_Clip.Location = '5, 58'

       $button_Clip.Name = "button_Clip"

       $button_Clip.Size = '244, 34'

       $button_Clip.TabIndex = 1

       $button_Clip.Text = "Send to Clipboard"

       $button_Clip.UseVisualStyleBackColor = $True

       $button_Clip.add_Click($button_Clip_Click)

       #

       # label1

       #

       $label1.BackColor = 'Navy'

       $label1.Font = "Microsoft Sans Serif, 14.25pt"

       $label1.ForeColor = 'Cyan'

       $label1.Location = '6, 5'

       $label1.Name = "label1"

       $label1.Size = '487, 46'

       $label1.TabIndex = 0

       $label1.Text = "label1"

       $label1.TextAlign = 'MiddleCenter'

       #

       # timer1

       #

       $timer1.add_Tick($timer1_Tick)

       $formNavyDTG.ResumeLayout()

       #endregion Generated Form Code

 

       #----------------------------------------------

 

       #Save the initial state of the form

       $InitialFormWindowState = $formNavyDTG.WindowState

       #Init the OnLoad event to correct the initial state of the form

       $formNavyDTG.add_Load($Form_StateCorrection_Load)

       #Clean up the control events

       $formNavyDTG.add_FormClosed($Form_Cleanup_FormClosed)

       #Show the Form

       return $formNavyDTG.ShowDialog()

 

} #End Function

 

#Call the form

Call-NavyDTG_psf | Out-Null

 

Comments

Popular posts from this blog

Sticky Key problem between Windows Server 2012 and LogMeIn

This week I instructed my first class using Windows Server 2012 accessed via LogMeIn and discovered a Sticky Key problem every time you press the Shift key. Here is my solution to resolve this.  First off, in the Preferences of LogMeIn for the connection to the Windows Server, click General . Change the Keyboard and mouse priority to Host side user and click Apply at the bottom. On the Windows 2012 server, open the Control Panel – Ease of Access – Change how your keyboard works . Uncheck Turn on Sticky Keys . Click Set up Sticky Keys . Uncheck Turn on Sticky Keys when SHIFT is pressed five times . Click OK twice. If you are using Windows Server 2012 as a Hyper-V host, you will need to redo the Easy of Use settings on each guest operating system in order to avoid the Sticky Key Problem. Updated Information: March 20, 2013 If you continue to have problems, Uncheck Turn on Filter Keys .

Where did a User’s Account Get Locked Out?

Updated: May 15, 2015 When this article was originally published, two extra carriage returns were add causing the code to malfunction.  The code below is correct.   My client for this week’s PowerShell class had a really interesting question. They needed to know where an account is being locked out at. OK, interesting. Apparently users hop around clients and forget to log off, leading to eventual lock out of their accounts. The accounts can be unlocked, but are then relocked after Active Directory replication. This problem is solved in two parts. The first one is to modify the event auditing on the network. The second part is resolved with PowerShell. The first part involves creating a group policy that will encompass your Domain Controllers. In this GPO, make these changes. Expand Computer Configuration \ Policies \ Windows Settings \ Security Settings \ Advanced Audit Policy Configuration \ Audit Policies \ Account Management Double click User Account Management C...

Backup and Restore AD LDS with DSDBUTIL.exe

Active Directory Lightweight Directory Services allow you to create a directory service that allows applications to have access to user accounts, groups, and authentication similar to Active Directory Domain Services.  The big advantage here is that the schema of the directory service will not be bound by the rules of an Active Directory database.  Exchange 2007/2010, for example, use an instance of AD LDS on the Edge Transport Server to provide for user authentication from the internet.  Because your Active Directory database is not exposed to the internet, this is more secure. Applications will handle most of the dirty work should they require AD LDS.  You may want to make sure the database is being backed up and also have a restore plan in place.  Should the database become corrupt, the application that uses that database will fail.  This document will walk you through backing up and restoring an instance of AD LDS using the dsdbutil.exe command. Fi...