WorkFlow help

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:

  1. Create a scheduled task to resume the job
  2. Detect and uninstall Java
  3. Reboot
  4. Install Java based on architecture of machine.

The problem I’m facing is twofold.

  1. It doesn’t uninstall java
  2. 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 

Steve,
welcome to the Powershell.org forum.

You cannot use “Set-ExecutionPolicy Unrestricted” inside a script. It has to be executed before running any script in order to be able to run scripts. It’s a little bit like the chicken and egg dilemma. :wink:

My first question would be: If you’re a beginner and try to learn Powershell why not keeping it simple? Why workflows? Why scheduled jobs? Why rebooting? You can uninstall any java version and install the version you like without rebooting.

I would recommend to use the msi version of the java installer. This way you have a slightly better control over the installation process. And it’s easier to run the installation silently. At the moment you simply fire the installation executable and probably the normal installation interface appears, right?

What is “InlineScript”?

Thanks for replying Olaf.

When I originally tried without works flow to just uninstall and reinstall the version I wanted, it gave me a mismatch error in the event logs which required a reboot to fix so that I could install the new version. I so figured I’d uninstall, reboot, and then install.

I haven’t gotten to the install part in my script yet because the scheduled job fails to resume, but it might through up the interface.

Inlinescript is what you have to use for some powershell commands that aren’t available within workflows, or don’t work as expected within workflows.

I’m open to suggestions or reworking the code if I have to.

I like to use msiexec to install/uninstall products. Uninstallation:
$msiexecargs = “/x {0} /qn” -f $msi.FullName
Start-Process msiexec.exe -ArgumentList $msiexecargs -Wait

Hmmm, ok we’re used to use always the current version of the JRE and with this it’s not a problem to uninstall the old one and install the new without the need for a reboot in between.
But I can remember to a former customer where we had some problems sometimes. There we used the Java uninstall tool from Oracle. You could try this.