Solved

Help in Powershell

Posted on 2014-11-03
2
98 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
2 Comments
 
LVL 79

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 39

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
This article explains how to install and use the NTBackup utility that comes with Windows Server.
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now