· 9 years ago · Jan 17, 2017, 04:18 PM
1#REG ADD 'HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings' /v AutoConfigURL /t REG_MULTI_SZ /d "hallo123456" /f
2#
3#
4#
5## Author: Miodrag Milic <miodrag.milic@gmail.com>
6## Last Change: 14-Aug-2015.
7#
8##requires -version 2.0
9#
10#<#
11#.SYNOPSIS
12# Get or set system proxy properties.
13#.DESCRIPTION
14# This function implements unified method to set proxy system wide settings.
15# It sets both WinINET ("Internet Options" proxy) and WinHTTP proxy.
16# Without any arguments function will return the current proxy properties.
17# To change a proxy property pass adequate argument to the function.
18#.EXAMPLE
19# Update-Proxy -Server "myproxy.mydomain.com:8080" -Override "" -ShowGUI
20# Set proxy server, clear overrides and show IE GUI.
21#.EXAMPLE
22# Update-Proxy | Export-CSV proxy; Import-CSV proxy | Update-Proxy -Verbose
23# Save and restore proxy properties
24#.EXAMPLE
25# $p = Update-Proxy; $p.Override += $p.Override += "*.domain.com" ; $p | proxy
26# Add "*.domain.com" to the proxy override list
27#.NOTES
28# The format of the parameters is the same as seen in Internet Options GUI.
29# To bypass proxy for a local network specify keyword ";<local>" at the end
30# of the Overide values.
31# Setting the winhttp proxy requires administrative prvilegies.
32#.OUTPUTS
33# [HashTable]
34##>
35#function Update-Proxy() {
36# [CmdletBinding()]
37# param(
38# # Proxy:Port
39# [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
40# [string] $Server,
41# # Semicollon delimited list of exlusions
42# [Parameter(ValueFromPipelineByPropertyName=$true)]
43# [string] $Override,
44# # 0 to disable, anything else to enable proxy
45# [Parameter(ValueFromPipelineByPropertyName=$true)]
46# [string] $Enable,
47# # Show Internet Options GUI
48# [switch] $ShowGUI
49# )
50# $key = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
51# $r = gp $key
52# Write-Verbose "Reading proxy data from the registry"
53# $proxy=@{
54# Server = if ($PSBoundParameters.Keys -contains 'Server') {$Server} else { $r.ProxyServer }
55# Override = if ($PSBoundParameters.Keys -contains 'Override') {$Override} else { $r.ProxyOverride }
56# Enable = if ($PSBoundParameters.Keys -contains 'Enable') {$Enable} else { $r.ProxyEnable }
57# }
58#
59# $set = "Server","Override","Enable" | ? {$PSBoundParameters.Keys -contains $_ }
60# if ($set) {
61# #if (!(test-admin)) { throw "Setting proxy requires admin privileges" }
62#
63# Write-Verbose "Saving proxy data to registry"
64#
65# sp $key ProxyServer $proxy.Server
66# sp $key ProxyOverride $proxy.Override
67# sp $key ProxyEnable $proxy.Enable
68# if (!(refresh-system)) { Write-Warning "Can not force system refresh after proxy change" }
69#
70# Write-Verbose "Importing winhttp proxy from IE settings"
71# $OFS = "`n"
72# [string]$res = netsh.exe winhttp import proxy source=ie
73# if ($res -match 'Access is denied') {Write-Warning $res}
74# else { Write-Verbose $res.Trim()}
75# }
76#
77# New-Object PSCustomObject -Property $proxy
78# if ($ShowGUI) { start control "inetcpl.cpl,,4" }
79#}
80#
81## The registry changes aren't seen until system is notified about it.
82## Without this function you need to open Internet Settings window for changes to take effect. See http://goo.gl/OIQ4W4
83#function refresh-system() {
84# $signature = @'
85#[DllImport("wininet.dll", SetLastError = true, CharSet=CharSet.Auto)]
86#public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
87#'@
88#
89# $INTERNET_OPTION_SETTINGS_CHANGED = 39
90# $INTERNET_OPTION_REFRESH = 37
91# $type = Add-Type -MemberDefinition $signature -Name wininet -Namespace pinvoke -PassThru
92# $a = $type::InternetSetOption(0, $INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
93# $b = $type::InternetSetOption(0, $INTERNET_OPTION_REFRESH, 0, 0)
94# return $a -and $b
95#}
96#
97#Set-Alias proxy Update-Proxy
98#proxy -Server "helloproxy.mydomain.com:8080" -Override "" -ShowGUI
99
100
101<#
102.SYNOPSIS
103 This script nullifies the AutoConfigURL registry value in the HKCU hive for the current and all
104 other users. This removes the auto configuration proxy URL value in IE.
105.NOTES
106 Created on: 5/7/2015
107 Created by: Adam Bertram
108 Filename: Remove-AutoConfigProxy.ps1
109.EXAMPLE
110 PS> .\Remove-AutoConfigProxy.ps1
111
112 This nullifies the AutoConfigURL HKCU registry value for the current and all other user profiles to
113 remove the IE auto configuration URL.
114
115#>
116[CmdletBinding()]
117param ()
118begin {
119 $ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
120 Set-StrictMode -Version Latest
121
122 function Get-LoggedOnUserSID {
123 <#
124 .SYNOPSIS
125 This function queries the registry to find the SID of the user that's currently logged onto the computer interactively.
126 #>
127 [CmdletBinding()]
128 param ()
129 process {
130 try {
131 if (-not (Get-PSDrive -Name HKU -ErrorAction SilentlyContinue)) {
132 New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS | Out-Null
133 }
134 (Get-ChildItem HKU: | where { $_.Name -match 'S-\d-\d+-(\d+-){1,14}\d+$' }).PSChildName
135 } catch {
136 Write-Warning -Message "$($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)"
137 }
138 }
139 }
140
141 function Set-RegistryValueForAllUsers {
142 <#
143 .SYNOPSIS
144 This function sets a registry value in every user profile hive.
145 .EXAMPLE
146 PS> Set-RegistryValueForAllUsers -RegistryInstance @{'Name' = 'Setting'; 'Type' = 'String'; 'Value' = 'someval'; 'Path' = 'SOFTWARE\Microsoft\Windows\Something'}
147
148 This example would modify the string registry value 'Type' in the path 'SOFTWARE\Microsoft\Windows\Something' to 'someval'
149 for every user registry hive.
150 .PARAMETER RegistryInstance
151 A hash table containing key names of 'Name' designating the registry value name, 'Type' to designate the type
152 of registry value which can be 'String,Binary,Dword,ExpandString or MultiString', 'Value' which is the value itself of the
153 registry value and 'Path' designating the parent registry key the registry value is in.
154 .PARAMETER Remove
155 A switch parameter that overrides the default setting to only change or add registry values. This removes one of more registry keys instead.
156 If this parameter is used the only required key in the RegistryInstance parameter is Path. This will automatically remove both
157 the x86 and x64 paths if the key is a child under the SOFTWARE key. There's no need to specify the WOW6432Node path also.
158 .PARAMETER Force
159 A switch parameter that is used if the registry value key path doesn't exist will create the entire parent/child key hierachy and creates the
160 registry value. If this parameter is not used, if the key the value is supposed to be in does not exist the function will skip the value.
161 #>
162 [CmdletBinding()]
163 param (
164 [Parameter(Mandatory = $true)]
165 [hashtable[]]$RegistryInstance,
166 [switch]$Remove
167 )
168 begin {
169 if (-not (Get-PSDrive -Name HKU -ErrorAction SilentlyContinue)) {
170 New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS | Out-Null
171 }
172 }
173 process {
174 try {
175 ## Change the registry values for the currently logged on user
176 $LoggedOnSids = Get-LoggedOnUserSID
177 if ($LoggedOnSids -is [string]) {
178 Write-Verbose -Message "Found 1 logged on user SID"
179 } else {
180 Write-Verbose -Message "Found $($LoggedOnSids.Count) logged on user SIDs"
181 }
182 foreach ($sid in $LoggedOnSids) {
183 Write-Verbose -Message "Loading the user registry hive for the logged on SID $sid"
184 foreach ($instance in $RegistryInstance) {
185 if ($Remove.IsPresent) {
186 Write-Verbose -Message "Removing registry key '$($instance.path)'"
187 Remove-Item -Path "HKU:\$sid\$($instance.Path)" -Recurse -Force -ea 'SilentlyContinue'
188 } else {
189 if (!(Get-Item -Path "HKU:\$sid\$($instance.Path)" -ea 'SilentlyContinue')) {
190 Write-Verbose -Message "The registry key HKU:\$sid\$($instance.Path) does not exist. Creating..."
191 New-Item -Path "HKU:\$sid\$($instance.Path | Split-Path -Parent)" -Name ($instance.Path | Split-Path -Leaf) -Force | Out-Null
192 } else {
193 Write-Verbose -Message "The registry key HKU:\$sid\$($instance.Path) already exists. No need to create."
194 }
195 Write-Verbose -Message "Setting registry value $($instance.Name) at path HKU:\$sid\$($instance.Path) to $($instance.Value)"
196
197 Set-ItemProperty -Path "HKU:\$sid\$($instance.Path)" -Name $instance.Name -Value $instance.Value -Type $instance.Type -Force
198 }
199 }
200 }
201
202 foreach ($instance in $RegistryInstance) {
203 if ($Remove.IsPresent) {
204 if ($instance.Path.Split('\')[0] -eq 'SOFTWARE' -and ((Get-Architecture) -eq 'x64')) {
205 $Split = $instance.Path.Split('\')
206 $x86Path = "HKCU\SOFTWARE\Wow6432Node\{0}" -f ($Split[1..($Split.Length)] -join '\')
207 $CommandLine = "reg delete `"{0}`" /f && reg delete `"{1}`" /f" -f "HKCU\$($instance.Path)", $x86Path
208 } else {
209 $CommandLine = "reg delete `"{0}`" /f" -f "HKCU\$($instance.Path)"
210 }
211 } else {
212 ## Convert the registry value type to one that reg.exe can understand
213 switch ($instance.Type) {
214 'String' {
215 $RegValueType = 'REG_SZ'
216 }
217 'Dword' {
218 $RegValueType = 'REG_DWORD'
219 }
220 'Binary' {
221 $RegValueType = 'REG_BINARY'
222 }
223 'ExpandString' {
224 $RegValueType = 'REG_EXPAND_SZ'
225 }
226 'MultiString' {
227 $RegValueType = 'REG_MULTI_SZ'
228 }
229 default {
230 throw "Registry type '$($instance.Type)' not recognized"
231 }
232 }
233 if (!(Get-Item -Path "HKCU:\$($instance.Path)" -ea 'SilentlyContinue')) {
234 Write-Verbose -Message "The registry key 'HKCU:\$($instance.Path)'' does not exist. Creating..."
235 New-Item -Path "HKCU:\$($instance.Path) | Split-Path -Parent)" -Name ("HKCU:\$($instance.Path)" | Split-Path -Leaf) -Force | Out-Null
236 }
237 if (-not $instance.Value) {
238 $instance.Value = '""'
239 }
240 $CommandLine = "reg add `"{0}`" /v {1} /t {2} /d {3} /f" -f "HKCU\$($instance.Path)", $instance.Name, $RegValueType, $instance.Value
241 }
242 Set-AllUserStartupAction -CommandLine $CommandLine
243 }
244 } catch {
245 Write-Warning -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)"
246 $false
247 }
248 }
249 }
250
251 function Set-AllUserStartupAction {
252 <#
253 .SYNOPSIS
254 A function that executes a command line for the any current logged on user and uses the Active Setup registry key to set a
255 registry value that contains a command line EXE with arguments that will be executed once for every user that logs in.
256 .PARAMETER CommandLine
257 The command line string that will be executed once at every user logon
258 #>
259 [CmdletBinding()]
260 param (
261 [Parameter(Mandatory = $true)]
262 [string]$CommandLine
263 )
264 process {
265 try {
266 ## Create the Active Setup registry key so that the reg add cmd will get ran for each user
267 ## logging into the machine.
268 ## http://www.itninja.com/blog/view/an-active-setup-primer
269 $Guid = [guid]::NewGuid().Guid
270 Write-Verbose -Message "Created GUID '$Guid' to use for Active Setup"
271 $ActiveSetupRegParentPath = 'HKLM:\Software\Microsoft\Active Setup\Installed Components'
272 New-Item -Path $ActiveSetupRegParentPath -Name $Guid -Force | Out-Null
273 $ActiveSetupRegPath = "HKLM:\Software\Microsoft\Active Setup\Installed Components\$Guid"
274 Write-Verbose -Message "Using registry path '$ActiveSetupRegPath'"
275 Write-Verbose -Message "Setting command line registry value to '$CommandLine'"
276 Set-ItemProperty -Path $ActiveSetupRegPath -Name '(Default)' -Value 'All Users Startup Action' -Force
277 Set-ItemProperty -Path $ActiveSetupRegPath -Name 'Version' -Value '1' -Force
278 Set-ItemProperty -Path $ActiveSetupRegPath -Name 'StubPath' -Value $CommandLine -Force
279 Write-Verbose -Message 'Done'
280 } catch {
281 Write-Warning -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)"
282 $false
283 }
284 }
285 }
286}
287
288process {
289 try {
290 $Instance = @{
291 'Name' = 'AutoConfigURL';
292 'Type' = 'String';
293 'Path' = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings';
294 'Value' = ''
295 }
296 Set-RegistryValueForAllUsers -RegistryInstance $Instance
297 } catch {
298 Write-Error "$($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)"
299 }
300}
301
302Set-RegistryValueForAllUsers -RegistryInstance @{'Name' = 'AutoConfigURL'; 'Type' = 'String'; 'Value' = 'hallo123456'; 'Path' = 'Software\Microsoft\Windows\CurrentVersion\Internet Settings'}