The easiest way would be to change those single quotes to double quotes and use variable interpolation.
$targetname = "test"
$Targetresource.Productkey = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$targetname.MSSQLSERVER\productkey"
A more readable way might be to use the format operator "-f".
$targetname = test
$target = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\{0}.MSSQLSERVER\productkey' -f $targetname
$Targetresource.Productkey = Get-ItemProperty $target