Link to home
Start Free TrialLog in
Avatar of Eldo issac
Eldo issac

asked on

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.
Avatar of oBdA
oBdA

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

Avatar of Eldo issac

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial