1 $appname = "OCS Inventory NG Agent"
2 $path = "C:\OCSUpdateTemp"
3 $logFile = "$path\update.log"
4 
5 $date = (Get-date).ToString("dd/MM/yyyy HH:mm")
6 Add-Content $logFile -value "$date - Execute removeupdateagent.ps1"
7 $date = (Get-date).ToString("dd/MM/yyyy HH:mm")
8 Add-Content $logFile -value "$date - Stop OCS Inventory Service before uninstall"
9 
10 # Stop OCS Inventory Service before uninstall
11 Stop-Service -Name "OCS Inventory Service"
12 
13 $date = (Get-date).ToString("dd/MM/yyyy HH:mm")
14 Add-Content $logFile -value "$date - Stop OCS Inventory Service OK"
15 
16 # Get registry uninstalling path
17 $32bit = get-itemproperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' | Select-Object DisplayName, DisplayVersion, UninstallString, PSChildName | Where-Object { $_.DisplayName -match "^*$appname*"}
18 $64bit = get-itemproperty 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' | Select-Object DisplayName, DisplayVersion, UninstallString, PSChildName | Where-Object { $_.DisplayName -match "^*$appname*"}
19 
20 # Test if 32 or 64bits installation
21 if ($64bit -eq "" -or $64bit.count -eq 0) {
22 	if ($32bit.UninstallString) {
23 		$uninst = $32bit.UninstallString
24 		$date = (Get-date).ToString("dd/MM/yyyy HH:mm")
25 		Add-Content $logFile -value  + "$date - Uninstall old agent is in progress..."
26         cmd.exe /c $uninst /S
27 	}
28 }
29 else {
30 	if ($64bit.UninstallString) {
31 		$uninst = $64bit.UninstallString
32 		$date = (Get-date).ToString("dd/MM/yyyy HH:mm")
33 		Add-Content $logFile -value "$date - Uninstall old agent is in progress..."
34 		cmd.exe /c $uninst /S
35 	}
36 }
37 
38 # Unregister old ocs inventory scheduled task if exists
39 schtasks /delete /tn "OCS Inventory Agent Update" /f
40 
41 $date = (Get-date).ToString("dd/MM/yyyy HH:mm")
42 Add-Content $logFile -value "$date - Scheduled task OCS Inventory Agent Update to launch the packager file"
43 
44 # Register new ocs inventory scheduled task
45 $hour = (Get-Date).AddMinutes(2).ToString("HH:mm")
46 $result = schtasks /create /tn "OCS Inventory Agent Update" /tr "cmd.exe /c $path\ocsupdate.exe" /sc once /st $hour /ru System
47 
48 if(!$result) {
49 	$date = (Get-date).ToString("dd/MM/yyyy HH:mm")
50 	Add-Content $logFile -value "$date - Scheduled task NOT OK"
51 	Add-Content $logFile -value "END LOG"
52 	[Environment]::Exit(1)
53 }
54 
55 $date = (Get-date).ToString("dd/MM/yyyy HH:mm")
56 Add-Content $logFile -value "$date - Scheduled task OK. The new agent will be installed in 2 minutes"
57 Add-Content $logFile -value "END LOG"
58 [Environment]::Exit(0)