How to pipe output results to variable

Need output results piped out to file

[pre]

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.

[/pre]

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

 

 

 

 

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

[array]$results += 

JS

[pre]

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?

 

 

 

[/pre]

Well, the error says pendingfilerenameoperations does not exist.

Yes so how do I code that

 

Any ideas?

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

[pre]

try {

$result += 1st statement -ea stop

$result += 2nd statement -ea stop

}

Catch { $result += $_.exception.message}

[/pre]

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

Let me know your result.

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

 

[pre]

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}

[/pre]

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

 

 

 

Guys

Got a little further

[pre]

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

[/pre]

 

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.

 

 

 

Mark this as resolved

I figured it out.