Extracting data from Invoke-Command variable returned from ScriptBlock

Welcome Forums General PowerShell Q&A Extracting data from Invoke-Command variable returned from ScriptBlock

Viewing 8 reply threads
  • Author
    Posts
    • #247645
      Participant
      Topics: 1
      Replies: 4
      Points: -19
      Rank: Member

      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.

      • This topic was modified 1 month, 2 weeks ago by Gringgo79.
    • #247647
      Participant
      Topics: 0
      Replies: 78
      Points: 348
      Helping Hand
      Rank: Contributor

      $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.

      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:

    • #247655
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      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:

      [/crayon]

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

      [/crayon]

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

      [/crayon]
    • #247852
      Participant
      Topics: 1
      Replies: 4
      Points: -19
      Rank: Member

      Thank you!

    • #247855
      Participant
      Topics: 1
      Replies: 4
      Points: -19
      Rank: Member

      Thank you for the Read Me First and your reply.

    • #247915
      Participant
      Topics: 1
      Replies: 4
      Points: -19
      Rank: Member

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

      $RegItems =<br />Invoke-Command  -ComputerName $ComputerName {<br />    $ENV:Path<br />}

      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'<br />$RegItems =<br />Invoke-Command -ComputerName $ComputerName {<br />    [PSCustomObject]@{<br />        KeyType  = (Get-Item -Path $using:RegKeyPath).GetValueKind('Path')<br />        KeyValue = (Get-ItemProperty -Path $using:RegKeyPath -Name 'Path').Path<br />    }<br />} Now you have the values you’re after in the variable $RegItems and you can access them seperately with their subexpression names

      $RegItems.KeyType<br />$RegItems.KeyValue

      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?

    • #247936
      Participant
      Topics: 0
      Replies: 78
      Points: 348
      Helping Hand
      Rank: Contributor

      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().

    • #247948
      Participant
      Topics: 1
      Replies: 4
      Points: -19
      Rank: Member

      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().

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

      Thanks again, @AdminOfThings45

    • #247963
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      thanks again.

      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.

Viewing 8 reply threads
  • You must be logged in to reply to this topic.