Solved

Help in Powershell

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

Too many email signature changes to deal with?

Are you constantly being asked to update your organization's email signatures? Do they take up too much of your time? Wouldn't you love to be able to manage all signatures from one central location, easily design them and deploy them quickly to users. Well, you can!

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

757 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

24 Experts available now in Live!

Get 1:1 Help Now