-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyingfiles.psm1
173 lines (136 loc) · 4.94 KB
/
copyingfiles.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
function copyingfiles ([string]$para1,[string]$para2,[string]$para3){
#import
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force;
$shell=New-Object -ComObject shell.application
$wshell=New-Object -ComObject wscript.shell
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
$filepath=$para1
$copytopath=$para2
$nonlog_flag=$para3
$action="copying files to logs folder from $filepath "
function netdisk_connect([string]$webpath,[string]$username,[string]$passwd,[string]$diskid){
net use $webpath /delete
net use $webpath /user:$username $passwd /PERSISTENT:yes
net use $webpath /SAVECRED
if($diskid.length -ne 0){
$diskpath=$diskid+":"
$checkdisk=net use
if($checkdisk -match $diskpath){net use $diskpath /delete}
net use $diskpath $webpath
}
}
if($PSScriptRoot.length -eq 0){
$scriptRoot="C:\testing_AI\modules"
}
else{
$scriptRoot=$PSScriptRoot
}
if($filepath -match "192.168.2.249"){
$checkstart=Get-Date
do{
start-sleep -s 2
$testpin= ping 192.168.2.249 /n 3
$checkpassed=(New-TimeSpan -start $checkstart -end (get-date)).TotalSeconds
}until( !($testpin -match "unreachable" -or $testpin -match "Request timed out" -or $testpin -match "failed") -or $checkpassed -gt 60)
if($checkpassed -le 60){
netdisk_connect -webpath \\192.168.2.249\srvprj\Inventec\Dell -username pctest -passwd pctest -diskid Y
$filepath= $filepath.replace("\\192.168.2.249\srvprj\Inventec\Dell","Y:")
}
if($checkpassed -gt 60){
$results="NG"
$index="netconnect failed"
}
}
if($results -ne "NG"){
$waitc=0
do{
start-sleep -s 2
$checkfilelink=(test-path $filepath)
$waitc++
}until($checkfilelink -or $waitc -gt 30)
if ($waitc -le 30 -and $checkfilelink){
$tcpath=(Split-Path -Parent $scriptRoot)+"\currentjob\TC.txt"
$tcnumber=((get-content $tcpath).split(","))[0]
$tcstep=((get-content $tcpath).split(","))[1]
$logpath=(Split-Path -Parent $scriptRoot)+"\logs\$($tcnumber)"
if(-not(test-path $logpath)){new-item -ItemType directory -path $logpath -Force|Out-Null}
$type_folder=$false
if($copytopath.Length -eq 0){
$copytopath=(Split-Path -Parent $scriptRoot)+"\logs\$($tcnumber)\copydata\"
if(-not(test-path $copytopath)){new-item -ItemType directory -path $copytopath -Force|Out-Null}
}
if($filepath.length -ne 0 ){
$filename=($filepath.split("\\"))[-1]
## if copy a folder files
if($filepath.Substring($filepath.Length-1,1) -match "\\"){
$type_folder=$true
$filepath= $filepath+"*"
}
$copyfroms=Get-ChildItem $filepath -file -Recurse
if($copytopath -eq "C:\" -or $copytopath -eq "C:\Windows\System32\"){
$copyfile0=(Get-ChildItem $copytopath\*).count
}
else{
$copyfile0=(Get-ChildItem $copytopath\* -r ).count
}
copy-Item -Path $filepath -Destination $copytopath -Recurse -Exclude "System Volume Information" -Force
if($copytopath -eq "C:\" -or $copytopath -eq "C:\Windows\System32\"){
$copyfroms=Get-ChildItem $filepath -file
}
else{
$copyfroms=Get-ChildItem $filepath -file -Recurse
}
if($copytopath -eq "C:\" -or $copytopath -eq "C:\Windows\System32\"){
$copyfile1=(Get-ChildItem $copytopath\*).count
}
else{
$copyfile1=(Get-ChildItem $copytopath\* -r).count
}
$copyfilecount=$copyfile1-$copyfile0
write-host "Folder files $($copyfilecount) files copied"
### check info ##
$runaction="cmdline"
Get-Module -name $runaction|remove-module
$mdpath=(Get-ChildItem -path "C:\testing_AI\modules\" -r -file |Where-object{$_.name -match "^$runaction\b" -and $_.name -match "psm1"}).fullname
Import-Module $mdpath -WarningAction SilentlyContinue -Global
if($copytopath -eq "C:\" -or $copytopath -eq "C:\Windows\System32\" -or $type_folder -eq $false){
&$runaction -para1 "dir $filename" -para2 "$copytopath" -para3 "cmd" -para5 "nolog"
}
else{
&$runaction -para1 "dir /s" -para2 "$copytopath" -para3 "cmd" -para5 "nolog"
}
$results="OK"
foreach($copyfrom in $copyfroms){
if($copytopath -eq "C:\" -or $copytopath -eq "C:\Windows\System32\" ){
$size=(Get-ChildItem "$copytopath\*" -file |Where-object{$_.name -eq $copyfrom.name}).Length
}
else{
$size=(Get-ChildItem "$copytopath\*" -file -Recurse |Where-object{$_.name -eq $copyfrom.name}).Length
}
if($size -ne $copyfrom.Length){
$results="NG"
}
}
$index="check $copytopath and cmd logs"
}
else{
$results="NG"
$index="no define copy from path"
}
}
else{
$results="NG"
$index="no defined path is found after waiting 60s"
}
}
######### write log #######
if( $nonlog_flag.Length -eq 0){
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 copyingfiles