Public/Check-VMHealth.ps1

<#
.Synopsis
This script is used to check VMs Health on PMs
 
.Description
This script will query all VMs Health on PMs
 
.Parameter ComputerName
Alias are "Cn" and "MachineName".
 
.Parameter Credential
Your ada account credential. It's just for DSE's PM querying.
It will not take effect if the PM list not contains DSE's PM.
 
.Example
$result = Get-VMHealth -ComputerName SVR1,SVR2 -Credential $cred_ada
test
.Outputs
PSObject[]
 
#>

function Get-VMHealth {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [Alias("Cn", "MachineName")]
        [String[]]$ComputerName,
        [scriptblock]$ScriptBlock,
        [PSCredential]$Credential

    )
    
    begin {
        
        if($ScriptBlock)
        {

            $sb = $ScriptBlock
        }
        else {
           $sb = {
            $Service = Get-Service vmms -ErrorAction Ignore

            if ($Service) 
            {

                $allVMs = Get-VM

                if(Get-VM | ? name -Match 'DSE')
                 {
                    $Role = 'DSE'
                 }
                 elseif(Get-VM | ? name -Match 'SPD')
                 {
                    $Role = 'SPD'
                 }
                 elseif(Get-VM | ? name -Match 'F')
                 {
                    $Role = 'Search'
                 }
                 else
                 {
                    $Role = 'Content'
                 }

                if($allVMs)
                {
                   # $vms = @()
                    $offVm = Get-VM | where-object State -eq off
                    if($offVm)
                    {
                        $offVM | % { $vms +=$_.name+"|"}
                        $vms = $vms.substring(0,$vms.length-1)
                        $vmstate = 'offline'
                    }#end offVv

                    $onlineVm = Get-VM | ? State -eq running
                    if($onlineVm)
                    {
                        $onlineVM | % { $vms +=$_.name+"|"}
                        $vms = $vms.substring(0,$vms.length-1)
                        $vmstate = 'running'
                    }#end onlineVM

                    return New-Object -TypeName PSObject -Property @{
                        VMList            = $vms
                        State           = $vmstate
                        Role = $Role
                        ComputerName    = $env:COMPUTERNAME
                    }
                }
                else{

                    return New-Object -TypeName PSObject -Property @{
                        VMList            = 'No VMs'
                        State           = 'NotDeploy'
                        Role = 'NA'
                        ComputerName    = $env:COMPUTERNAME
                    }
                }
            }
            else {
                return New-Object -TypeName PSObject -Property @{
                    VMList            = 'No VMs'
                    State           = 'NoService'
                    ComputerName    = $env:COMPUTERNAME
                }
            }

           
            }
        }
       
    }
    
    process {
        
            $result = invoke-SPORequest -ComputerName $ComputerName -ScriptBlock $sb -Credential $Credential
    }
    
    end {
        return $result
    }
}