Extracting data from Invoke-Command variable returned from ScriptBlock

I have this slice of code that returns data from my Invoke-Command statement.

$RegItems = Invoke-Command -ComputerName $Computer {
($KeyType = Get-Item -Path $using:RegKeyPath).GetValueKind(‘Path’)
($KeyValue = Get-ItemProperty -Path $using:RegKeyPath -Name Path).Path
}

This statement: Write-Output “RegItems: $RegItems” shows me that I have what I want, but I can’t figure out how to get the data out. The output below has the two items I want. String represents the type of registry key and following that is the value of the key.

OUTPUT: RegItems: String %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\WindowsPowerShell\v1.0;

Your assistance is greatly appreciated.

$RegItems appears to be an array (collection to be more PowerShelly) since you are returning multiple success stream writes from one command. You can access the index of each array element to retrieve the values.

$RegItems[0] # Contains the key type
$RegItems[1] # Contains the registry value


When you double quote a variable that is an array, it stringifies the array object. It is like casting the array to a string [string]$RegItems. The result is all of the items joined into a single string delimited by the $OFS value on the system. $OFS by default is a single space.

It is unclear what you want to do with the extracted data. But here are some examples:

# Output type and value on separate lines
$RegItems

# Write a special string for each item
"Key Type: {0}`nKey Value: {1}" -f $RegItems[0],$RegItems[1]

# Create object with properties to reference
$obj = [pscustomobject]@{ 'KeyType' = $RegItems[0]; 'KeyValue' = $RegItems[1]}
$obj.KeyType # Key Type
$obj.KeyValue # Key Value
$obj.KeyValue -split ';' # Delimits value by semi-colon to list each path on its own line


John, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “CODE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

If you want the get the value of the environment variable of a reomte computer you actually just need this:

$RegItems = 
Invoke-Command  -ComputerName $ComputerName {
    $ENV:Path
}

But if you want to get this info from the remote registry you can do it like this:

$RegKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
$RegItems = 
Invoke-Command -ComputerName $ComputerName {
    [PSCustomObject]@{
        KeyType  = (Get-Item -Path $using:RegKeyPath).GetValueKind('Path')
        KeyValue = (Get-ItemProperty -Path $using:RegKeyPath -Name 'Path').Path
    }
}

Now you have the values you’re after in the variable $RegItems and you can access them seperately with their subexpression names

$RegItems.KeyType
$RegItems.KeyValue

Thank you!

Thank you for the Read Me First and your reply.

[quote quote=247655]If you want the get the value of the environment variable of a remote computer you actually just need this:

$RegItems =
Invoke-Command -ComputerName $ComputerName {
$ENV:Path
}

But if you want to get this info from the remote registry you can do it like this:

$RegKeyPath = ‘HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment’
$RegItems =
Invoke-Command -ComputerName $ComputerName {
[PSCustomObject]@{
KeyType = (Get-Item -Path $using:RegKeyPath).GetValueKind(‘Path’)
KeyValue = (Get-ItemProperty -Path $using:RegKeyPath -Name ‘Path’).Path
}
}
Now you have the values you’re after in the variable $RegItems and you can access them seperately with their subexpression names

$RegItems.KeyType
$RegItems.KeyValue
[/quote]

I used the second example for my script. I see where I had the issue, thanks again.

After making the changes and checking my variables, all is good. Then I run into a perplexing issue. Later in the script I have If ($RegItems.KeyType -eq “String” -and $RegItems.KeyValue -match “%”) the value of $RegItems.KeyTypehas changed from “String” to “1” or “ExpandString” to “2”.

How is this possible?

For KeyType, you are seeing the [int] value of your property. KeyType is actually a [Microsoft.Win32.RegistryValueKind] object type.

When you see ExpandString or String, a ToString() method must be applied implicitly. When you see the numerical value, either value__ is retrieved or [int] conversion is implicitly applied somewhere. You could potentially try explicitly forcing the output you want to see with $RegItems.KeyType.ToString().

[quote quote=247936]For KeyType, you are seeing the [int] value of your property. KeyType is actually a [Microsoft.Win32.RegistryValueKind] object type.

When you see ExpandString or String, a ToString() method must be applied implicitly. When you see the numerical value, either value__ is retrieved or [int] conversion is implicitly applied somewhere. You could potentially try explicitly forcing the output you want to see with $RegItems.KeyType.ToString().

[/quote]

I had an inkling of a string function being involved, and this, $RegItems.KeyType.ToString() ,certainly was the trick.

Thanks again, @AdminOfThings45

One last little thing. Could please stop posting full quotes every time you answer? If you have to clarify a particular part of a post you should quote only this particular part - not the full post.

Thanks in advance.