Solved

Help in Powershell

Posted on 2014-11-03
2
109 Views
Last Modified: 2014-11-05
Hey guys,

I have made the following script for getting the physical server names on which my logical instances are hosted. the script is:

$computers = "Instance1","Instance2";$computers|%{Get-WmiObject -Class win32_bios -cn $_ | Format-table __Server, @{N="InstanceName";E={$computers}}}

Open in new window


I'm getting the output of the following script as:

__SERVER                                                    InstanceName
--------                                                                       ----------
Server1                                                 {Instance1,Instance2}

__SERVER                                                    InstanceName
--------                                                                     ----------
Server2                                                  {Instance1,Instance2}

Can you guys please modify the script so that I can get the output in the following format:

__SERVER                                             InstanceName
--------                                                      ----------
Server1                                                 {Instance1}
Server2                                                  {Instance2

Thanks in advance
0
Comment
Question by:Abhinav Singh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 81

Expert Comment

by:David Johnson, CD, MVP
ID: 40420960
made code more readable.
$computers = "LOCALHOST","127.0.0.1";
foreach ($computer in $computers)
    {
        Get-WmiObject -Class win32_bios -cn $computer | 
        Format-table __Server, 
        @{
        N="InstanceName";
        E={$computers}
        }
    }

Open in new window

0
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 40421079
The problem is with having Format-Table within the foreach-object scriptblock, and then referencing the entire $computers array within it as well.  Better to just use a Select-Object command within a looping structure.
$computers = "Instance1","Instance2"
$computers | % `
{
    $instance = $_
    Get-WmiObject -Class win32_bios -cn $_ |
     Select __Server, @{N="InstanceName";E={$instance}}
} | Format-Table

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Power Action 2 39
Unable to create lists in Exchange 2013 1 32
Bing Maps Add-in in Exchange 2016 1 74
Copy data from one CSV into another line by line 5 14
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question