?
Solved

Help in Powershell

Posted on 2014-11-03
2
Medium Priority
?
129 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 82

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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

752 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