-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remove-TrustedHosts.ps1
131 lines (82 loc) · 3.82 KB
/
Remove-TrustedHosts.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#####
### Copyright (c) 2019 Weston Berg
### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
### IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
### FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
### AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
### LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
### OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
### SOFTWARE.
###
### CMDLET for removing trusted hosts from a machine.
### If the '$Remote' switch is not set then only the trusted
### hosts from the local machine will be removed if necessary.
###
### Author: Weston Berg
### Date: Aug. 14, 2018
#####
Function Remove-TrustedHosts {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=0)][string[]]$Hosts,
[Parameter(Mandatory=$true)][PSCredential]$Credential,
[Parameter(Mandatory=$false)][switch]$RemoveDuplicates = $false
)
# Pre-Processing
# Only want the initial snapshot of the Trusted Hosts
Begin{
# Retrieve current Trusted Hosts string
Write-Verbose "Retrieving current list of Trusted Hosts"
$TrustedHosts = Get-Item WSMan:\localhost\Client\TrustedHosts -ErrorAction Stop | Select -ExpandProperty Value
Write-Verbose "Initial Trusted Hosts: $TrustedHosts`n"
}
# Main processing block.
# Determine the new set of Trusted Hosts after removal.
Process{
# Split list for checking supplied hosts against
$TrustedHostsArr = $TrustedHosts.Split(',')
# Boolean for keeping track of if the Trusted Host list is changed
$ListChanged = $false
# Boolean for keeping track of if duplicate has been/should be removed
$Removable = $true
# Remove the Hosts if they are present in the Trusted Hosts list
Write-Verbose "Beginning Trusted Host removal...`n"
ForEach($RemoveHost in $Hosts) {
# Reset for each new host getting checked
$Removable = $true
Write-Verbose "Checking for $RemoveHost and removing if present"
# For reference go to: https://stackoverflow.com/a/39921952
$TrustedHostsArr = $TrustedHostsArr | ForEach-Object {
if(($_ -eq $RemoveHost) -and $Removable) {
# If not supposed to remove duplicates set removable flag to false
if($Removable -and (!$RemoveDuplicates)) {
$Removable = $false
}
# Set flag indicating Trusted Host has been removed
$ListChanged = $true
Write-Verbose "$_ removed successfully"
} else { $_ }
}
}
}
# Post-Processing
# Only want to set the Trusted Hosts once if necessary
End{
Write-Verbose "`n"
# Check if trusted hosts was modified, if not do nothing
if($ListChanged) {
# Rejoin Trusted Hosts
$UpdatedHosts = [string]::Join(",", $TrustedHostsArr)
# Add updated list to the computer
Write-Verbose "Setting Trusted Hosts to $UpdatedHosts"
# Set Trusted Hosts
Set-Item "WSMan:\localhost\Client\TrustedHosts" -Value $UpdatedHosts -ErrorAction Stop -Credential $Credential
} else {
# Nothing changed
Write-Verbose "Trusted Hosts unchanged"
}
}
}
# Testing
#Remove-TrustedHosts -Hosts "Host1", "HOST2", "HoSt4" -Credential(Get-Credential) -RemoveDuplicates -Verbose
#Remove-TrustedHosts "HOST1","hOsT3" -Credential (Get-Credential) -Verbose