Author Posts

December 1, 2015 at 6:23 pm

Ive been having a blast playing with Powershell but Ive ran into what I think is a simple issue that has me stumped.


Invoke-Command -ComputerName $i -ScriptBlock {foreach ($i in $i)
{Write-Output "Name: $i" + "`n" + "=========================="; Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Format-Table –AutoSize}} -credential $cred

Its in the Write-Output portion. I want each result returned to start with the hostname so I know which results belong to which system.

Currently I get this output

Name:
+

+
==========================

With this {"Name:" + $i + "`n" + "=========================="; I get

Name:
==========================

I know $i has to have my variable in it or my script wouldnt work at all.

Thanks in advance!

December 1, 2015 at 6:54 pm

You need to use the define a param block in your scriptblock and pass in the value using the -argumentlist parameter of invoke-command, or if you are using PoSh v3.0 or newer, you can user $using to pass them in. Check out this post: https://powershell.org/kb/remote-variables-and-invoke-command/

December 1, 2015 at 10:07 pm

Thank you for the input. This isnt as easy as I would have expected.

When I do

{Write-Host "Name:" + $using:i + "`n" + "==========================";

I get the whole file of hostnames. Id like to just get the hostname for the server the installed apps come from.

But then I realized I could just pull the hostname from the remote computer with

{Write-Host "`n" "`n" "`n" "Name:" $env:computername "`n" "==========================";

Thanks again!