-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMsAppInstall.psm1
268 lines (192 loc) · 7.82 KB
/
MsAppInstall.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
Function MsAppInstall([string]$para1,[string]$para2){
Add-Type -AssemblyName System.Windows.Forms
#Start-Process ms-windows-store://search/?query=AV1%20Video%20Extension%20App
$nonlog_flag=$para2
if($PSScriptRoot.length -eq 0){
$scriptRoot="C:\testing_AI\modules\"
}else{
$scriptRoot=$PSScriptRoot
}
$action="MSAppInstall_$($para1)"
$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}
$actionss ="screenshot"
Get-Module -name $actionss|remove-module
$mdpath=(Get-ChildItem -path $scriptRoot -r -file |Where-object{$_.name -match "^$actionss\b" -and $_.name -match "psm1"}).fullname
Import-Module $mdpath -WarningAction SilentlyContinue -Global
#Find AppID
#region old method
<#selenium
$actionse ="selenium_prepare"
Get-Module -name $actionse|remove-module
$mdpath=(Get-ChildItem -path $scriptRoot -r -file |Where-object{$_.name -match "^$actionse\b" -and $_.name -match "psm1"}).fullname
Import-Module $mdpath -WarningAction SilentlyContinue -Global
&$actionse -para1 "edge" -para2 "nonlog"
Get-ChildItem "C:\testing_AI\modules\selenium\WebDriver.dll" |Unblock-File
Add-Type -Path "C:\testing_AI\modules\selenium\WebDriver.dll"
$driver = New-Object OpenQA.Selenium.Edge.EdgeDriver
$driver.Manage().Window.Maximize()
$driver.Navigate().GoToUrl("https://www.microsoft.com/en-us")
Start-Sleep -Seconds 60
[System.Windows.Forms.SendKeys]::SendWait("{ESC}")
$jscommand = "var searchb = document.getElementById('search');"
$jscommand += "searchb.click()"
$driver.ExecuteScript($jscommand)
Set-Clipboard -value $para1
Start-Sleep -Seconds 5
[System.Windows.Forms.SendKeys]::SendWait("^v")
#$jscommand = "var searchtext = document.getElementById('cli_shellHeaderSearchInput');"
#$jscommand += "searchtext.value = '" + $para1 +"';"
#$jscommand += "searchtext.click();"
#$driver.ExecuteScript($jscommand)
#[System.Windows.Forms.SendKeys]::SendWait($para1)
#------
$keyword = "AV1"
do{
Write-Host "waitting to get MSApp id"
Start-Sleep -Seconds 10
$webtext = $driver.PageSource
$pattern = 'href="(.*?)"'
$matches = [regex]::Matches($webtext, $pattern)
$urllist = @()
if ($matches.Count -gt 0) {
foreach ($match in $matches) {
$urllist += $match.Groups[1].Value
}
}
else {
Write-Host "not find href"
}
$urljudge = $para1.Replace(" ","-")
foreach($list in $urllist){
if($list -match "/$urljudge/"){
$Appid = $list.Split("/")[$list.Split("/").length-1]
}
}
} until ($Appid)
Write-Host "get MSApp id $Appid"
# if ($webtext -match $keyword) {
# $index = $webtext.IndexOf($keyword)
# $result = $webtext.Substring($index + $keyword.Length)
# Write-Host "find: $result"
# }
# else {
# Write-Host "not find"
# }
#------
#Start-Sleep -Seconds 10
#[System.Windows.Forms.SendKeys]::SendWait("{DOWN}")
#Start-Sleep -Seconds 10
#[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
#Start-Sleep -Seconds 10
#$currenturl = $driver.Url
#$Appid = [regex]::Match($currenturl, "/([A-Za-z0-9]+)\?").Groups[1].Value
$driver.Close()
$driver.Dispose()
#selenium#>
#endregion old method
### start wu ###
$actionmd="enable_wu"
Get-Module -name $actionmd|remove-module
$mdpath=(Get-ChildItem -path $scriptRoot\* -r -file |Where-object{$_.name -match "^$actionmd\b" -and $_.name -match "psm1"}).fullname
Import-Module $mdpath -WarningAction SilentlyContinue -Global
&$actionmd -para1 nonlog
### search app ###
$srch= winget search $para1 --source=msstore --accept-source-agreements
$Appid=$srch[-1].Replace($para1,"").split("")[1]
### install app ###
$installmsapp=`
winget install `
--id $Appid `
--accept-package-agreements `
--accept-source-agreements `
--silent
$results=$installmsapp[-1]
#region old method
<### open msstore ## check if fail open
Start-Process ms-windows-store://pdp/?ProductId=$Appid
Start-Sleep -Seconds 25
[System.Windows.Forms.SendKeys]::SendWait("{tab}")
Start-Sleep -Seconds 2
[System.Windows.Forms.SendKeys]::SendWait("+{tab}")
Start-Sleep -Seconds 2
[System.Windows.Forms.SendKeys]::SendWait(" ")
Start-Sleep -Seconds 10
[System.Windows.Forms.SendKeys]::SendWait("%{F4}") ## close once for fail open login window
Start-Sleep -Seconds 2
if((Get-Process -Name "*WinStore*") -eq $null){
Start-Process ms-windows-store://pdp/?ProductId=$Appid
Start-Sleep -Seconds 25
[System.Windows.Forms.SendKeys]::SendWait("{tab}")
Start-Sleep -Seconds 2
[System.Windows.Forms.SendKeys]::SendWait("+{tab}")
Start-Sleep -Seconds 2
[System.Windows.Forms.SendKeys]::SendWait(" ")
}
$judge = "*" + $para1.Replace(" ","") + "*"
$runbefore = Get-Date
# $running = Get-Date
$flag = 0
do{
echo "Waiting for app installation to complete..."
Start-Sleep -Seconds 20
$running = Get-Date
if(($running - $runbefore).TotalMinutes -gt 5){
$runbefore = Get-Date
#$running = Get-Date
$flag += 1
if($flag -gt 3){
break
echo "App installation fail 3 times."
}
else{
Stop-Process -Name "*WinStore*"
Start-Sleep -Seconds 5
Start-Process ms-windows-store://pdp/?ProductId=$Appid
Start-Sleep -Seconds 25
[System.Windows.Forms.SendKeys]::SendWait("{tab}")
Start-Sleep -Seconds 2
[System.Windows.Forms.SendKeys]::SendWait("+{tab}")
Start-Sleep -Seconds 2
[System.Windows.Forms.SendKeys]::SendWait(" ")
}
}
}until(Get-AppxPackage -Name $judge -ErrorAction SilentlyContinue)
##screenshot##
&$actionss -para3 nonlog -para5 $para1
if(Get-AppxPackage -Name $judge){
echo "App installation has completed."
$results="OK"
$appinfo = Get-AppxPackage -Name $judge
$infolog = @()
$infolog += "Successful Install App"
$infolog += "AppVersion:" + $appinfo.Version
$infolog += "PackageFullName:" + $appinfo.PackageFullName
Set-Content -Path "$picpath\$($para1).txt" -Value $infolog
}else{
$results="Instsll Failed"
}
$Index="check windowsupdate & appinstall"
Stop-Process -Name WinStore.App
###>
#endregion
### disable wu ###
$actionmd="disable_wu"
Get-Module -name $actionmd|remove-module
$mdpath=(Get-ChildItem -path $scriptRoot\* -r -file |Where-object{$_.name -match "^$actionmd\b" -and $_.name -match "psm1"}).fullname
Import-Module $mdpath -WarningAction SilentlyContinue -Global
&$actionmd -para1 nonlog
### save logs ##
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
}
}
# 匯出模絁E�E�E�E�E�E�E�E�E�E�E�E�E�E�E�E�E員
Export-ModuleMember -Function MsAppInstall