-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerShellTelemetry.ps1
More file actions
101 lines (85 loc) · 3.15 KB
/
PowerShellTelemetry.ps1
File metadata and controls
101 lines (85 loc) · 3.15 KB
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
function Get-PowerShellTelemetryReporting {
<#
.SYNOPSIS
Enables/Disables PowerShell Telemetry Reporting
.DESCRIPTION
PowerShell 7 adds telemetry reporting to Microsoft. In some cases, that's a good thing.
In others, not so much. You decide.
This function will check if THIS instance of PowerShell has telemetry enabled. It's a
global ENV var, but it must exist BEFORE pwsh is instantiated.
.EXAMPLE
Get-PowerShellTelemetryReporting
Shows whether telemetry reporting is enabled
.LINK
https://devblogs.microsoft.com/powershell/new-telemetry-in-powershell-7-preview-3/
#>
param (
[switch] $Simple = $false
)
if ($PSVersionTable.PSVersion.Major -ge 7) {
[string] $setting = $env:POWERSHELL_TELEMETRY_OPTOUT
[bool] $IsEnabled = $true
if (-not $Simple) {
Write-Host "Powershell Telemetry Reporting is controlled via ENV:\POWERSHELL_TELEMETRY_OPTOUT"
Write-Host " To disable, it must be 'true', 'yes', or '1'"
}
if (($setting.Length -le 0) -or ($null = $setting)) {
$IsEnabled = $true
} elseif (($setting.ToLower() -eq 'true') -or ($setting.ToLower() -eq 'yes') -or ($setting -eq '1')) {
$IsEnabled = $false
} else {
if (-not $Simple) { Write-Host " Current setting is: $setting" -ForegroundColor Yellow }
}
if (-not $Simple) {
if ($IsEnabled) {
Write-Host " Telemetry reporting is ENABLED" -ForegroundColor Red
} else {
Write-Host " Telemetry reporting is DISABLED" -ForegroundColor Green
}
}
} else {
if (-not $Simple) { Write-Host 'Only PowerShell version 7 and above have the telemetry reporting option. Reporting is NOT enabled.' -ForegroundColor Green }
$IsEnabled = $false
}
if ($Simple) { $IsEnabled }
}
function Set-PowerShellTelemetryReporting {
<#
.SYNOPSIS
Enables/Disables PowerShell Telemetry Reporting
.DESCRIPTION
PowerShell 7 adds telemetry reporting to Microsoft. In some cases, that's a good thing.
In others, not so much. You decide.
You must be an admin to modify "global" environment variables... and you have to set it
globally because it must exist BEFORE pwsh is instantiated.
.EXAMPLE
Set-PowerShellTelemetryReporting
Tries to disable Telemetry Reporting
.EXAMPLE
Set-PowerShellTelemetryReporting -Enable
Tries to enable Telemetry Reporting
.LINK
https://devblogs.microsoft.com/powershell/new-telemetry-in-powershell-7-preview-3/
#>
param (
$Enable = $false
)
if ($PSVersionTable.PSVersion.Major -ge 7) {
Write-Host "Powershell Telemetry Reporting is controlled via ENV:\POWERSHELL_TELEMETRY_OPTOUT"
Write-Host " To disable, it must be 'true', 'yes', or '1'"
try {
if ($Enable) {
if (Get-PowerShellTelemetryReporting -Simple) { [Environment]::SetEnvironmentVariable("MyTestVariable", $null, "Machine") }
} else {
[Environment]::SetEnvironmentVariable('POWERSHELL_TELEMETRY_OPTOUT', 'true', 'Machine')
}
} catch {
Write-Host 'Error setting environment variable' -ForegroundColor Red
Write-Host 'You prolly need to run this as Admin' -ForegroundColor Yellow
Write-Host $_.Exception.Message
}
}
} else {
Write-Host 'Only PowerShell version 7 and above have the telemetry reporting option. Reporting is not an option.' -ForegroundColor Green
}
}