LrngToFly
Posts: 16
Joined: Fri Nov 03, 2017 6:28 pm

Powershell script can't seem to read registry key

Hello-

I believe I have some sort of a permissions issue when running a powershell script. Not certain...

For my script, two lines are:
$regkey_value = (Get-ItemProperty -path 'HKLM:SOFTWARE\Microsoft\Office\ClickToRun\Configuration').Platform
Out-File -FilePath $AccessPSLog -Append -InputObject "Office Registry Key: $regkey_value"

The first line reads a registry key, the second line writes the value to a log file. When this script is run via Advanced Installer, all I get is "Office Registry key: " in my log file. When I do a Set-ExecutionPolicy unrestricted and run the powershell script in a powershell window, I get either "Office Registry key: x86" or "Office Registry key: x64" in my log file. I have my installer set to Run As Administrator.

Hopefully this there is an straightforward solution. Thanks for any advice!
Daniel
Posts: 8238
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Powershell script can't seem to read registry key

Hello,

This could happen because you do not also set the execution policy within your installation setup. To do so, you could create a BAT file with the following content:

Code: Select all

powershell.exe Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
and add it as a temporary file in "Files and Folders" page of your setup project. Then you could launch the above set execution policy BAT through a "Launch file" custom action. The custom action should be executed just before your powershell action.

Let us know if this helped.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
LrngToFly
Posts: 16
Joined: Fri Nov 03, 2017 6:28 pm

Re: Powershell script can't seem to read registry key

Ah, I figured out it wasn't a permissions issue after all. My installer is 32 bit and I haven't checked the 64 bit Powershell scrip checkbox in the custom action.

This line of powershell script
$regkey_value = (Get-ItemProperty -path 'HKLM:SOFTWARE\Microsoft\Office\ClickToRun\Configuration').Platform

was actually reading

SOFTWARE\WOW6432Node\Microsoft\Office\ClickToRun\Configuration').Platform

which doesn't exist...

From this post:

https://stackoverflow.com/questions/630 ... l-instance

I found I could force the 32 bit script to read with this code instead:

$key = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, [Microsoft.Win32.RegistryView]::Registry64)
$subKey = $key.OpenSubKey("SOFTWARE\Microsoft\Office\ClickToRun\Configuration")
$regkey_value = $subKey.GetValue("Platform")

Or I probably could have either switched my installer to be 64 bit or checked the 64 bit Powershell script.

Thanks for your help!
Daniel
Posts: 8238
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Powershell script can't seem to read registry key

You are always welcome. I am glad you got this working.

Thank you for sharing with us the solution too!

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”