Solved

Help in Powershell

Posted on 2014-11-03
2
95 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 78

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

Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need some help with powershell script 5 36
will this script work on w2k3 server? 2 12
file name warning 4 26
Executing Powershell script from SQL Server 4 21
OfficeMate Freezes on login or does not load after login credentials are input.
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

895 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

14 Experts available now in Live!

Get 1:1 Help Now