Comparing in Powershell

Hello Guys,

I have written the following code for getting the cluster status:

cls
$computers =  Get-Content D:\Abhi\Server.txt

foreach ($line in $computers)
{
    Get-WmiObject -Class win32_bios -cn $line | Select __Server, @{N="InstanceName";E={$line}}
}

Open in new window


The input in the Server.txt file will be like this:

LogicalName1
LogicalName2
LogicalName3
LogicalName4

The output of the code will be:
__SERVER       InstanceName
--------       ------------
MachineName1   LogicalName1
MachineName2   LogicalName2
MachineName3   LogicalName3
MachineName4   LogicalName4

The thing is I want this code to even check if the entries in the __SERVER column is not same. I mean there shouldn't be a single machine name for two or more logical instances. If that is the case a mail should be triggered. I'm not able to figure out how the looping constructs should be in this case conceptually.  

Kindly give your inputs and edits.
Eldo issacAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try this:
cls
$Computers =  Get-Content D:\Abhi\Server.txt
$Result = $Computers | % {
	Get-WmiObject -Class win32_bios -cn $_ | Select __Server, @{N="InstanceName";E={$_}}
}
$Result
If ($Result.Count -ne ($Result | Select-Object -Property __Server -Unique).Count) {
	"More than one instance running on one machine!" | Write-Warning
}

Open in new window

0
Eldo issacAuthor Commented:
Hello oBdA,

Thanks for your reply. The comparison is working fine. Only thing is $Result is coming like this:

__SERVER                                         InstanceName                                  
--------                                                            ------------                                  
M1                                                                \\M1\root\cimv2:Win32_BIOS.Name="Defa...

I need the Instance name as the one mentioned in input txt file.
Kindly suggest an edit for this.
0
oBdACommented:
Sorry:
cls
$Computers =  Get-Content D:\Abhi\Server.txt
$Result = $Computers | % {
	$Line = $_
	Get-WmiObject -Class win32_bios -cn $Line | Select __Server, @{N="InstanceName";E={$Line}}
}
$Result
If ($Result.Count -ne ($Result | Select-Object -Property __Server -Unique).Count) {
	"More than one instance running on one machine!" | Write-Warning
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eldo issacAuthor Commented:
Hello oBdA,

Thank you very much for your code, it works perfectly. Just one more favor, in our environment we also have 3 SQL instances running on 2 machines. Like this:

__SERVER       InstanceName
--------       ------------
MachineName1   LogicalName1
MachineName1   LogicalName2
MachineName2   LogicalName3

Is there an edit that u can suggest in your code, so that I can do monitoring on this cluster as well. Meaning, in this all the three logical instances shouldn't be online on a single machine.

Kindly give your comments.
0
oBdACommented:
Like that:
cls
$Computers =  Get-Content D:\Abhi\Server.txt
$Result = $Computers | % {
	$Line = $_
	Get-WmiObject -Class win32_bios -cn $Line | Select __Server, @{N="InstanceName";E={$Line}}
}
$Result
If (1 -eq ($Result | Select-Object -Property __Server -Unique).Count) {
	"All instances running on one machine!" | Write-Warning
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.