/
VC-Device-Add.ps1
88 lines (74 loc) · 2.24 KB
/
VC-Device-Add.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
[CmdletBinding()]
param (
[Parameter(mandatory=$True)]
[string]
$DeviceName
)
$already = "false"
$tokeAddress = $null
$start = (Get-Date)
do {
$findRawTxt = VC-Pipe "LIST" | Select-String "\s+\-\-\>\s+" | findstr "$DeviceName"
if ($null -eq $findRawTxt) {
throw [System.Exception]::new("Not Found $DeviceName")
}
else {
if ($findRawTxt.IndexOf('(In-use by you)') -ne -1) {
Write-Host "Device Already!"
}
elseif ($findRawTxt.IndexOf('(In-use by:') -ne -1) {
$runningMillis = (Get-Date).Subtract($start).TotalMilliseconds
if ($runningMillis -gt 120000) {
break
}
Write-Host "Device Other Host Used Sleep! $runningMillis"
Start-Sleep -Seconds 5
continue
}
$m = "$findRawTxt" | Select-String -Pattern "\s+\-\-\>\s+.+?\s+\((?<TokenAddress>.+)\)"
if ($m.Matches.Success) {
$tokeAddress = $m.Matches[0].Groups['TokenAddress'].Value
if ($findRawTxt.IndexOf('(In-use by you)') -ne -1) {
$already = "true"
}
break
}
else {
throw [System.Exception]::new("Regex Match Failed $DeviceName -> [$findRawTxt]")
}
}
} while ($null -eq $tokeAddress);
if ($null -eq $tokeAddress) {
throw [System.Exception]::new("While breaked. Not Found $DeviceName")
}
Start-Sleep -Seconds 1
$BakErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Continue"
$i = 0
do {
if ($i -ne 0) {
# 재시도
Start-Sleep -Seconds 1
Write-Host "Repeat Count : $i"
}
try {
$ret = VC-Pipe "USE,$tokeAddress"
if ($ret -eq 'OK') {
Write-Host "Device Added 1: $DeviceName - $tokeAddress"
return @($True, $tokeAddress, $already)
}
}
catch {
# 실패했다고 했지만 List를 조회 해보면 이미 연결 처리가 됨
$findRawTxt = VC-Pipe "LIST" | Select-String "\s+\-\-\>\s+" | findstr "$DeviceName"
if ($null -ne $findRawTxt -Or $findRawTxt.IndexOf('(In-use by you)') -ne -1) {
Write-Host "Device Added 2: $DeviceName - $tokeAddress"
return @($True, $tokeAddress, $already)
}
$ErrorActionPreference = $BakErrorActionPreference
throw
}
$i++
} while ($i -le 2)
$ErrorActionPreference = $BakErrorActionPreference
throw [System.Exception]::new("Device Not Added. $ret")