-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-TrustedHosts.ps1
180 lines (109 loc) · 5.73 KB
/
Get-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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#####
### 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 retrieving the trusted hosts from a machine.
### If the '$Remote' switch is not set then only the trusted
### hosts from the local machine will be retrieved.
###
### Author: Weston Berg
### Date: Aug. 5, 2018
#####
Function Get-TrustedHosts {
[CmdletBinding(DefaultParameterSetName='RunLocal')]
param(
[Parameter(ParameterSetName='RunLocal', Mandatory=$false)][switch]$Local = $false,
[Parameter(ParameterSetName='RunRemote', Mandatory=$false)][switch]$Remote = $false,
[Parameter(ParameterSetName='RunRemote', Mandatory=$true,ValueFromPipeline=$true)][string[]]$ComputerName,
[Parameter(ParameterSetName='RunRemote', Mandatory=$true)][PSCredential]$Credential
)
# Pre-Processing
Begin{}
# Main processing block.
# Get the Trusted Hosts via WSMan and 'return' them as output with format 'Computer : TrustedHosts'
Process{
# Determine if caller only wants local scope or is retrieving from remote computers
if([string]::Equals($PSCmdlet.ParameterSetName, 'RunLocal')) {
# Only retrieve the trusted hosts for the local computer
Write-Verbose "Retrieving Trusted Hosts from local machine"
# Get the Trusted Hosts list from WSMan
$TrustedHosts = Get-Item WSMan:\localhost\Client\TrustedHosts | Select -ExpandProperty Value
# Format the output
$Output = "localhost : $TrustedHosts"
$Output
# Ignore any computers passed in. Return after one run
Break
} else {
# Piping through ForEach loop allows for computers to be passed in via pipline or parameter
$ComputerName | ForEach-Object {
# Retrieve Trusted host from specified computer(s) using provided credentials
Write-Verbose "Retrieving Trusted Hosts from remote machine"
# Test network connection to the computer
Write-Verbose "Checking connection to $_..."
if(Test-Connection -ComputerName $_ -Count 1 -Quiet) {
Write-Verbose "Connection to $_ verified`n"
try {
# Open PowerShell Session
Write-Verbose "Creating PowerShell session..."
try {
$PsSession = New-PSSession -ComputerName $_ -Credential $Credential -ErrorAction Stop
} catch {
Write-Host "Error occurred during creation of PowerShell session" -ForegroundColor Red
# Throw error to outer catch
throw
}
Write-Verbose "PowerShell session created successfully!`n"
# Retrieve Trusted Hosts
Write-Verbose "Retrieving Trusted Hosts from $_..."
try {
$RemoteHosts = Invoke-Command -Session $PsSession -ErrorAction Stop -ScriptBlock {
# Get the Trusted Hosts list from WSMan
$TrustedHosts = Get-Item WSMan:\localhost\Client\TrustedHosts | Select -ExpandProperty Value
# Format the output
$Output = "$args : $TrustedHosts"
$Output
} -ArgumentList $_
$RemoteHosts
} catch {
Write-Host "Error while retrieving Trusted Hosts from $_" -ForegroundColor Red
# Remove PowerShell session before exiting
Remove-PSSession $PsSession
# Throw error to outer catch
throw
}
Write-Verbose "Trusted Hosts retrieved successfully`n"
# Remove the PowerShell session
Write-Verbose "Removing PowerShell session..."
try {
Remove-PSSession $PsSession -ErrorAction Stop
} catch {
Write-Host "Error while removing PowerShell session" -ForegroundColor Red
# Throw error to outer catch
throw
}
Write-Verbose "PowerShell session removed`n"
} catch {
# Display error
Write-Host $Error[0] -ForegroundColor Red
}
} else {
Write-Verbose "Unable to connect to machine $_"
"$_ : Unable To Connect"
}
}
}
} # Process
# Post-Processing
End{}
}
# Testing
#Get-TrustedHosts -Verbose
#"localhost", "RemoteMachine" | Get-TrustedHosts -Credential (Get-Credential) -Verbose
#Get-TrustedHosts -Local -Verbose
#Get-TrustedHosts -Remote -ComputerName ("localhost","RemoteMachine") -Credential (Get-Credential) -Verbose