-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddRoute.ps1
108 lines (62 loc) · 1.95 KB
/
AddRoute.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
[CmdletBinding(DefaultParameterSetName='None')]
Param
(
# Param1 help description
[Parameter(Mandatory = $true)]
$Network,
#$NextHop = '100.69.197.253',
[Parameter(Mandatory = $true)]
$SubNet,
[Parameter(Mandatory = $true)]
$Gateway,
[Switch]$Persistent,
[string[]]
$Computers = ( Get-Content 'C:\Users\fhadmin\Desktop\computers.txt' )
)
Begin
{
[string]$routeString = 'Invoke-Expression "route'
if ( $PSBoundParameters['Persistent'] ) {
$routeString += ' -p'
}
$routeString += ' add {0} MASK {1} {2}"' -f $Network, $SubNet,$Gateway
$scriptblock = [scriptblock]::Create( $routeString )
Function Test-RemoteConnection ( $Conn ) {
Try {
if ( $Conn -match $env:COMPUTERNAME ) {
return $true
} else {
Invoke-Command -ScriptBlock {$true} -ComputerName $Conn -ea stop
}
} catch {
return $false
}
}
}
Process
{
foreach ($c in $computers ) {
if ( Test-RemoteConnection $c ) {
#Invoke-Command -ScriptBlock $test -ComputerName $c
$tempresult = '' | Select-Object Computer,Result
$tempresult.Computer = $c
try {
$CommandResult = Invoke-Command -ScriptBlock $scriptblock -ComputerName $c -ErrorAction Stop
if ( [string]::IsNullOrEmpty( $CommandResult ) ) {
Write-Warning "No Results"
Write-Warning "Computer: $c"
Write-Warning "Command: $routeString"
}
$tempresult.Result = $CommandResult
} catch {
$tempresult.Result = $_.Exception.Message
}
$tempresult
} else {
Write-Warning "Unable to connect to $c"
}
}
}
End
{
}