Updating registry with invoke sql result

Hi,

I’m trying to update registry with the result from select query, the registry was updated with null although the query result is not null.
if I run this:

$taradisc = Invoke-Sqlcmd -Query "
select discount_id from openquery(officels,'select * from discount')
where basesource=10 and discount_type='A' and status='T' and in_sale='T' and discitemonly='T'" -ServerInstance "localhost\SQLEXPRESS"  
$Updreg =$taradisc.discount_id
 $Updreg

I get the query result 5
PS C:\Windows\system32> $Updreg
5

but when I try to update the registry with $Updreg, It update the registry with null.

Invoke-Command -ComputerName testmachine {New-ItemProperty -path registry::HKEY_LOCAL_MACHINE\SOFTWARE\PosiTwip\Salesman -name  TaraWeightDiscountId -Type String -Value $Updreg -Force} 
TaraWeightDiscountId : 
PSPath               : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\PosiTwip\Salesman
PSParentPath         : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\PosiTwip
PSChildName          : Salesman
PSProvider           : Microsoft.PowerShell.Core\Registry

I expect that TaraWeightDiscountId will show 5
What should I do?

When passing variables to Invoke-Command, you need to prefix with $Using:

Try changing -Value $Updreg to -Value $Using:Updreg

It works great. Thanks!

בתאריך יום ג׳, 16 במרץ 2021, 17:24, מאת TonyD via PowerShell Forums ‏<powershell@discoursemail.com>: