public/Get-PspVMInfo.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Function Get-PspVMInfo {
    <#
    .SYNOPSIS
    Gets virtualization information for local or remote machines.
 
    .DESCRIPTION
    Gets virtualization information for local or remote machines.
    Will try and access the required data through a PowerShell remoting session, but in case this fails reverts to RemoteRegistry.
    This does however require RemoteRegistry to be enabled on the machine.
 
    .PARAMETER ComputerName
    Provide the computername(s) to query.
    Using this parameter will create a temporary PSSession to obtain the information if available.
    If PowerShell remoting is not available, it will try and obtain the information through ADSI.
    Default value is the local machine.
 
    .PARAMETER Credential
    Provide the credentials for the PowerShell remoting session to be created if current credentials are not sufficient.
 
    .PARAMETER PSSession
    Provide the PowerShell remoting session object to query if this is already available.
    Once the information has been gathered, the PowerShell session will remain available for further use.
 
    .EXAMPLE
    PS C:\> Get-PspVMInfo -ComputerName CONTOSO-SRV01,CONTOSO-WEB01,CONTOSO-APP01 | Format-List
 
    ComputerName : CONTOSO-SRV01
    HVIntegrationServicesRunning : True
    HVHostName : hv01.contoso.com
    HVVMName : HV-VM01
    VMWareToolsRunning : False
    InventoryDate : 3/16/2019 1:44:10 AM
 
    ComputerName : CONTOSO-WEB01
    HVIntegrationServicesRunning : True
    HVHostName : hv01.contoso.com
    HVVMName : HV-VM02
    VMWareToolsRunning : False
    InventoryDate : 3/16/2019 1:44:10 AM
 
    ComputerName : CONTOSO-APP01
    HVIntegrationServicesRunning : True
    HVHostName : hv02.contoso.com
    HVVMName : HV-VM12
    VMWareToolsRunning : False
    InventoryDate : 3/16/2019 1:44:10 AM
 
    Gets the virtualization information for CONTOSO-SRV01, CONTOSO-WEB01 and CONTOSO-APP01.
 
    .NOTES
    Name: Get-PspVMInfo.ps1
    Author: Robert Prüst
    Module: PSP-Inventory
    DateCreated: 16-03-2019
    DateModified: 16-03-2019
    Blog: http://powershellpr0mpt.com
 
    .LINK
    http://powershellpr0mpt.com
    #>


    [OutputType('PSP.Inventory.VMInfo')]
    [Cmdletbinding(DefaultParameterSetName = 'Computer')]
    param(
        [Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'Computer')]
        [ValidateNotNullorEmpty()]
        [Alias('CN')]
        [String[]]$ComputerName = $env:COMPUTERNAME,
        [Parameter(ParameterSetName = 'Computer')]
        [PSCredential]$Credential,
        [Parameter(Position = 0, ValueFromPipeline = $true, ParameterSetName = 'Session')]
        [Alias('Session')]
        [System.Management.Automation.Runspaces.PSSession[]]$PSSession
    )
    process {
        if ($PSCmdlet.ParameterSetName -eq 'Computer') {
            $PSSession = @()
            $SessionProperties = @{
                ErrorAction  = 'Stop'
                Computername = ''
            }
            if ($Credential.Username) {
                $SessionProperties.Add('Credential', $Credential)
            }
            foreach ($Computer in $ComputerName) {
                $Computer = $Computer.toUpper()
` $SessionProperties.ComputerName = $Computer
                Try {
                    $PSSession += New-PSSession @SessionProperties
                }
                catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
                    Write-Warning "[$Computer] - Unable to open PS Remoting session. Reverting to Remote Registry"
                    try {
                        $HVService = Get-Service -ComputerName $Computer -DisplayName '*Hyper-V*' -ErrorAction SilentlyContinue | Where-Object {$_.Status -eq 'Running'}
                        $VMService = Get-Service -ComputerName $Computer -Name VMTools -ErrorAction SilentlyContinue
                        $HVHostName = $null
                        $HVVMName = $null
                        $Path = @("SOFTWARE\\Microsoft\\Virtual Machine\\Guest\\Parameters")
                        Write-Verbose "[$Computer] - Checking Registry Path: $Path"
                        $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Computer, 'Registry64')
                        try {
                            Write-Verbose "[$Computer] - Checking Registry Key: $Path"
                            $regkey = $reg.OpenSubKey($Path)
                            try {
                                $HVHostName = $regkey.getValue("PhysicalHostNameFullyQualified")
                                $HVVMName = $regkey.getValue("VirtualMachineName")
                            }
                            catch {
                                Write-Warning "[$Computer] - Unable to access key: $Path "
                            }
                        }
                        catch {}
                        $reg.Close()
                        
                        [PSCustomObject]@{
                            PSTypeName                   = 'PSP.Inventory.VMInfo'
                            ComputerName                 = $Computer
                            HVIntegrationServicesRunning = if ($HVService) {$true}else {$false}
                            HVHostName                   = $HVHostName
                            HVVMName                     = $HVVMName
                            VMWareToolsRunning           = if ($VMService) {$true}else {$false}
                            InventoryDate                = (Get-Date)
                        }
                
                    }
                    catch {
                        Write-Error "[$Computer] - Unable to open Remote Registry"
                        Continue
                    }

                }
                catch {
                    Write-Warning "[$Computer] - cannot be reached. $($_.Exception.Message)"
                }
            }
        }
        foreach ($Session in $PSSession) {
            _GetVMInfo -PSSession $Session
        }
    }
    End {
        if ($PSCmdlet.ParameterSetName -eq 'Computer' -AND $PSSession.count -gt 0) {
            Remove-PSSession $PSSession
        }
    }
}