Advanced Windows PowerShell Scripting Video Training

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

Tuesday, July 21, 2015

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

 

No comments: