@@ -6,10 +6,16 @@ function Send-PageViewTelemetry
6
6
(
7
7
[Parameter (
8
8
Mandatory = $true ,
9
- HelpMessage = ' Specify the page or operation name .' )]
10
- [System.Management.Automation.CommandInfo ]
9
+ HelpMessage = ' Specify the source cmdlet object .' )]
10
+ [System.Management.Automation.PSCmdlet ]
11
11
[ValidateNotNullOrEmpty ()]
12
- $CommandInfo ,
12
+ $SourcePSCmdlet ,
13
+
14
+ [Parameter (
15
+ Mandatory = $false ,
16
+ HelpMessage = ' Specify the parameter set name.' )]
17
+ [System.Boolean ]
18
+ $IsSuccess ,
13
19
14
20
[Parameter (
15
21
Mandatory = $false ,
@@ -31,26 +37,61 @@ function Send-PageViewTelemetry
31
37
)
32
38
Process
33
39
{
34
- if ($null -eq [Constants ]::TelemetryClient)
35
- {
40
+ if ($null -eq [Constants ]::TelemetryClient) {
41
+ Write-Verbose - Message ' Initialize telemetry client '
36
42
$TelemetryClient = New-Object Microsoft.ApplicationInsights.TelemetryClient
37
43
$TelemetryClient.InstrumentationKey = [Constants ]::PublicTelemetryInstrumentationKey
38
44
$TelemetryClient.Context.Session.Id = $CurrentSessionId
39
45
$TelemetryClient.Context.Device.OperatingSystem = [System.Environment ]::OSVersion.ToString()
40
46
[Constants ]::TelemetryClient = $TelemetryClient
41
47
}
42
48
49
+ if ([string ]::IsNullOrWhiteSpace([Constants ]::HashMacAddress)) {
50
+ Write-Verbose - Message ' hash mac address'
51
+ $macAddress = ' '
52
+ $nics = [System.Net.NetworkInformation.NetworkInterface ]::GetAllNetworkInterfaces()
53
+ foreach ($nic in $nics ) {
54
+ if ($nic.OperationalStatus -eq ' Up' -and -not [string ]::IsNullOrWhiteSpace($nic.GetPhysicalAddress ())) {
55
+ $macAddress = $nic.GetPhysicalAddress ().ToString()
56
+ }
57
+ }
58
+
59
+ if ($macAddress -ne ' ' ) {
60
+ $bytes = [System.Text.Encoding ]::UTF8.GetBytes($macAddress )
61
+ $sha256 = New-Object - TypeName System.Security.Cryptography.SHA256CryptoServiceProvider
62
+ $macAddress = [System.BitConverter ]::ToString($sha256.ComputeHash ($bytes ))
63
+ $macAddress = $macAddress.Replace (' -' , ' ' ).ToLowerInvariant()
64
+ }
65
+ [Constants ]::HashMacAddress = $macAddress
66
+ }
67
+
43
68
$client = [Constants ]::TelemetryClient
44
69
45
70
$page = New-Object Microsoft.ApplicationInsights.DataContracts.PageViewTelemetry
46
71
$page.Name = " cmdletInvocation"
47
72
$page.Duration = $Duration
48
73
49
- $page.Properties [" IsSuccess" ] = $True .ToString()
50
- $page.Properties [" OS" ] = [System.Environment ]::OSVersion.ToString()
74
+ if ($PSBoundParameters.ContainsKey (' IsSuccess' )) {
75
+ $page.Properties [' IsSuccess' ] = $PSBoundParameters [' IsSuccess' ].ToString()
76
+ } else {
77
+ $page.Properties [' IsSuccess' ] = $true .ToString()
78
+ }
79
+ $page.Properties [' x-ms-client-request-id' ] = [Constants ]::CurrentSessionId
80
+ $page.Properties [' OS' ] = [System.Environment ]::OSVersion.ToString()
81
+ $page.Properties [' HostVersion' ] = $PSCmdlet.Host.Version
82
+ $page.Properties [' HashMacAddress' ] = [Constants ]::HashMacAddress
83
+ $page.Properties [' PowerShellVersion' ] = $PSVersionTable.PSVersion.ToString ()
84
+ $page.Properties [' Command' ] = $SourcePSCmdlet.MyInvocation.MyCommand.Name
85
+ $page.Properties [' CommandParameterSetName' ] = $SourcePSCmdlet.ParameterSetName
86
+ $page.Properties [' CommandInvocationName' ] = $SourcePSCmdlet.MyInvocation.InvocationName
51
87
52
- $page.Properties [" x-ms-client-request-id" ]= [Constants ]::CurrentSessionId
53
- $page.Properties [" PowerShellVersion" ]= $PSVersionTable.PSVersion.ToString ();
88
+ if ($null -ne $SourcePSCmdlet.MyInvocation.BoundParameters ) {
89
+ $parameters = " "
90
+ foreach ($Key in $SourcePSCmdlet.MyInvocation.BoundParameters.Keys ) {
91
+ $parameters += " -$Key *** "
92
+ }
93
+ $page.Properties [' CommandParameters' ] = $parameters
94
+ }
54
95
55
96
if ($null -ne $MyInvocation.MyCommand )
56
97
{
@@ -60,7 +101,7 @@ function Send-PageViewTelemetry
60
101
$page.Properties [" ModuleVersion" ] = $MyInvocation.MyCommand.Module.Version.ToString ()
61
102
}
62
103
}
63
- $page.Properties [" start-time" ]= $StartDateTime
104
+ $page.Properties [" start-time" ]= $StartDateTime.ToUniversalTime ().ToString( " o " )
64
105
$page.Properties [" end-time" ]= (Get-Date ).ToUniversalTime().ToString(" o" )
65
106
$page.Properties [" duration" ]= $Duration.ToString (" c" );
66
107
@@ -75,16 +116,11 @@ function Send-PageViewTelemetry
75
116
}
76
117
}
77
118
78
- foreach ($Key in $page.Properties )
79
- {
80
- Write-Debug " collect telemetry data[$Key ]=$ ( $CustomProperties [$Key ]) "
81
- }
82
-
83
119
$client.TrackPageView ($page )
84
120
85
121
try
86
122
{
87
- // $client.Flush ()
123
+ $client.Flush ()
88
124
}
89
125
catch
90
126
{
0 commit comments