This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Remove-TrustedHost.ps1
97 lines (80 loc) · 3.11 KB
/
Remove-TrustedHost.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
###############################################################################################################
# Language : PowerShell 4.0
# Filename : Remove-TrustedHost.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Remove a trusted host (WinRM)
# Repository : https://github.com/BornToBeRoot/PowerShell
###############################################################################################################
<#
.SYNOPSIS
Remove a trusted host (WinRM)
.DESCRIPTION
Remove one, multiple or all trusted host(s) (WinRM).
.EXAMPLE
Remove-TrustedHost -TrustedHost "192.168.178.27", "TEST-DEVICE-02"
.EXAMPLE
Remove-TrustedHost -All
.LINK
https://github.com/BornToBeRoot/PowerShell/blob/master/Documentation/Function/Remove-TrustedHost.README.md
#>
function Remove-TrustedHost
{
[CmdletBinding(DefaultParameterSetName='TrustedHost', SupportsShouldProcess=$true, ConfirmImpact="High")]
param(
[Parameter(
ParameterSetName='TrustedHost',
Position=0,
Mandatory=$true)]
[String[]]$TrustedHost,
[Parameter(
ParameterSetName='All',
Position=0,
Mandatory=$true,
HelpMessage="Remove all trusted host(s)")]
[switch]$All
)
Begin{
if(-not([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator"))
{
throw "Administrator rights are required to remove a trusted host!"
}
}
Process{
$TrustedHost_Path = "WSMan:\localhost\Client\TrustedHosts"
[System.Collections.ArrayList]$TrustedHosts = @()
try{
$TrustedHost_Value = (Get-Item -Path $TrustedHost_Path).Value
$TrustedHost_ValueOrg = $TrustedHost_Value
if($PSCmdlet.ParameterSetName -eq "TrustedHost")
{
if(-not([String]::IsNullOrEmpty($TrustedHost_Value)))
{
$TrustedHosts += $TrustedHost_Value.Split(',')
}
foreach($TrustedHost2 in $TrustedHost)
{
if($TrustedHosts -notcontains $TrustedHost2)
{
Write-Warning -Message "Trusted host ""$TrustedHost2"" does not exists in ""$TrustedHost_Path"" and will be skipped."
continue
}
$TrustedHosts.Remove($TrustedHost2)
}
$TrustedHost_Value = $TrustedHosts -join ","
}
elseif($PSCmdlet.ParameterSetName -eq "All")
{
$TrustedHost_Value = ""
}
if(($TrustedHost_Value -ne $TrustedHost_ValueOrg) -and ($PSCmdlet.ShouldProcess($TrustedHost_Path)))
{
Set-Item -Path $TrustedHost_Path -Value $TrustedHost_Value -Force
}
}
catch{
throw
}
}
End{
}
}