-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver_uninstall.psm1
132 lines (91 loc) · 3.53 KB
/
driver_uninstall.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
function driver_uninstall ([string]$para1,[string]$para2){
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force;
$wshell=New-Object -ComObject wscript.shell
$shell=New-Object -ComObject shell.application
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Windows.Forms,System.Drawing
$pkgename=$para1
$nonlog_flag=$para2
if($PSScriptRoot.length -eq 0){
$scriptRoot="C:\testing_AI\modules"
}
else{
$scriptRoot=$PSScriptRoot
}
$tcpath=(Split-Path -Parent $scriptRoot)+"\currentjob\TC.txt"
$tcnumber=((get-content $tcpath).split(","))[0]
$tcstep=((get-content $tcpath).split(","))[1]
$action=((get-content $tcpath).split(","))[2]
$picpath=(Split-Path -Parent $scriptRoot)+"\logs\$($tcnumber)\"
if(-not(test-path $picpath)){new-item -ItemType directory -path $picpath |out-null}
$unresults=@()
$packages = Get-Package |Where-object{$_.name -like "*$pkgename*"}
if($packages.count -ne 0){
do{
$packages = Get-Package |Where-object{$_.name -like "*$pkgename*"}
foreach($package in $packages){
$packagename=$package.Name
start-sleep -s 30
<#
#region method 1
$argList = foreach ($i in (0..($package.Meta.Attributes.Keys.Count - 1))) {
if ($package.Meta.Attributes.Keys[$i] -eq 'UninstallString') {
$package.Meta.Attributes.Values[$i]
}
}
# return $Command
Invoke-Expression "& $Command "
#endregion
#>
#region method 2
#Get Uninstall String from Registry
$us = Get-childItem -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" -ErrorAction SilentlyContinue | Get-ItemProperty | Where-Object {$_.DisplayName -like $packagename} | select DisplayName, UninstallString
if($us.DisplayName.count -ne 0){
# Splitting the Uninstall String into executable and argument list
$unused, $filePath, $argList = $us.UninstallString -split '"', 3
# Any of the following command can start the process
$argList += ' -silent', '-deviceinitiated'
Start-Process -FilePath "C:\Windows\SysWOW64\RunDll32.EXE" -ArgumentList $argList -Wait
$counttime=0
do{
start-sleep -s 10
$checkuninstall=(Get-Package |Where-object{$_.name -eq $packagename}).count
$counttime++
}
until ($checkuninstall -eq 0 -or $counttime -gt 10)
$unresult="$packagename uninstall done"
if($counttime -gt 10){$index="$packagename uninstall fail over 100sec"}
$unresults=$unresults+@($unresult)
}
}
}until($packages.count -eq 0)
$results="OK"
$timenow=get-date -format "yyMMdd_HHmmss"
$uninstalllog=$picpath+"$($timenow)_step$($tcstep)_$($pkgename)_uninstall_pass.txt"
if($unresults -match "fail"){
$results="NG"
$uninstalllog=$picpath+"$($timenow)_step$($tcstep)_$($pkgename)_uninstall_fail.txt"
}
new-item $uninstalllog -Force|out-null
$unresults=$unresults|Out-String
add-content -path $uninstalllog -value $unresults
}
else{
$timenow=get-date -format "yyMMdd_HHmmss"
$uninstalllog=$picpath+"$($timenow)_step$($tcstep)_$($pkgename)_uninstall_nofound.txt"
$results="NG"
add-content -path $uninstalllog -value "$($pkgename) no found from get-package"
}
$index="check logs"
######### 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 driver_uninstall