Help accessing remote registry Value on domain Computers

When I run the following script on my computer it runs with no problem. When I try and run on computers in a domain, it gets the PC name and Username but fail to return a value from the registry key. Any help on how to get it to pull the remote registry value would be great! I have enabled remote registry access on the computers in question.

Thank you!!


$computername = Get-Content -path ".\pclist2.txt"

if (test-connection -computername $computername -quiet)
{
#Get PC name
foreach ($pc in $computername)
    {
    #Who Is logged on
    $loggedon =@(Get-WmiObject -ComputerName $pc -Namespace root\cimv2 -Class Win32_ComputerSystem)[0].UserName;

    #Citrix Info from Registry
    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('CurrentUser', $pc.computer)
    $regkey = $reg.OpenSubkey("SOFTWARE\\Citrix\\PNAgent")
    $LMPS = $regkey.GetValue("User Model 000")
    
    }
}

#Write Reg_Binary to String
$String = [string]::Join($null, ($LMPS | % { [char][int]$_; }))

#Isolate Username in String
$Username = $String.Substring(29,8)

#Write to .csv file
Add-Content "c:\PS\Audit.csv" "$pc,$loggedon,$UserName"

You could better use Invoke-Command and put all actions in the scriptblock. Below is an example.

Invoke-Command -ComputerName $pc -ScriptBlock {
$WinLogon = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
$LoggedOn = (Get-CimInstance -Namespace root\cimv2 -Class Win32_ComputerSystem).UserName
  [PSCustomObject]@{
    DefaultDomainName = $WinLogon.DefaultDomainName
    LoggedOn = $LoggedOn
  }
}

When targeting servers joined to domain, use -Credential parameter of Invoke-Command to authenticate.

Instead of $pc.computer you should use only $pc