Strange Sceen Output Behavior

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Jason Durnil Jason Durnil 1 year, 7 months ago.

  • Author
    Posts
  • #32478
    Profile photo of Jason Durnil
    Jason Durnil
    Participant

    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!

  • #32479
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    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/

  • #32481
    Profile photo of Jason Durnil
    Jason Durnil
    Participant

    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!

You must be logged in to reply to this topic.