SoftwareTimestamping.psm1

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
291
292
293
294
295
Function Get-SWTimestamping {
<#
  .SYNOPSIS
  Gets the software timestamping configuration of a specified NICs
   
  .DESCRIPTION
  Software Timestamping is a feature that enables increased time accuracy by calculating the delay introduced by the network stack
 
  Use this command to enable Software Timestamping for a specified NIC
 
  Note: Enabling software timestamping on virtual NICs, WAN Miniport, WI-FI Adapters, is not supported
 
  .EXAMPLE
  Get-SWTimestamping -NetAdapterName NIC1, NIC2
   
  .EXAMPLE
  $tsAdapters = Get-SWTimestamping -NetAdapterName NIC1, NIC2
 
  .PARAMETER NetAdapterName
  The adapter to retrieve the timestamping configuration
 
  Reference the 'Name' Property of Get-NetAdapter for possible adapter names
#>


    param (
        [String[]] $NetAdapterName = (Get-NetAdapter | Where-Object `
                     {$_.InterfaceDescription -notlike '*Microsoft Kernel Debug*' -and
                      $_.InterfaceDescription -notlike '*WAN Miniport*' -and
                      $_.InterfaceDescription -notlike '*WI-FI*'}).Name
    )

    if ($NetAdapterName -eq $null) {
        Write-Warning -Message 'No acceptable adapters were found. Review the help of this cmdlet as some adapters are not eligible for software timestamping'
        break
    }
    
    $NetAdapterDescription = (Get-NetAdapter -Name $NetAdapterName).InterfaceDescription

    $status = Get-NetAdapterAdvancedProperty -InterfaceDescription $NetAdapterDescription `
                                             -RegistryKeyword SoftwareTimestampSettings -AllProperties `
                                             -ErrorAction SilentlyContinue | Select-Object Name, RegistryKeyword, RegistryValue

    If (-not($status)) {
        Write-Error "Software Timestamping is not configured on $NetAdapterName"
    }

    $status
}

Function Enable-SWTimestamping {
<#
  .SYNOPSIS
  Enables software timestamping on specified NICs
   
  .DESCRIPTION
  Software Timestamping is a feature that enables increased time accuracy by calculating the delay introduced by the network stack
 
  Use this command to enable Software Timestamping for a specified NIC
 
  Note: This is a software only implementation and does not include the latency/uncertainty added by the miniport or hardware
  Note: Enabling software timestamping on virtual NICs, WAN Miniport, WI-FI Adapters, is not supported
 
  Known Issue: Devices that have special characters such as parenthesis currently do not work
 
  .EXAMPLE
  Enable-SWTimestamping -NetAdapterName NIC1, NIC2
   
  .EXAMPLE
  Enable-SWTimestamping -NetAdapterName NIC1, NIC2, NIC3, NIC4 -TimestampValue 5
 
  .PARAMETER NetAdapterName
  The adapter you want to enable timestamping on
 
  Reference the 'Name' Property of Get-NetAdapter for possible adapter names
 
  .PARAMETER TimestampValue
  Specifies the specific traffic to be timestamped.
     
  Default is 5
        1 - All RX
        2 - All Tx
        3 - All RX - ALL TX
        4 - Only Selective TX
        5 - ALL RX (Selective TX)
#>


    param (
        [Parameter(Mandatory=$true)]
        [String[]] $NetAdapterName ,

        [ValidateRange(1,5)]
        [int] $TimestampValue = 5
    )

    $InterfaceGUIDs = (Get-NetAdapter -Name $NetAdapterName).InterfaceGuid

    Get-ChildItem 'HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}' -Recurse -ErrorAction SilentlyContinue | ForEach-Object {
        foreach ($interfaceGUID in $interfaceGUIDs) {
            $psPath = $_.PSPath
            $driverDesc = (Get-ItemProperty -Path $PsPath).DriverDesc

            if ( (Get-ItemProperty -Path $PsPath) -match $InterfaceGUID) {
                Switch ($driverDesc) {
                    {
                        $PSItem -like '*Microsoft Kernel Debug*' -or 
                        $PSItem -like '*WAN Miniport*' -or 
                        $PSItem -like '*WI-FI*'
                    } 
                    {
                        Write-Error 'Enabling software timestamps on Kernel Debug adapters, WAN Miniports, or WI-FI adapters is not supported'
                        break
                    }
            
                    default {
                        Set-ItemProperty -Path $psPath -Name SoftwareTimestampSettings -Value $TimestampValue
                    }
                }
            }
        }
    }
}

Function Disable-SWTimestamping {
<#
  .SYNOPSIS
  Disables software timestamping on specified NICs
   
  .DESCRIPTION
  Software Timestamping is a feature that enables increased time accuracy by calculating the delay introduced by the network stack
 
  Use this command to remove the Software Timestamping configuration for a specified NIC
 
  Known Issue: Devices that have special characters such as parenthesis currently do not work
 
  .EXAMPLE
  Disable-SWTimestamping -NetAdapterName NIC1, NIC2
   
  .PARAMETER NetAdapterName
  The adapter you want to enable timestamping on
 
  Reference the 'Name' Property of Get-NetAdapter for possible adapter names
#>


    param (
        [Parameter(Mandatory=$true)]
        [String[]] $NetAdapterName
    )

    $InterfaceGUIDs = (Get-NetAdapter -Name $NetAdapterName).InterfaceGuid

    Get-ChildItem 'HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}' -Recurse -ErrorAction SilentlyContinue | ForEach-Object {
        foreach ($interfaceGUID in $interfaceGUIDs) {
            $psPath = $_.PSPath
            
            if ( (Get-ItemProperty -Path $PsPath) -match $InterfaceGUID) {
                Remove-ItemProperty -Path $psPath -Name SoftwareTimestampSettings
            }
        }
    }
}

enum Ensure {
   Absent
   Present
}

[DscResource()]
class SoftwareTimestamping {

    [DscProperty(Key)]
    [string] $NetAdapterName

    [DscProperty(Mandatory)]
    [Ensure] $Ensure

    [DscProperty()]
    [validaterange(1,5)]
    [int] $TimestampValue = 5

    [DscProperty(NotConfigurable)]
    [string] $RegistryKeyword

    [DscProperty(NotConfigurable)]
    [string] $RegistryValue

    [DscProperty(NotConfigurable)]
    [string] $interfaceGUID


    [SoftwareTimestamping] Get() {
        $NetAdapter = (Get-NetAdapter -Name $this.NetAdapterName)
        
        $status = Get-NetAdapterAdvancedProperty -InterfaceDescription $NetAdapter.InterfaceDescription `
                                                 -RegistryKeyword SoftwareTimestampSettings -AllProperties `
                                                 -ErrorAction SilentlyContinue | Select-Object Name, RegistryKeyword, RegistryValue
        
        $this.interfaceGUID   = $NetAdapter.InterfaceGUID
        
        If ($status.RegistryValue -eq $NULL) {
            $this.RegistryValue = 'Not Configured'
        } 
        Else {
            $this.RegistryValue   = $status.RegistryValue
        }

        If ($status.RegistryKeyword -eq $NULL) {
            $this.RegistryKeyword = 'Not Configured'
        } 
        Else {
            $this.RegistryKeyword   = $status.RegistryKeyword
        }

        return $this
    }

    [bool] Test() {
        $this.Get()

        switch ($this.Ensure) {
            'Absent' {
                if ($this.RegistryKeyword -ne 'Not Configured') {
                    Write-Verbose -Message " - Expected Software Timestamping Configuration for $($this.NetAdapterName) : Not Configured"
                    Write-Verbose -Message " - Actual Software Timestamping Configuration for $($this.NetAdapterName) : $($this.RegistryValue)"
                    Write-Verbose -Message " --- Configuration required for $($this.NetAdapterName) :: Removing Software Timestamping Configuration"

                    return $false
                }
                else { return $true }
            }

            'Present' {
                If ($this.TimestampValue -eq $this.RegistryValue) {
                    Write-Verbose "No configuration required for $($this.NetAdapterName)"
                } 
                else {
                    Write-Verbose -Message " - Expected Software Timestamping Configuration for $($this.NetAdapterName) : $($this.TimestampValue)"
                    Write-Verbose -Message " - Actual Software Timestamping Configuration for $($this.NetAdapterName) : $($this.RegistryValue)"
                    Write-Verbose -Message " --- Configuration required for $($this.NetAdapterName) :: Setting Software Timestamping Value"
                }
            }

            default { 
                $message = 'Catastrophic Failure: An invalid value for the enum ''Ensure'' was presented to the Test() Method. Debug the DSC Resource using ''Enable-DscDebug -BreakAll'' and determine the cause of entry of the ''default'' switch case'
                Write-Error -Category InvalidArgument -Message $message 
            
                break 
            }
        }
        
        return ($this.TimestampValue -eq $this.RegistryValue)
    }

    [void] Set() {
        $this.Get()

        Get-ChildItem 'HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}' -ErrorAction SilentlyContinue | ForEach-Object {
            $psPath = $_.PSPath
            $driverDesc = (Get-ItemProperty -Path $PsPath).DriverDesc

            if ( (Get-ItemProperty -Path $PsPath) -match $this.InterfaceGUID) {
                switch ($this.Ensure) {
                    'Absent' {
                        Write-Warning "Disabling Software Timestamping on adapter: $($this.NetAdapterName)"
                        
                        Remove-ItemProperty -Path $psPath -Name SoftwareTimestampSettings -ErrorAction SilentlyContinue
                    }

                    'Present' {
                        Switch ($driverDesc) {
                            {
                                $PSItem -like '*Microsoft Kernel Debug*' -or 
                                $PSItem -like '*WAN Miniport*' -or 
                                $PSItem -like '*WI-FI*'
                            } 
                            {
                                Write-Error "The following adapter is not supported for Software Timestamping: $($this.NetAdapterName)"
                                Write-Warning 'Enabling software timestamps on Kernel Debug adapters, WAN Miniports, or WI-FI adapters is not supported'

                                break
                            }
            
                            default {
                                Write-Verbose "Enabling Software Timestamping on adapter: $($this.NetAdapterName)"
                                Set-ItemProperty -Path $psPath -Name SoftwareTimestampSettings -Value $this.TimestampValue
                            }
                        }
                    }
                }
            }
        }
    }
}

Export-ModuleMember -Function *-SWTimestamping