Posted On November 24, 2021

PowerShell: Find Hyper-V Host by Guest VM Name

kimconnect 0 comments
blog.KimConnect.com >> Codes , Virtualization >> PowerShell: Find Hyper-V Host by Guest VM Name
# findVmHostByGuestName.ps1

$vmName='TESTVM'

function findVmHostByGuestName($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
    }
}

findVmHostByGuestName $vmName

Leave a Reply

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

Related Post

Set File Permissions Recursively – Running on File Server

What problems does this solve? This error: "Failed to enumerate objects in the container. Access…

PowerShell: How To Set IP and Domain Restrictions to Specific IIS Sites

# Enable IP Filtering Feature in IIS using PowerShell Install-WindowsFeature Web-IP-Security Restart-Service W3SVC # Optional:…

Script to use WinRAR for backups

REM winrar_backup.batecho offSET FNAME=KIMCONNECTSET LOGFILE=\\FILESERVER01\backup\ads-02-backup.txtIF EXIST V:\ (NET USE V: /DELETE)net use v: \\FILESERVER01\backupIF %ERRORLEVEL%…