Specified cast is not valid. (at<ScriptBlock>

Topics: Archive - Deployment Scripts, Archive - General
Sep 25, 2014 at 3:49 PM
I have a few random systems that throw the following error for an application install script. One win 7 and one win 8 all other systems seem to install fine.

Specified cast is not valid. (at<scriptBlock>.
c:....AppDeployToolkitMain.ps1:
line 1009
at Get-InstalledApplication

at Remove-MSIApplications,
c:....AppDeployToolkitMain.ps1:
line 1235

Any help or suggestions would be appreciated
Oct 1, 2014 at 2:50 PM
It would be useful to see your Deploy-Application.ps1 script to better understand why you are getting this error.
Oct 14, 2014 at 7:32 PM

*=============================================================================

Remove any previous versions of Application

*=============================================================================

Remove-MSIApplications "Application" 

*=============================================================================

Remove left over ENTRUST registry keys

*=============================================================================

Remove-RegistryKey -Key 'HKCU:\Software\App' -Recurse 
Remove-RegistryKey -Key 'HKLM:\Software\App' -Recurse 
Remove-RegistryKey -Key 'HKLM:\Software\Wow6432Node\App' -Recurse
Developer
Oct 14, 2014 at 8:04 PM
Which version of the toolkit are you using so that we can look up those line numbers from the error message.
Oct 17, 2014 at 5:56 PM
I am also have the same error in the Get-InstalledApplication function as part of an uninstall. I am using the toolkit to install Java 7 Update 71 and have it attempt to remove any previous version of Java 6 or 7. So far I know of only one system that is giving me this error in a deployment of about 500.

So, from "Deploy-Application.ps1"
Remove-MSIApplications "Java(TM) 6 Update"
From "_Java_7Update71_x86_EN_01_PSAppDeployToolkit_Install.log"
[16-10-2014 18:22:58] [Pre-Installation] Getting information for installed Application Name [Java(TM) 6 Update]...
[16-10-2014 18:22:58] [Pre-Installation] Specified cast is not valid.
(at <ScriptBlock>, C:\Windows\ccmcache\m9\AppDeployToolkit\AppDeployToolkitMain.ps1: line 1053
at Get-InstalledApplication, C:\Windows\ccmcache\m9\AppDeployToolkit\AppDeployToolkitMain.ps1: line 1053
at Remove-MSIApplications, C:\Windows\ccmcache\m9\AppDeployToolkit\AppDeployToolkitMain.ps1: line 1305
at <ScriptBlock>, C:\Windows\ccmcache\m9\Deploy-Application.ps1: line 109)
I am using version 3.2.0 of the toolkit and from the logs you can see I am using SCCM (2012 R2) for this deployment. OS for this user is Windows 8.1 x64 and his system has Java 7 Update 67 and Java Dev Kit 8.5; it does not have any version of Java 6 installed. Line 1053 in "AppDeployToolkitMain.ps1" deals with looping through all the currently installed applications and their uninstallation properties in the registry so my guess would be that my user has a program installed that has some properties with special characters in it that is throwing off PowerShell. I will continue gathering some data from the system to see if I can confirm my theory but I saw this thread and thought I should share what I have found so far.

Thanks!
Oct 17, 2014 at 9:16 PM
I found the offending key in the registry, but I can't figure out what it doesn't like about it. The error is specifically coming from the command:
Get-ItemProperty -LiteralPath $_.PsPath
Found on line 1053 in "AppDeployToolkitMain.ps1" as part of a larger command. This command works fine except for these two PSPaths:
Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\nbi-glassfish-mod-4.0.0.89.0
Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\nbi-nb-base-8.0.0.0.201403101706
Trying to execute the command manually gives me the same error
Get-ItemProperty -LiteralPath 'Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\nbi-glassfish-mod-4.0.0.89.0'

Get-ItemProperty : Specified cast is not valid.
At line:1 char:1
+ Get-ItemProperty -LiteralPath "Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_M ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ItemProperty], InvalidCastException
    + FullyQualifiedErrorId : System.InvalidCastException,Microsoft.PowerShell.Commands.GetItemPropertyCommand
So these two applications are nothing special, GlassFish Server and NetBeans IDE, but why does this command have such and issue with these Registry Key names? I know removing the programs and deleting the keys would probably get things working as far as the toolkit is concerned but I would rather find out why it is having a problem instead of just working around it. Anyone have any ideas?
Oct 17, 2014 at 10:24 PM
Found the issue, underneath these keys was a DWORD value with invalid data. Once I deleted that value it worked as expected. For the OP, I was able to find what keys were having the problem using this script:
$regKeyApplications = @( "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" )
Foreach ($regKey in $regKeyApplications ) {
    If (Test-Path $regKey -ErrorAction SilentlyContinue) {
        $regKeyApplication = Get-ChildItem $regKey -ErrorAction SilentlyContinue | ForEach-Object { 
            Write-Host $_.PsPath
            Get-ItemProperty -LiteralPath $_.PsPath 
            }
    }
}
This script is essentially just the lines of code from "AppDeployToolkitMain.ps1" that were having the original error. I changed it very slightly to output the PSPath to console before Get-ItemProperty was called so that I could match up the errors to the registry key that was having the problem. Hopefully this can help the OP.

Thanks for the great toolkit!
Oct 23, 2014 at 3:21 PM
I am using V3.1.1. If there was a way to ignore the invalid DWORD that would be great as I have 14000 systems to deploy to and handling the each failure on a case by case would be difficult.
Developer
Nov 4, 2014 at 1:13 AM
Thanks for bringing this to our attention. This fix has been integrated into the toolkit with the latest 3.5.0 release.