Hi All,
I’m trying to run a script to detect java, remove it and then install the version I want. This would be run across multiple workstations, but act like it runs locally (ie: we have software to deploy this and run on machines).
The Plan:
- Create a scheduled task to resume the job
- Detect and uninstall Java
- Reboot
- Install Java based on architecture of machine.
The problem I’m facing is twofold.
- It doesn’t uninstall java
- It doesn’t resume the job after a reboot, the job remains stuck in a suspended state.
If I take the code out of the workflow and run it, it seems to work up until after the reboot…however I need the workflow to do the reboot and continue part.
Thanks in advance for any help…it’s running PS4.0. I’m just learning PowerShell, so please forgive any obvious mistakes. Oh, the first unregister-scheduled job is because I need to remove it before creating it during testing cause it’ll leave one in there prior to reboot.
Set-ExecutionPolicy Unrestricted Unregister-ScheduledJob -Name PowerShellResume #script to uninstall java and reinstall preferred version. #This script should differentiate between 64 bit version 32 bit and install the correct version. # Create a Scheduled Task to resume script on startup $user = "sometestaccount" $pwd = ConvertTo-SecureString -String "***************" -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential($user, $pwd) $AtStartup = New-JobTrigger -AtStartup Register-ScheduledJob -Name PowerShellResume ` -Credential $cred ` -Trigger $AtStartup ` -ScriptBlock {Import-Module PSWorkflow;` Resume-Job -Name ARJava} WorkFlow Remove-Add-Java { #Get Uninstall Strings for Java $items = InlineScript{Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -contains "java" } | Select-Object -Property DisplayName, UninstallString, DisplayVersion} #Run through and trigger uninstallers if multiple versions Foreach ($item in $items) { If ($item.UninstallString) { $uninst = $item.UninstallString InlineScript{& cmd /c $uninst /quiet /norestart} } else { {continue} } } #restart the computer Restart-Computer -Wait #Detect OS and architecture level $OS = (Get-WmiObject Win32_OperatingSystem).OSArchitecture #Run proper installer based on architecture If ($OS -like "64-bit") { Start-Process C:\Support\jre-8u161-windows-x64.exe -NoNewWindow -Wait } Else { Start-Process C:\Support\jre-8u161-windows-i586.exe -NoNewWindow -Wait } Unregister-ScheduledJob -Name PowerShellResume } #Run the workflow and give it a name which is used in the scheduled task. Remove-Add-Java -JobName ARJava