How to get multiple value data from remote registry key

DHoss 61 Reputation points
2022-05-02T03:22:09.44+00:00

I am trying to get two value data in one line. It works fine when I do it in two line but I want to use one line code. Any help would be appreciated.
Here is my code:

Blockquote

$Getservice = Get-Service -ComputerName $Computer -Name RemoteRegistry
If ($Getservice.Status -eq "Stopped") {
Set-service -ComputerName $Computer -Name RemoteRegistry -Status Running -StartupType Manual -ErrorAction stop

$regcon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $Computer)
$regkey = $regcon.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\")
$gvalue = $regkey.GetValue('DisplayVersion')
$winver = $regkey.GetValue('ReleaseID')
}
Do {
Get-service -ComputerName $Computer -Name RemoteRegistry | Stop-Service -Force
Set-Service -ComputerName $Computer -Name RemoteRegistry -Status Stopped -StartupType Disabled
$StopRemoteRegistry = Get-Service -ComputerName $Computer -Name RemoteRegistry
} while($StopRemoteRegistry.StartType -eq "Enabled")

Blockquote

I am trying to accomplish...
$winver = $regkey.GetValue('ReleaseID', 'DisplayVersion')

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,091 Reputation points
    2022-05-02T14:59:11.147+00:00

    Do you want a single STRING or do you want an ARRAY?

    # as a string
    $winver = "{0} {1}" -f $regkey.GetValue('DisplayVersion'), $regkey.GetValue('ReleaseID')
    
    # as an array
    $winver = $regkey.GetValue('DisplayVersion'), $regkey.GetValue('ReleaseID')
    

    I'm curious: Why does it have to be done in one line of code?


  2. Rich Matheisen 45,091 Reputation points
    2022-05-02T18:45:26.86+00:00

    This (using only a local machine) produces a single string in the $winver variable:

    $regcon = [Microsoft.Win32.RegistryKey]::OpenBaseKey("LocalMachine","Registry64")
    $regkey = $regcon.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\")
    # as a string
    $winver = "{0} {1}" -f $regkey.GetValue('ReleaseID'),$regkey.GetValue('DisplayVersion')
    
    $winver
    

    Since you provide no explanation of how "it does not work" I have no idea how to help you,