Posted On November 24, 2021

PowerShell: Search for Hyper-V Guest VM That Has Not Been Registered In Cluster

kimconnect 0 comments
blog.KimConnect.com >> Codes , Virtualization >> PowerShell: Search for Hyper-V Guest VM That Has Not Been Registered In Cluster
# findVmHost.ps1

$vmName='TESTVM01'

function findVmHost($vmName){
    try{
        Import-Module Hyper-V
        Import-Module FailoverClusters
        $allHyperVHosts={(Get-ClusterNode | Where { $_.State –eq "Up" }).Name | %{$_.ToLower()}}.Invoke()
        $allVms=foreach ($hyperVHost in $allHyperVHosts){invoke-command -computername $hyperVHost -scriptblock{write-host "Getting VM List on $env:computername";Get-VM |select Name,Path}|select-object * -ExcludeProperty RunspaceId,PSShowComputerName}
        $matchedHost=$allVms|?{$_.Name -like "*$vmName*"}
        if($matchedHost){
            return $matchedHost
        }else{
            write-host "'$vmName' is not found in cluster '$((get-cluster).Name)'"
            return $null
        }
    }catch{
        write-warning $_
        return $false
    }
}

findVmHost $vmName

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: An Exercise in Calculating Checksums

$out = new-object byte[] 1073741824; #1GB(new-object Random).NextBytes($out);[IO.File]::WriteAllBytes($dummyFile, $out);Measure-command{$hash=jacksum -a crc8 -x $dummyFile}write-host $hash# New ServerPS…

PowerShell: Enable CredSSP on Windows

PowerShell: # Enabled WinRMEnable-PSRemoting -Force# Enable CredSSPEnable-WSManCredSSP -Role Server -Force Legacy Command Line: # Enable…

Script to Disable User Accounts

accountsToDisable.txtorangeapplepeartermed user REM disableAccounts.batREM read accountsToDisable.txt and convert names into proper DN entries. Save result…