PoshCode Archive  Artifact [66dd921f06]

Artifact 66dd921f060cf684484bc55cde4ffc4174a4b41151c60a990262ac95f66ebcd5:

  • File Test-VM.ps1 — part of check-in [ad7e23bc4b] at 2018-06-10 13:26:21 on branch trunk — This function is prepared for Hyper-V 3.0 since we don’t have a native cmdlet in the module. (user: Huajun Gu size: 1588)

# encoding: ascii
# api: powershell
# title: Test-VM
# description: This function is prepared for Hyper-V 3.0 since we don’t have a native cmdlet in the module.
# version: 0.1
# type: function
# author: Huajun Gu
# license: CC0
# function: Test-VM
# x-poshcode-id: 3482
# x-archived: 2012-06-30T22:15:04
# x-published: 2012-06-25T17:40:00
#
#
Function Test-VM
{
    [cmdletbinding()]
    Param
    (
        [Parameter(Mandatory=$true,Position=1)]
        [string[]]$Name,
        [Parameter(Mandatory=$true,Position=2)]
        [string[]]$ComputerName
    )
    Process
    {
        $results = @()
        foreach ($cName in $ComputerName) {
            foreach ($vName in $Name) {
                $result = New-Object System.Management.Automation.PSObject
                Try
                {
                    $vm = Get-VM -ComputerName $cName -Name $vName -ErrorAction Stop
                }
                Catch
                {
                    #Display an error message
                }
                if ($vm -ne $null) {
                    $Existence = $true
                } else {
                    $Existence = $false
                }				
                $result | Add-Member -NotePropertyName ComputerName -NotePropertyValue $cName
                $result | Add-Member -NotePropertyName Name -NotePropertyValue $vName
                $result | Add-Member -NotePropertyName Existence -NotePropertyValue $Existence
                $results += $result
            }
        }
        return $results
    }
}