-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-VMGeneology.ps1
131 lines (108 loc) · 3.64 KB
/
Get-VMGeneology.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<#
.SYNOPSIS
Gathers current 'geneology' of a VM.
.DESCRIPTION
Gathers current 'geneology' of a VM, which gathers and returns the current host the VM is running on, the current datastore, the current
cluster, the number of hosts in the cluster, the current virtual port group and VLAN and the current vSwitch
.PARAMETER Name
Display name of VM guest
.PARAMETER vCenterServer
vCenter server name
.EXAMPLE
.\Get-VMGeneology.ps1 -Name testvm1 -Verbose
.EXAMPLE
.\Get-VMGeneology.ps1 -Name testvm1 -vCenterServer viserver1.company.com -Verbose
.EXAMPLE
Get-VM testvm1 | .\Get-VMGeneology.ps1
VM : VISERVER01
CurrentHost : esxi01.company.com
CurrentCluster : Prod
NumHostsInCluster : 5
CurrentDatastore : filer1_fc_lun_1
PortGroup : Prod_192.168.1.x_VLAN_10
PortGroupVLAN : 10
vSwitch : vSwitch1
.NOTES
9/27/14 K. Kirkpatrick Created
TODO: [+] Add full support for supplying multiple VMs
GitHub: https://github.com/vScripter
Twitter: @vScripter
Email: [email protected]
[-------------------------------------DISCLAIMER-------------------------------------]
All script are provided as-is with no implicit
warranty or support. It's always considered a best practice
to test scripts in a DEV/TEST environment, before running them
in production. In other words, I will not be held accountable
if one of my scripts is responsible for an RGE (Resume Generating Event).
If you have questions or issues, please reach out/report them on
my GitHub page. Thanks for your support!
[-------------------------------------DISCLAIMER-------------------------------------]
.LINK
https://github.com/vScripter
#>
[cmdletbinding(PositionalBinding = $true)]
param (
[parameter(Mandatory = $true,
HelpMessage = "Enter display name of VM ",
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ParameterSetName = "Name",
Position = 0)]
[alias("VM")]
$Name,
[parameter(Mandatory = $false, Position = 1, ValueFromPipeline = $true)]
[alias("VC")]
[string]$vCenterServer
)
BEGIN {
# define variables/functions
#Requires -Version 3
$colFinalResults = @()
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
} # end BEGIN
PROCESS {
foreach ($vm in $Name) {
try {
<#
- get vm and store result into variable
- use Get-View to call the config attribute in order to access child values
- Get VMHost where guest is currently running
- Use Get-ViewType to store cluster detail of current host
- Grab current virtual port group the guest is using
- Grab current vSwitch the VM is running on
#>
$getvm = $null
$vmconfig = $null
$vihost = $null
$HostDetail = $null
$ClusterDetail = $null
$currentvpg = $null
$CurrentVswitch = $null
$objVMGenes = @()
Write-Verbose -Message "Querying for guest geneology"
$GetVM = Get-VM -Name $vm
$VMConfig = ($GetVM | Get-View).config
$VIHost = $GetVM | Get-VMHost
$ClusterDetail = $VIHost | Get-Cluster | Get-View
$CurrentVPG = $GetVM | Get-VirtualPortGroup
$CurrentVswitch = $GetVM | Get-VirtualSwitch
$objVMGenes = [PSCustomObject] @{
VM = $VMConfig.Name
CurrentHost = $GetVM.VMHost
CurrentCluster = $ClusterDetail.Name
NumHostsInCluster = $ClusterDetail.host.count
CurrentDatastore = $VMConfig.datastoreurl.name
PortGroup = $CurrentVPG.Name
PortGroupVLAN = $CurrentVPG.VLanId
vSwitch = $CurrentVswitch.Name
} # end objVMGenes
$colFinalResults += $objVMGenes
} catch {
Write-Warning -Message "$_"
} # end try/catch
} # end foreach $vm in $name
} # end PROCESS
END {
Write-Verbose -Message "Calling final results"
$colFinalResults
} # end END