1 # Move ocsupdate.exe on Temp file 2 $location = Get-Location 3 $tempFolder = "OCSUpdateTemp" 4 $path = "C:\$tempFolder" 5 $logFile = "$path\update.log" 6 7 #First remove folder if exists 8 Remove-Item "$path" -Force -Recurse 9 10 # Create new folder to move OCS script and OCS exe 11 New-Item -Path "C:\" -Name "$tempFolder" -ItemType "directory" 12 #Create log file 13 New-Item -Path "$path" -Name "update.log" -ItemType "file" 14 Move-Item "$location\*.exe" -Destination "$path\ocsupdate.exe" 15 Move-Item "$location\removeupdateagent.ps1" -Destination "$path\removeupdateagent.ps1" 16 17 Add-Content $logFile -value "UPDATE AGENT LOG FILE" 18 $date = (Get-date).ToString("dd/MM/yyyy HH:mm") 19 Add-Content $logFile -value "$date - Starting..." 20 21 # Unregister old ocs inventory scheduled task if exists 22 schtasks /delete /tn "OCS Inventory Agent Uninstall" /f 23 24 $date = (Get-date).ToString("dd/MM/yyyy HH:mm") 25 Add-Content $logFile -value "$date - Scheduled task OCS InventoryAgent Uninstall to launch $path\removeupdateagent.ps1" 26 27 # Register new ocs inventory scheduled task 28 $hour = (Get-Date).AddMinutes(5).ToString("HH:mm") 29 $result = schtasks /create /tn "OCS Inventory Agent Uninstall" /tr "PowerShell.exe -ExecutionPolicy ByPass -File $path\removeupdateagent.ps1" /sc once /st $hour /ru System 30 31 if(!$result) { 32 $date = (Get-date).ToString("dd/MM/yyyy HH:mm") 33 Add-Content $logFile -value "$date - Scheduled task NOT OK" 34 [Environment]::Exit(1) 35 } 36 37 $date = (Get-date).ToString("dd/MM/yyyy HH:mm") 38 Add-Content $logFile -value "$date - Scheduled task OK. The old Agent will be uninstalled in 5 minutes" 39 [Environment]::Exit(0)