-
Notifications
You must be signed in to change notification settings - Fork 0
/
VPNconnect.psm1
187 lines (124 loc) · 6.11 KB
/
VPNconnect.psm1
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
181
182
183
184
185
186
187
function VPNconnect([string]$para1){
if($PSScriptRoot.length -eq 0){
$scriptRoot="C:\testing_AI\modules"
}
else{
$scriptRoot=$PSScriptRoot
}
if($para1.Length -eq 0){
$testVpn = Get-VpnConnection -ErrorAction SilentlyContinue
if($testVpn){
$vpnName = $testVpn[0].Name
$vpnServerAddress = $testVpn[0].ServerAddress
$username = "vpn" # 使用者名稱
$password = "vpn" | ConvertTo-SecureString -AsPlainText -Force # 密碼
# 將密碼轉換為明文字串
$passwordString = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
#Start-Process -NoNewWindow -FilePath "rasdial.exe" -ArgumentList "$vpnName $username $passwordString"
$command = "rasdial.exe $vpnName $username $passwordString"
$connectinfo = Invoke-Expression -Command $command
Start-Sleep -Seconds 30
$connectionStatus = (Get-VpnConnection -Name $vpnName).ConnectionStatus
if ($connectionStatus -eq "Connected") {
Write-Host "VPN Connect Successfully"
$results="OK"
}else{
Write-Host "VPN Connect failed , delete this VPN"
$results="NG"
Remove-VpnConnection -Name $vpnName -Force
}
}
else{
$VPNlist = Invoke-WebRequest -Uri "http://www.vpngate.net/api/iphone/" -UseBasicParsing | ConvertTo-Csv
#$pattern = "public-vpn-\d+,\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
$pattern = "public-vpn-\d+,\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3},\d+,\d+,\d+,[A-Za-z]+,[A-Za-z]+"
$matches = [regex]::Matches($VPNlist, $pattern)
$JPlist = @()
foreach ($match in $matches) {
$match.Value
$JPlist += $match.Value.Split(",")[0]
}
$i = 0
do{
$outs = "Public VPN TestConnect List -" + ($i + 1) + "/" + ($JPlist.Count)
Write-Output $outs
#VPN Connect
$vpnName = "VPNtest-Japan"
$vpnServerAddress = $JPlist[$i].ToString() + ".opengw.net"
Add-VpnConnection -Name $vpnName -ServerAddress $vpnServerAddress -PassThru
$username = "vpn" # 使用者名稱
$password = "vpn" | ConvertTo-SecureString -AsPlainText -Force # 密碼
# 將密碼轉換為明文字串
$passwordString = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
# 使用 rasdial 命令進行 VPN 連線
#$connectinfo = Start-Process -NoNewWindow -FilePath "rasdial.exe" -ArgumentList "$vpnName $username $passwordString"
$command = "rasdial.exe $vpnName $username $passwordString"
$connectinfo = Invoke-Expression -Command $command
# 等待連線建立
Start-Sleep -Seconds 30
$connectionStatus = (Get-VpnConnection -Name $vpnName).ConnectionStatus
if ($connectionStatus -eq "Connected") {
Write-Host "VPN Connect Successfully"
$results="OK"
break
}else{
Write-Host "VPN Connect failed , delete this VPN"
$results="NG"
Remove-VpnConnection -Name $vpnName -Force
}
$i++
}until($i -ge $JPlist.Count)
}
$tcpath=(Split-Path -Parent $scriptRoot)+"\currentjob\TC.txt"
$tcnumber=((get-content $tcpath).split(","))[0]
$tcstep=((get-content $tcpath).split(","))[1]
$action="VPNconnect"
$index="Last test VPN's DDNS Name : $vpnServerAddress"
######### write log #######
Get-Module -name "outlog"|remove-module
$mdpath=(Get-ChildItem -path "C:\testing_AI\modules\" -r -file |Where-object{$_.name -match "outlog" -and $_.name -match "psm1"}).fullname
Import-Module $mdpath -WarningAction SilentlyContinue -Global
#write-host "Do $action!"
outlog $action $results $tcnumber $tcstep $index
}
else{
$vpnName = "VPNtest-Japan"
$vpnServerAddress = (Get-VpnConnection -Name $vpnName).ServerAddress
rasdial.exe $vpnName /disconnect
Remove-VpnConnection -Name $vpnName -Force
$testvpn = Get-VpnConnection -Name $vpnName -ErrorAction SilentlyContinue
if($testvpn){
$results = "NG"
}else{
$results = "OK"
}
$tcpath=(Split-Path -Parent $scriptRoot)+"\currentjob\TC.txt"
$tcnumber=((get-content $tcpath).split(","))[0]
$tcstep=((get-content $tcpath).split(","))[1]
$action="VPNconnect"
$index="Last test VPN's DDNS Name : "
######### write log #######
Get-Module -name "outlog"|remove-module
$mdpath=(Get-ChildItem -path "C:\testing_AI\modules\" -r -file |Where-object{$_.name -match "outlog" -and $_.name -match "psm1"}).fullname
Import-Module $mdpath -WarningAction SilentlyContinue -Global
#write-host "Do $action!"
outlog $action $results $tcnumber $tcstep $index
}
$output = ""
$output += "This is Public VPN Info:`n"
$output += " Vpn Name:$($vpnName)`n"
$output += " VpnServerAddress:$($vpnServerAddress)`n"
$output += " Username:$($username)`n"
$output += " Password:$($passwordString)`n"
$output += "Run rasdial.exe with powershell to connect VPN`n"
$output += "The following is the command message:`n"
$output += "rasdial.exe $vpnName $username $passwordString`n"
$output += "Add-VpnConnection -Name $vpnName -ServerAddress $vpnServerAddress -PassThru`n"
foreach($item in $connectinfo){
$output += "$($item)`n"
}
$output += "$($action)_$($index)"
$datenow=get-date -format "yyMMdd_HHmmss"
$output | Out-File -FilePath "C:\testing_AI\logs\$tcnumber\$($datenow)_step$($tcstep)_VPNconnectInfo.txt" -Encoding unicode
}
export-modulemember -Function VPNconnect