This project has moved and is read-only. For the latest updates, please go here.

Powershell script not running?

Topics: Configuration Issue
Mar 11, 2015 at 9:05 PM
Edited Mar 11, 2015 at 9:05 PM
I am installing Java 8 with a custom update, and it works fine. But included in that update is a powershell script to uninstall old Java installs; and it doesn't appear to be working. I can run the script from a powershell prompt - running as admin - and it works fine. Do the powershell scripts through custom updates get ran as admin? Is there anywhere I can look for a log of the custom update other than the windows update log? Below is my powershell script.
Param ([string]$iArch="")
#$ErrorActionPreference = "SilentlyContinue"
$cArch = Get-WMIObject win32_processor -Property addresswidth | Select-Object -Property addresswidth
If ($cArch.addresswidth -eq "32")
{
    If ($iArch -eq 32)
    {
        $uninst = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | ForEach-Object {Get-ItemProperty $_.pspath} | Where {$_.DisplayName -like "Java ?* Update *"}
    }
    else
    {
        exit
    }
    ForEach($i in $uninst)
    {
        #write-host $i.PSChildName
        $n = $i.PSChildName
        $cmd = "msiexec.exe"
        $args = @("/x","$n","/quiet")
        & $cmd $args
    }
}
If ($cArch.addresswidth -eq "64")
{
    If ($iArch -eq 32)
    {
        $uninst = Get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | ForEach-Object {Get-ItemProperty $_.pspath} | Where {$_.DisplayName -like "Java ?* Update *"}
    }
    elseif ($iArch -eq 64)
    {
        $uninst = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | ForEach-Object {Get-ItemProperty $_.pspath} | Where {$_.DisplayName -like "Java ?* Update *"}
    }
    ForEach($i in $uninst)
    {
        #write-host $i.PSChildName
        $n = $i.PSChildName
        $cmd = "msiexec.exe"
        $args = @("/x","$n","/quiet")
        & $cmd $args
    }
}
Mar 11, 2015 at 9:40 PM
Hi, you can find the log here : C:\Windows\Temp\CustomUpdateEngine.log
CustomUpdate runs with 'System' account if the option 'Can request user input' is NOT tick, otherwise, it runs with the same account than the logged user.
Mar 11, 2015 at 11:44 PM
I look like very close to the tool i wrote for uninstall2install Java. Maybe in a couple of days the Tool is online, then you can try to uninstall all existing java versions and install one java version.
Mar 12, 2015 at 2:00 PM
Edited Mar 12, 2015 at 2:08 PM
OK, so I looked at the log and it claimed to have ran the powershell script but nothing was done on the PC. I modified my script so that it doesn't require an argument and created a simple custom update to just run the uninstall script. No CustomUpdateEngine.log was created and nothing was uninstalled yet it reported successfully installed in Windows Update. Below is the powershell and then the xml for the update. Also, I added logging to the script and a log file was not created. How does wpp work with the powershell execution policy? Does the PC think it's running the script locally or off a network share? Or does it just ignore the execution policy? That could be the issue since I haven't ran any powershell scripts on the test PC before.
Param ([string]$iArch="32")
$log    = "C:\Windows\Temp\jUI32.log" 
$date = Get-Date 
$ErrorActionPreference = "SilentlyContinue"
"******Import users script ran on " + $date + " by: "+$env:USERNAME+": " | Out-File $log -append 
"---------------------------------------------------" | Out-File $log -append
$cArch = Get-WMIObject win32_processor -Property addresswidth | Select-Object -Property addresswidth
If ($cArch.addresswidth -eq "32")
{
    $uninst = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | ForEach-Object {Get-ItemProperty $_.pspath} | Where {$_.DisplayName -like "Java ?* Update *"}
    ForEach($i in $uninst)
    {
        #write-host $i.PSChildName
        $n = $i.PSChildName
        $cmd = "msiexec.exe"
        $args = @("/x","$n","/quiet")
        & $cmd $args
    }
}
If ($cArch.addresswidth -eq "64")
{
    $uninst = Get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | ForEach-Object {Get-ItemProperty $_.pspath} | Where {$_.DisplayName -like "Java ?* Update *"}
    ForEach($i in $uninst)
    {
        #write-host $i.PSChildName
        $n = $i.PSChildName
        $cmd = "msiexec.exe"
        $args = @("/x","$n","/quiet")
        & $cmd $args | Out-File $log -append 
        $n+" - uninstalled" | Out-File $log -append 
    }
}
"---------------------------------------------------" | Out-File $log -append
<CustomUpdate>
<Action>
<ElementType>CustomUpdateElements.VariableElement</ElementType>
<Name>return</Name>
<Type>Int</Type>
<ID>2ebd075c-0b44-438d-b781-89fcd3f94689</ID>
</Action>
<Action>
<ElementType>CustomUpdateElements.ScriptElement</ElementType>
<ScriptType>Powershell</ScriptType>
<Filename>jUI32.ps1</Filename>
<Arguments></Arguments>
<KillProcess>False</KillProcess>
<TimeBeforeKilling>10</TimeBeforeKilling>
<Variable>2ebd075c-0b44-438d-b781-89fcd3f94689</Variable>
</Action>
</CustomUpdate>
Mar 13, 2015 at 1:05 PM
After some trial and error I was able to make it work. My custom update xml is below. What's odd though is that the update ran just fine, but there was still no CustomUpdateEngine.log at C:\Windows\Temp.
<CustomUpdate>
<Action>
<ElementType>CustomUpdateElements.VariableElement</ElementType>
<Name>ret</Name>
<Type>Int</Type>
<ID>9dc0ecf3-6d10-489a-ade4-185dbd850217</ID>
</Action>
<Action>
<ElementType>CustomUpdateElements.ExecutableElement</ElementType>
<PathToExecutable>%windir%\System32\WindowsPowershell\v1.0\powershell.exe</PathToExecutable>
<Parameters>-ExecutionPolicy Bypass -WindowStyle Hidden -NonInteractive -NoProfile -File jUI32.ps1</Parameters>
<KillProcess>False</KillProcess>
<TimeBeforeKilling>10</TimeBeforeKilling>
<Variable>9dc0ecf3-6d10-489a-ade4-185dbd850217</Variable>
</Action>
</CustomUpdate>
Marked as answer by burgler00 on 3/13/2015 at 5:05 AM