forked from ms-iot/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppInstall.bat
More file actions
223 lines (199 loc) · 6.32 KB
/
AppInstall.bat
File metadata and controls
223 lines (199 loc) · 6.32 KB
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
@echo off
::
:: Ensure we execute batch script from the folder that contains the batch script
::
pushd %~dp0
SETLOCAL
:: ---------------------------------------------------------------------
:: Variable setup
::
:: Example:
:: set defaultappx=defappxname (Name only. No need for .appx extension)
:: set certslist=cert1name cert2name (Name only. No need for .cer extension. You can delimit mutliple certificates with a space.)
:: ---------------------------------------------------------------------
set defaultappx=MainAppx_1.0.0.0_Win32_Debug
set certslist=MainAppx_1.0.0.0_Win32_Debug
::set defaultappx=MainAppx_1.0.2.0_Win32_Debug
::set certslist=MainAppx_1.0.2.0_Win32_Debug
::
:: Get Appx Family Name
::
mindeployappx /FetchPackageFamilyName /packagepath:%~dp0%defaultappx%.appx > %temp%\appxfamname.txt
set /p fullappxfamilyname=<%temp%\appxfamname.txt
set appxfamilyname=%fullappxfamilyname:~19,-1%
echo Appx Family Name: %appxfamilyname%
::
:: Get Appx Version
::
mindeployappx /FetchPackageVersion /packagepath:%~dp0%defaultappx%.appx > %temp%\appxversion.txt
set /p appxversion=<%temp%\appxversion.txt
echo Appx Version: %appxversion%
::
:: Get Installed Appx Version
::
mindeployappx /FetchPackageVersion /packagefamilyname:%appxfamilyname% > %temp%\installedversion.txt
set /p installedversion=<%temp%\installedversion.txt
echo Installed Appx Version: %installedversion%
::
:: If Appx with same Family Name isn't already installed, install it
::
set installtype=Add
If NOT "%installedversion%"=="%installedversion:ReturnCode=%" (
echo No appx with Family Name %appxfamilyname% is currently installed. Appx can be installed.
goto SUB_DEPLOYAPPX
)
::
:: If Appx with same Family Name is already installed, compare Appx version and Installed Appx version
:: Install if Appx version > Installed Appx version
::
echo Comparing Appx Version and Installed Appx Version.
set installtype=Update
call :COMPARE_VERSIONS %appxversion% %installedversion%
if %errorlevel% == 1 (
echo Appx version greater than Installed Appx Version. Appx can be installed.
goto SUB_DEPLOYAPPX
)
::
:: If Appx version =< Installed Appx version, exit
echo Appx version is less than or equal to the Installed Appx Version. Appx cannot be installed.
echo Exiting.
goto CLEANUP
:: -------------------------------------------------------------------------------
::
:: SUB_DEPLOYAPPX
::
:: Installs certificate and deploys specified appx package. Exits on failure
::
:: -------------------------------------------------------------------------------
:SUB_DEPLOYAPPX
::
:: Add AllowAllTrustedApps Reg Key
::
echo Adding AllowAllTrustedApps Reg Key.
reg add "HKLM\Software\Policies\Microsoft\Windows\Appx" /v AllowAllTrustedApps /t REG_DWORD /d 1 /f
if %errorlevel% == 0 (
echo Successfuly added AllowAllTrustedApps to registry.
) else (
echo Failed to add AllowAllTrustedApps to registry: %errorlevel%
echo ErrorCode: %errorlevel%
goto CLEANUP
)
::
:: Instal Certificates
::
(for %%i in (%certslist%) do (
echo Installing %%i Certificate
certmgr.exe -add .\%%i.cer -r localMachine -s root > %temp%\%%i_cer_result.txt
if %errorlevel% == 0 (
echo Successfuly installed %%i Certificate.
) else (
echo Failed to install %%i Certificate.
echo ErrorCode: %errorlevel%
goto CLEANUP
)))
::
:: Create Scheduled Task to Deploy Appx
::
set taskname=DeployAppxTask
echo Creating Scheduled Task "%taskname%" for Appx Installation.
del /Q %systemdrive%\data\users\defaultaccount\appdata\local\temp\%taskname%_deploy_done.txt 2> nul:
del /Q %systemdrive%\data\users\defaultaccount\appdata\local\temp\%defaultappx%_result.txt 2> nul:
schtasks /create /f /tn "%taskname%" /ru DefaultAccount /sc ONSTART /tr "%~dp0deployappx.bat %taskname% %installtype%"
if %errorlevel% == 0 (
echo Successfuly Created Scheduled Task "%taskname%".
) else (
echo Failed to Create Scheduled Task "%taskname%"
echo ErrorCode: %errorlevel%
goto CLEANUP
)
::
:: Run Schedued Task
::
echo Running Scheduled Task "%taskname%"
schtasks /run /tn %taskname%
if %errorlevel% == 0 (
echo Successfuly Started Scheduled Task "%taskname%".
) else (
echo Failed to Start Scheduled Task "%taskname%"
echo ErrorCode: %errorlevel%
goto CLEANUP
)
::
:: Check if Sched Task Ran
::
setlocal
set ITER=0
:CHECK_FOR_FILE
if NOT EXIST %systemdrive%\data\users\defaultaccount\appdata\local\temp\%taskname%_deploy_done.txt (
set /A ITER=ITER+1
if "%ITER%" == "50" (
echo Deployment of %taskname% Task Timedout
goto CLEANUP
)
ping -n 4 localhost > nul:
goto CHECK_FOR_FILE
)
endlocal
findstr /B /L "ReturnCode:[0x0]" %systemdrive%\data\users\defaultaccount\appdata\local\temp\%defaultappx%_result.txt
if %errorlevel% == 0 (
echo Successfuly Deployed %defaultappx%.
) else (
echo Failed to Deploy %defaultappx%
echo ErrorCode: %errorlevel%
goto CLEANUP
)
echo.
goto CLEANUP
setlocal
:: -------------------------------------------------------------------------------
::
:: COMPARE_VERSIONS
::
:: Compares two version numbers and returns the result in the ERRORLEVEL
::
:: Returns 1 if version1 > version2
:: 0 if version1 = version2
:: -1 if version1 < version2
::
:: The nodes must be delimited by . or , or -
::
:: Nodes are normally strictly numeric, without a 0 prefix. A letter suffix
:: is treated as a separate node
::
:: -------------------------------------------------------------------------------
:COMPARE_VERSIONS version1 version2
setlocal enableDelayedExpansion
set "v1=%~1"
set "v2=%~2"
call :DIVIDE_LETTERS v1
call :DIVIDE_LETTERS v2
:loop
call :PARSE_NODE "%v1%" n1 v1
call :PARSE_NODE "%v2%" n2 v2
if %n1% gtr %n2% exit /b 1
if %n1% lss %n2% exit /b -1
if not defined v1 if not defined v2 exit /b 0
if not defined v1 exit /b -1
if not defined v2 exit /b 1
goto :loop
:PARSE_NODE version nodeVar remainderVar
for /f "tokens=1* delims=.,-" %%A in ("%~1") do (
set "%~2=%%A"
set "%~3=%%B"
)
exit /b
:DIVIDE_LETTERS versionVar
for %%C in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do set "%~1=!%~1:%%C=.%%C!"
exit /b
:: -------------------------------------------------------------------------------
::
:: CLEANUP
::
:: -------------------------------------------------------------------------------
:CLEANUP
echo Cleaning Up.
schtasks /delete /f /tn %taskname% 2> nul:
echo Exiting.
popd
ENDLOCAL
exit /b