How to pipe output results to variable

Welcome Forums General PowerShell Q&A How to pipe output results to variable

Viewing 7 reply threads
  • Author
    Posts
    • #191137
      Participant
      Topics: 21
      Replies: 74
      Points: 354
      Rank: Contributor

      Need output results piped out to file

      
      PS C:\WINDOWS\system32> $results = invoke-command -computer TGCS001-2012R2 -scriptblock {reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager" /v PendingFileRenameOperations /f}
      
      
      PS C:\WINDOWS\system32> $results = invoke-command -computer TGCS001-2012R2 -scriptblock {(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager' -Name PendingFileRenameOperations ).PendingFileRenameOperations}
      
      Property PendingFileRenameOperations does not exist at path HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager.
      + CategoryInfo : InvalidArgument: (PendingFileRenameOperations:String) [Get-ItemProperty], PSArgumentException
      + FullyQualifiedErrorId : System.Management.Automation.PSArgumentException,Microsoft.PowerShell.Commands.GetItemPropertyCommand
      + PSComputerName : TGCS001-2012R2
      
       
      
      PS C:\WINDOWS\system32> $results
      The operation completed successfully.
      
      
      

      As you can see the first command places results in $Results.  The second command places results to console

      How do I get the results to be added to $results

      I tried +=

       

      Any ideas?

       

      Thank you

       

      Tom

       

       

       

       

    • #191140
      js
      Participant
      Topics: 27
      Replies: 716
      Points: 1,879
      Helping Hand
      Rank: Community Hero

      After the first command, $result isn't an array, so you can't add to it that way.

      
      [array]$results += 
      
      
    • #191143
      Participant
      Topics: 21
      Replies: 74
      Points: 354
      Rank: Contributor

      JS

      
      try
      {
      [array] $results = invoke-command -computer TGCS001-2012R2 -scriptblock {reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager" /v PendingFileRenameOperations /f}
      }
      catch
      {
      [array] $results += invoke-command -computer TGCS001-2012R2 -scriptblock {(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager' -Name PendingFileRenameOperations ).PendingFileRenameOperations}
      }
      
      Added the array and when it errors I do not get anything in $results
      
      I added   the try catch
      
      If line 1 has a value I need line 2 to run if line one is not present no need to run line two.
      
       
      
      Any ideas?
      
       
      
       
      
       
      
      
      • #191146
        js
        Participant
        Topics: 27
        Replies: 716
        Points: 1,879
        Helping Hand
        Rank: Community Hero

        Well, the error says pendingfilerenameoperations does not exist.

    • #191149
      Participant
      Topics: 21
      Replies: 74
      Points: 354
      Rank: Contributor

      Yes so how do I code that

       

      Any ideas?

    • #191200
      Participant
      Topics: 21
      Replies: 38
      Points: 202
      Helping Hand
      Rank: Participant

      You have to handle the error. First of all, writing through mobile, so unable to design well. But can guide you the concept.

      
      try {
      
      $result += 1st statement -ea stop
      
      $result += 2nd statement -ea stop
      
      }
      
      Catch { $result += $_.exception.message}
      
      

      So, successful statement saves its output through try block and failed statement will do the same through catch block.

      Let me know your result.

    • #191215
      Participant
      Topics: 21
      Replies: 74
      Points: 354
      Rank: Contributor

      Sankhadip Roy

      Thank you that seems to work out

      How can I add the computer name to the $Results  I tried this with no luck

       

      
      try
      {
      $results = invoke-command -computer TGCS001-2012R2 -scriptblock {reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager" /v PendingFileRenameOperations /f} -ea Stop
      
      $results += invoke-command -computer TGCS001-2012R2 -scriptblock {(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager' -Name PendingFileRenameOperations ).PendingFileRenameOperations} -ea Stop
      
      $Results += 'Server TGCS001-2012R2'
      }
      catch { $Results += $_.exception.message}
      
      

      PS C:\WINDOWS\system32> $results
      ERROR: The system was unable to find the specified registry key or value.

       

      Would like $results to show

      Server = TGCS001-2012R2
      ERROR: The system was unable to find the specified registry key or value.

      Any ideas?

      Thanks

      Tom

       

       

       

    • #191227
      Participant
      Topics: 21
      Replies: 74
      Points: 354
      Rank: Contributor

      Guys

      Got a little further

      
      $Results += 'Server TGCS001-2012R2' | out-string
      try
      {
      $results += invoke-command -computer TGCS001-2012R2 -scriptblock {reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager" /v PendingFileRenameOperations /f} -ea Stop
      
      $results += invoke-command -computer TGCS001-2012R2 -scriptblock {(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager' -Name PendingFileRenameOperations ).PendingFileRenameOperations} -ea Stop
      
      }
      catch { $Results += $_.exception.message}
      
      $Results += 'Server TGCS002-2016' | out-string
      try
      {
      $results += invoke-command -computer TGCS002-2016.home.tgcsnet.com -scriptblock {reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager" /v PendingFileRenameOperations /f} -ea Stop
      
      $results += invoke-command -computer TGCS002-2016.home.tgcsnet.com -scriptblock {(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager' -Name PendingFileRenameOperations ).PendingFileRenameOperations} -ea Stop
      
      }
      catch { $Results += $_.exception.message}
      
      

       

      The output looks like this

      PS C:\WINDOWS\system32> $results
      Server TGCS001-2012R2
      ERROR: The system was unable to find the specified registry key or value.Server TGCS002-2016
      ERROR: The system was unable to find the specified registry key or value.

       

      How can I get the second, third forth etc computers on a separate line

      Server TGCS001-2012R2
      ERROR: The system was unable to find the specified registry key or value.

      Server TGCS002-2016
      ERROR: The system was unable to find the specified registry key or value.

      A blank line between each computer would be nice also.

       

       

       

    • #191233
      Participant
      Topics: 21
      Replies: 74
      Points: 354
      Rank: Contributor

      Mark this as resolved

      I figured it out.

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