Running PowerShell command from VB (or C#) on multiple machines - how to know which computer is returning infomation

Darren Rose 496 Reputation points
2021-01-18T14:06:33.337+00:00

I am running code as per simple example below, in reality it will be a lot more machines, how do I know which machine each set of results is from?

I read mention of PSComputerName, but how do I get the value from this as the usual way of getting a value e.g. how I get the Name in this example below for Get-Culture doesn't work

Thanks

Dim computerlist As New List(Of String)({"PC1", "PC2", "PC3"})

Dim powershell As PowerShell = PowerShell.Create()
Dim command As New PSCommand()

command.AddCommand("Invoke-Command")
command.AddParameter("ComputerName", computerlist)
command.AddParameter("ScriptBlock", ScriptBlock.Create("Get-Culture"))

powershell.Commands = command

Dim results = powershell.Invoke()

TextBox1.AppendText(results(0).BaseObject.Name)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,005 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,734 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,549 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue 37,541 Reputation points Microsoft Vendor
    2021-01-19T07:49:45.297+00:00

    Hi,

    PSComputerName is a NoteProperty. Does this work for you?

    results(0).Properties("PSComputerName").Value  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. MotoX80 34,346 Reputation points
    2021-01-19T01:07:12.37+00:00

    How about something like this?

    command.AddParameter("ScriptBlock", ScriptBlock.Create("'{0},{1},{2},{3}' -f $env:COMPUTERNAME, (Get-Culture).LCID,(Get-Culture).Name,(Get-Culture).DisplayName"))
    

    Results should look like this

    PS C:\> '{0},{1},{2},{3}' -f $env:COMPUTERNAME, (Get-Culture).LCID,(Get-Culture).Name,(Get-Culture).DisplayName
    SLICK,1033,en-US,English (United States)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.