Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8027739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:54:00+00:00 2026-06-04T23:54:00+00:00

I am writing a script to administer Hyper-VMs using the PowerShell Management Library for

  • 0

I am writing a script to administer Hyper-VMs using the PowerShell Management Library for Hyper-V.
Since we are using several Hyper-V Hosts and our VMs can change their host for performance reasons or other reasons I need a script that finds out which Host a VM runs on for the following functions.

This was my try at accomplishing this:

function IdentifyHost
    {
    param
    (
        [parameter(Position=0, Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $VM
    )

    [Array]$hosts=Get-VMHost

    if ($hosts.count -eq 0) 
    {
        Write-Warning "No valid hosts found."
    }

    for ([int]$i=0; $i -lt $hosts.count; $i++ )
    {
        try
        {
            $out = Get-VM -Name $VM -Server $hosts[$i] -ErrorAction Stop
        }

        catch [UnauthorizedAccessException]
        {
            Write-Warning "Access to $hosts[$i] denied."
        }

        if ($VM -is [String])
        {
            if ($out.VMElementName -eq $VM )
            {
                return $out.__SERVER
            }
        }

        elseif ($VM.ElementName -ne $null)
        {
            if ($out.VMElementName -eq $VM.VMElementName)
            {
                return $out.__SERVER
            }
        }

    }

    Write-Warning "No Host found for $VM"
}

Get-VMHost returns an array of all available Hyper-V hosts in the local area network.
My problem is that my function always returns the first element of the $hosts array whenever there is an UnauthorizedAccessException for the first element.

The plan is as following:
If the VM exists on the Host he will return a WMI Object representing that VM whose VMElementName property is equal to the VMs name given as parameter.
If the VM is given a WMI Object representing a VM the VMElementName properties of the two objects are equal.
If the VM does not exist on the Host he returns nothing.
If there’s an access issue it should be catched.

But somehow it doesn’t work out.

My question is this: What am I doing wrong in the code? And how can I fix it?

EDIT: The output of the function is the access problem warning for the first element of the $hosts array and then the first element of $hosts itself.

EDIT2: I fixed this myself by changing the return from the fragile $hosts[$i] to $out.__Server

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:54:02+00:00Added an answer on June 4, 2026 at 11:54 pm

    Okay so I found a possible way of solving this issue:

    Instead of returning the $hosts[$i] which yields unfavorable results I return the __Server property of $out, assuming there is a valid $out that matches the conditions.

    If any of you guys knows a better or cleaner way of doing this, please by my guest.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a script that can FTP files and folders using PHP functionality. I'm
Im writing a script in powershell which is supposed to connect to a MySQL
I'm writing a script that will grab several files and then copy them into
I'm writing a script to access all dynamic textfields in the library of a
I am writing a script using DBI to execute a select query to an
I am currently writing a script whereby an array of checkboxes are displayed using:
I'm writing a script in Ruby for work. Our company uses FileMaker Pro for
I’m writing a script to change the UUID of an NTFS partition (AFAIK, there
I am writing a script using jQuery to add multiple control boxes(divs) to a
I am writing a script for MS PowerShell. This script uses the Copy-Item command.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.