Skip to content

Instantly share code, notes, and snippets.

@ddikman
Created November 15, 2016 09:43
Show Gist options
  • Save ddikman/30178994b4c790786b8f70142afcce49 to your computer and use it in GitHub Desktop.
Save ddikman/30178994b4c790786b8f70142afcce49 to your computer and use it in GitHub Desktop.
Rework/addition to the AddingTrustedSites.ps1 from Microsoft. Adds the specified IP ranges to trusted sites in Internet Explorer.
#---------------------------------------------------------------------------------
# This script is an addition to the AddingTrustedRanges.ps1 script provided by Microsoft
#---------------------------------------------------------------------------------
#requires -version 2.0
<#
.SYNOPSIS
The PowerShell script which can be used to add trusted IP ranges in Internet Explorer.
.DESCRIPTION
The PowerShell script which can be used to add trusted IP ranges in Internet Explorer.
.PARAMETER TrustedAddresses
Spcifies the trusted ip addresses in Internet Explorer.
.PARAMETER HTTP
Once you use the HTTP switch parameter, the domain will be use the http:// prefix.
.EXAMPLE
C:\PS> C:\AddingTrustedRanges.ps1 -TrustedAddresses "192.168.1.1","192.168.1.2" -HTTP
Successfully added '192.168.1.1' and '192.168.1.2' domain to trusted sites in Internet Explorer.
#>
Param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ParameterSetName="SingleDomain")]
[Alias('Addresses')][ValidateNotNullOrEmpty()]
[String[]]$TrustedAddresses,
[Parameter(Mandatory=$false)]
[Switch]$HTTP
)
#Initialize key variables
$UserRegPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges"
#Figure out next range name
Function GetNextRangeName
{
Param
(
[String]$KeyPath
)
$existingRanges = (Get-ChildItem -Path $KeyPath -Name).Count
return "Range$($existingRanges + 1)"
}
#Main function
Function CreateKeyReg
{
Param
(
[String]$KeyPath,
[String]$Name
)
If(Test-Path -Path $KeyPath)
{
Write-Verbose "Creating a new key '$Name' under $KeyPath."
New-Item -Path "$KeyPath" -ItemType File -Name "$Name" `
-ErrorAction SilentlyContinue | Out-Null
}
Else
{
Write-Warning "The path '$KeyPath' not found."
}
}
Function SetRegValues
{
Param
(
[Boolean]$blnHTTP=$false,
[String]$Address,
[String]$RegPath
)
Try
{
$TrustedZone = 2
If ($blnHTTP) { $protocol = 'http' } Else { $protocol = 'https' }
Set-ItemProperty -Path $RegPath -Name $protocol -Value $TrustedZone `
-ErrorAction SilentlyContinue | Out-Null
Write-Verbose "Creating a Dword value named '$protocol.ToUpper()' and set the value to $TrustedZone."
Set-ItemProperty -Path $RegPath -Name ":Range" -Value $Address `
-ErrorAction SilentlyContinue | Out-Null
Write-Verbose "Creating a string value named ':Range' and set the value to '$Address'."
}
Catch
{
Write-Host "Failed to add trusted sites in Internet Explorer." -BackgroundColor Red
}
}
#Adding trusted sites in the registry
Foreach($TrustedAddress in $TrustedAddresses)
{
$RangeName = GetNextRangeName -KeyPath $UserRegPath
CreateKeyReg -KeyPath $UserRegPath -Name $RangeName
$RangePath = "$UserRegPath\$RangeName"
SetRegValues -RegPath $RangePath -Address $TrustedAddress -blnHTTP $HTTP
Write-Host "Successfully added '$TrustedAddress' domain to trusted Sites in Internet Explorer."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment