-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns_settings.psm1
114 lines (79 loc) · 2.98 KB
/
dns_settings.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
function dns_settings ([string]$para1,[string]$para2){
$paracheck1=$PSBoundParameters.ContainsKey('para1')
if( $paracheck1 -eq $false -or $para1.length -eq 0 ){
$para1=""
}
if($PSScriptRoot.length -eq 0){
$scriptRoot="C:\testing_AI\modules"
}
else{
$scriptRoot=$PSScriptRoot
}
$action="DNSsettings"
$tcpath=(Split-Path -Parent $scriptRoot)+"\currentjob\TC.txt"
$tcnumber=((get-content $tcpath).split(","))[0]
$tcstep=((get-content $tcpath).split(","))[1]
$picpath=(Split-Path -Parent $scriptRoot)+"\logs\$($tcnumber)\"
if(-not(test-path $picpath)){new-item -ItemType directory -path $picpath |out-null}
$dnsip=$para1
$nonlogflag=$para2
$getinfo=ipconfig
$checkline=$getinfo -match "allion.test"
if($checkline){
$linenu=$getinfo.IndexOf($checkline)
($getinfo|Select-Object -Skip $linenu|Select-Object -First 3|Select-Object -last 1) -match "\d{1,}\.\d{1,}\.\d{1,}\.\d{1,}" |Out-Null
$ipout=$matches[0]
Get-NetIPAddress|ForEach-Object{
if($_.IPAddress -match $ipout -and $_.AddressFamily -eq "IPv4"){
$adtname=$_.InterfaceAlias
}
}
$timennow=get-date -Format "yyMMdd_HHmmss"
$dnsconfigtxt=(Split-Path -Parent $scriptRoot)+"\logs\$($tcnumber)\$($timennow)_step$($tcstep)_dnsconfig_before.txt"
Get-DnsClientServerAddress -InterfaceAlias $adtname|Where-object{$_.AddressFamily -eq "2"}|Out-String|set-content $dnsconfigtxt
if($dnsip.Length -gt 2){
## change dns
Set-DnsClientServerAddress -InterfaceAlias $adtname -ServerAddresses $dnsip
Clear-DnsClientCache
start-sleep -s 30
$timennow=get-date -Format "yyMMdd_HHmmss"
$dnsconfigtxt=(Split-Path -Parent $scriptRoot)+"\logs\$($tcnumber)\$($timennow)_step$($tcstep)_dnsconfig_after.txt"
Get-DnsClientServerAddress -InterfaceAlias $adtname|Where-object{$_.AddressFamily -eq "2"}|Out-String|set-content $dnsconfigtxt
$results="NG"
$index="DNS changed to $dnsip fail"
if((get-content $dnsconfigtxt) -match $dnsip){
$results="OK"
$index="DNS changed to $dnsip"
}
}
else{
## rollback dns settins
Set-DnsClientServerAddress -InterfaceAlias $adtname -ResetServerAddresses
Clear-DnsClientCache
start-sleep -s 30
$timennow=get-date -Format "yyMMdd_HHmmss"
$dnsconfigtxt=(Split-Path -Parent $scriptRoot)+"\logs\$($tcnumber)\$($timennow)_step$($tcstep)_dnsconfig_after.txt"
Get-DnsClientServerAddress -InterfaceAlias $adtname|Where-object{$_.AddressFamily -eq "2"}|Out-String|set-content $dnsconfigtxt
$results="NG"
$index="DNS changed to original settings fail"
if((get-content $dnsconfigtxt) -match "168"){
$results="OK"
$index="DNS changed to original settings"
}
}
## check action result ##
}
else{
$results="-"
$index="no allion.test connected"
}
if($nonlogflag.length -eq 0){
$index="ipconfig_result.txt"
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
}
}
export-modulemember -Function dns_settings