-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSet-Registry-KeyValue.ps1
97 lines (89 loc) · 3.69 KB
/
Set-Registry-KeyValue.ps1
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
<#
Author: Stan Crider
Date: 14July2020
Crap:
Sets the specified registry key on all domain controllers in specified domain and restarts specified service
#>
## Set user variables; specify the specifics
$DesiredRegistryPath = "HKLM:\SYSTEM\CurrentControlSet\Services\DNS\Parameters\"
$DesiredKey = "TcpReceivePacketSize"
$DesiredValue = 65280
# Specify the service object name to be restarted
$DesiredService = "DNS"
# Specify domain or set to (Get-ADDomain).DNSRoot for current domain
$Domain = (Get-ADDomain).DNSRoot
## Script below
$Servers = Get-ADDomainController -Filter * -Server $Domain | Sort-Object HostName
ForEach($Server in $Servers){
# Reset reusable variables for each round
$CurrentValue = $null
If($env:COMPUTERNAME -match $Server.name){
$ServerName = "localhost"
}
Else{
$ServerName = $Server.Name
}
# Test connectivity to server
$Online = Test-Connection -ComputerName $ServerName -Quiet -Count 2
If($Online){
# Test registry path
Try{
$TestPath = Invoke-Command -ComputerName $ServerName -ScriptBlock {Test-Path -Path $using:DesiredRegistryPath} -ErrorAction Stop
}
Catch{
$TestPath = $false
}
If($TestPath -eq $true){
# Test registry key
Try{
Invoke-Command -ComputerName $ServerName -ScriptBlock {Get-ItemProperty -Path $using:DesiredRegistryPath | Select-Object -ExpandProperty $using:DesiredKey} -ErrorAction Stop | Out-Null
$TestKey = $true
}
Catch{
$TestKey = $false
}
If($TestKey -eq $true){
# Check value of registry key
$CurrentValue = Invoke-Command -ComputerName $ServerName -ScriptBlock {(Get-ItemProperty -Path $using:DesiredRegistryPath -Name $using:DesiredKey).$using:DesiredKey}
If($CurrentValue -eq $DesiredValue){
# No changes necessary; abort
Write-Output ("The registry key $DesiredKey on $ServerName is already set to $DesiredValue.")
}
Else{
# Change value of registry key
Write-Output ("The registry key $DesiredKey on $ServerName will be changed from $CurrentValue to $DesiredValue.")
Try{
Invoke-Command -ComputerName $ServerName -ScriptBlock {
Set-ItemProperty -Path $using:DesiredRegistryPath -Name $using:DesiredKey -Value $using:DesiredValue
Restart-Service -Name $using:DesiredService
} -ErrorAction Stop
}
Catch{
Write-Error $_.Exception.Message
}
}
}
Else{
# Create key and set value
Write-Output ("The registry key $DesiredKey will be created on $ServerName.")
Try{
Invoke-Command -ComputerName $ServerName -ScriptBlock {
New-ItemProperty -Path $using:DesiredRegistryPath -Name $using:DesiredKey -Value $using:DesiredValue
Restart-Service -Name $using:DesiredService
} -ErrorAction Stop
}
Catch{
Write-Error $_.Exception.Message
}
}
}
Else{
# If registry path does not exist, abort
Write-Warning ("The registry path $DesiredRegistryPath does not exist on $ServerName.")
}
}
Else{
# If server does not respond, abort
Write-Warning ("The server $ServerName is not online.")
}
}