(imported comment written by brolly3391)
First, here is the way I would do it:
q: if (exists key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft.NETFramework” whose (exists value “InstallRoot” whose (exists folder (it as string) whose (exists file “netfxsbs10.exe” of it)) of it) of registry) then (version of file “netfxsbs10.exe” of folder (value “InstallRoot” of key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft.NETFramework” of registry as string) )as string else (“Not Installed”)
A: 2.0.50727.42
T: 3.307 ms
I: singular string
Lets see if we can make your approach work.
First, we cannot use ELSE outside of an IF THEN ELSE construct. Let’s take the windows XP possibilities on first:
q: version of file “netfxsbs10.exe” of folder “C:\WINDOWS\Microsoft.NET\Framework”
A: 2.0.50727.42
T: 2.156 ms
I: singular version
seems good. But what happens if we take the Windows 2000 possibility on the same XP test box?
q: version of file “netfxsbs10.exe” of folder “C:\WINNT\Microsoft.NET\Framework”
E: Singular expression refers to nonexistent object.
An error result for any part of our relevance is bad because it will error out the entire statement even if it would otherwise be true. So we need to make sure that the folder and file both exist before we pull the file version. The folder is easy:
q: exists folder “C:\WINNT\Microsoft.NET\Framework”
A: False
but the file + folder is not as easy:
q: exists file “netfxsbs10.exe” of folder “C:\WINNT\Microsoft.NET\Framework”
E: Singular expression refers to nonexistent object.
It’s best to approach this with a WHOSE IT clause.
q: exists folder “C:\WINNT\Microsoft.NET\Framework” whose ( exists file “netfxsbs10.exe” of it )
A: False
Now we don’t get that error which was going to trip us up later. It always seems to me like these things are built upside-down… So now we put our test for existence into the IF THEN ELSE and get:
q: if (exists folder “C:\WINNT\Microsoft.NET\Framework” whose ( exists file “netfxsbs10.exe” of it )) then (version of file “netfxsbs10.exe” of folder “C:\WINNT\Microsoft.NET\Framework”) else “Not Installed”
E: Incompatible types.
we are so close… IF THEN ELSE wants the return types of the THEN and the ELSE to match, so we should turn the version into a string to match the string “Not Installed”. We can do that using the cast AS STRING.
q: if (exists folder “C:\WINNT\Microsoft.NET\Framework” whose ( exists file “netfxsbs10.exe” of it )) then (version of file “netfxsbs10.exe” of folder “C:\WINNT\Microsoft.NET\Framework”) as string else “Not Installed”
A: Not Installed
T: 0.503 ms
I: singular string
and the Windows XP half:
q: if (exists folder “C:\WINDOWS\Microsoft.NET\Framework” whose ( exists file “netfxsbs10.exe” of it )) then (version of file “netfxsbs10.exe” of folder “C:\WINDOWS\Microsoft.NET\Framework”) as string else “Not Installed”
A: 2.0.50727.42
T: 2.900 ms
I: singular string
Victory! No more error when we run the Windows 2000 version on a Windows XP box. Now we can stick them together in a nested IF THEN ELSE. This would be your final relevance using the method you put forward in your post.
q: if (exists folder “C:\WINDOWS\Microsoft.NET\Framework” whose ( exists file “netfxsbs10.exe” of it )) then (version of file “netfxsbs10.exe” of folder “C:\WINDOWS\Microsoft.NET\Framework”) as string else ( if (exists folder “C:\WINNT\Microsoft.NET\Framework” whose ( exists file “netfxsbs10.exe” of it )) then (version of file “netfxsbs10.exe” of folder “C:\WINNT\Microsoft.NET\Framework”) as string else “Not Installed” )
A: 2.0.50727.42
T: 2.875 ms
I: singular string
Frequently, installed runtimes and applications will have a pointer in a fixed location in the Registry that tells you the folder where it is installed. I like to use those registry pointers instead of hard coded folder names whenever possible when querying the information for those applications. It takes care of the “What if the user somehow installed the application in a non-default location?” question. Of course we then need to test for the existence of the registry key as well as the existence of the folder and file before using it or else we risk getting that “E: Singular expression refers to nonexistent object” error from before. The nice thing is that we don’t have to write 2 different versions and paste them together in a nested IF THEN ELSE. The not so nice thing is we get really deep on the nested WHOSE IF clauses as we check for the existence of each item along the way. (see the statement at the top of page)
There are lots of good examples of how the BigFix pros do this in the existing fixlets, especially the MS0x-xxx series.