Welcome › Forums › General PowerShell Q&A › How to pipe output results to variable
- This topic has 8 replies, 3 voices, and was last updated 1 week ago by
Participant.
-
AuthorPosts
-
-
December 1, 2019 at 8:56 pm #191137
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
-
December 1, 2019 at 11:29 pm #191140
After the first command, $result isn't an array, so you can't add to it that way.
[array]$results +=
-
December 1, 2019 at 11:58 pm #191143
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?
-
December 2, 2019 at 12:01 am #191146
Well, the error says pendingfilerenameoperations does not exist.
-
-
December 2, 2019 at 12:25 am #191149
Yes so how do I code that
Any ideas?
-
December 2, 2019 at 12:48 pm #191200
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.
-
December 2, 2019 at 3:10 pm #191215
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
-
December 2, 2019 at 4:15 pm #191227
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.
-
December 2, 2019 at 7:34 pm #191233
Mark this as resolved
I figured it out.
-
-
AuthorPosts
- You must be logged in to reply to this topic.