scripting to tell me if a disc is a VMWARE or Hyper-V disk

I can’t find any help to tell me how to modify my script to report if a drive is a vMWare or Hyper-V drive.

#---------- functions - Start.
function Where-OpticalDrives
{
<#
.SYNOPSIS
Describe purpose of “Where-OpticalDrives” in 1-2 sentences.

.DESCRIPTION
Add a more complete description of what the function does.

.PARAMETER InputObject
Describe parameter -InputObject.

.EXAMPLE
Where-OpticalDrives -InputObject Value
Describe what this call does

.NOTES
Place additional notes here.

.LINK
URLs to related sites
The first link is opened by Get-Help -Online Where-OpticalDrives

.INPUTS
List of input types that are accepted by this function.

.OUTPUTS
List of output types produced by this function.
#>

param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = ‘Data to filter’)]
[Object]$InputObject
)

process
{
if ($InputObject.DriveType -eq 5)
{
$InputObject
}
}
}

function Where-RemoteDrives
{
<#
.SYNOPSIS
Describe purpose of “Where-RemoteDrives” in 1-2 sentences.

.DESCRIPTION
Add a more complete description of what the function does.

.PARAMETER InputObject
Describe parameter -InputObject.

.EXAMPLE
Where-RemoteDrives -InputObject Value
Describe what this call does

.NOTES
Place additional notes here.

.LINK
URLs to related sites
The first link is opened by Get-Help -Online Where-RemoteDrives

.INPUTS
List of input types that are accepted by this function.

.OUTPUTS
List of output types produced by this function.
#>

param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = ‘Data to filter’)]
[Object]$InputObject
)
process
{
if ($InputObject.DriveType -eq 4)
{
$InputObject
}
}
}

function Where-LocalHDisks
{
<#
.SYNOPSIS
Describe purpose of “Where-LocalHDisks” in 1-2 sentences.

.DESCRIPTION
Add a more complete description of what the function does.

.PARAMETER InputObject
Describe parameter -InputObject.

.EXAMPLE
Where-LocalHDisks -InputObject Value
Describe what this call does

.NOTES
Place additional notes here.

.LINK
URLs to related sites
The first link is opened by Get-Help -Online Where-LocalHDisks

.INPUTS
List of input types that are accepted by this function.

.OUTPUTS
List of output types produced by this function.
#>

param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = ‘Data to filter’)]
[Object]$InputObject
)
process
{
if ($InputObject.DriveType -eq 3)
{
$InputObject
}
}
}

function Where-LocalDisks
{
<#
.SYNOPSIS
Describe purpose of “Where-LocalDisks” in 1-2 sentences.

.DESCRIPTION
Add a more complete description of what the function does.

.PARAMETER InputObject
Describe parameter -InputObject.

.EXAMPLE
Where-LocalDisks -InputObject Value
Describe what this call does

.NOTES
Place additional notes here.

.LINK
URLs to related sites
The first link is opened by Get-Help -Online Where-LocalDisks

.INPUTS
List of input types that are accepted by this function.

.OUTPUTS
List of output types produced by this function.
#>

param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = ‘Data to filter’)]
[Object]$InputObject
)
process
{
if ($InputObject.DriveType -eq 2)
{
$InputObject
}
}
}
#---------- functions - End.

#---------- Configurations - Start.

$cre = get-Credential # This user account should be Adminstrator.

$Servers = Get-Content -Path ‘list.txt’ # Replace this with your TXT file containg Server names.

Do not change below this line.

$OS = (Get-WmiObject -Class win32_operatingsystem).caption
$MediaType = Get-WmiObject -Class MSFT_PhysicalDisk -Namespace root\Microsoft\Windows\Storage

$HVDrive = Get-VMHardDiskDrive | Where-Object { $_.DriveType -eq 1 }

$LogicalDisks = Get-WmiObject -Class Win32_LogicalDisk | Where-LocalDisks
$LocalHDisks = $LogicalDisks | Where-LocalHDisks
$RemoteDrives = Get-WmiObject -Class Win32_LogicalDisk | Where-RemoteDrives
$OpticalDrives = Get-WmiObject -Class Win32_CDRomDrive | Where-OpticalDrives

$HVDrive = Get-VM | Select-Object -ExpandProperty VMID | Get-VHD | Select-Object -ExpandProperty VhdFormat

Do not change above this line.

$Records = 1000 # Sets Max Records
$CSV = “DiskSpaceReport_$(((Get-Date).ToUniversalTime()).ToString(‘yyyy-MM-dd’)).csv” # File to export to.
#---------- Configurations - End.
#---------- The report - Start.
Write-Verbose -Message " Your Report started at: $(((Get-Date).ToUniversalTime()).ToString(‘yyyy-MM-dd’))."
#---------- Wright to File - Start.
#Get-WmiObject Win32_LogicalDisk -filter ‘DriveType=3’ -computerName $servers | ## use this if you want to show only Drive Type 3 drives.
Get-WmiObject -Class Win32_LogicalDisk -ComputerName $Servers | Select-Object ` -Property SystemName, DeviceID, VolumeName,
@{N = ‘Size(MB)’ ;E = {’{0:N2}’ -f ($.Size/1MB)}},
@{N = ‘FreeSpace(MB)’ ;E = {’{0:N2}’ -f ($
.FreeSpace/1MB)}},
@{N = ‘Used(MB)’ ;E = {’{0:N2}’ -f (($.Size-$.FreeSpace)/1MB)}},
@{N = ‘PercentFree’ ;E = {’{0:P2}’ -f ($.FreeSpace/$.Size)}},
@{N = ‘PercentUsed’ ;E = {’{0:P2}’ -f (($.Size-$.FreeSpace)/$.Size)}},
@{N = ‘Drive Type’ ;E = {switch ($
.DriveType) { 0{’(unknown)’} 2{‘USB/FD’} 3{‘HDD’} 4{‘SSD’} 5{‘CD/DVD/BD’} 6{‘RAM disk’}}}},
@{N = ‘VM Drv’ ;E = {if($hvdrive) {$VhdFormat} else{‘no VM Disk’}}},
@{N = ‘Operating System’ ;E = {$OS}},
@{N = ‘Free Space (Warning)’ ;E = {if($
.FreeSpace/$_.Size -lt .10){‘Below 10%’}else{‘GOOD’}}} | Export-Csv -Path $CSV -NoTypeInformation -Encoding UTF8 | Format-Table -AutoSize
#---------- Wright to File - END.

#---------- Wright to Screen - Start.
#Get-WmiObject Win32_LogicalDisk -filter ‘DriveType=3’ -computerName $servers | ## use this if you want to show only Drive Type 3 drives.
Get-WmiObject -Class Win32_LogicalDisk -ComputerName $Servers | Select-Object ` -Property SystemName, DeviceID, VolumeName,
@{N = ‘Size(MB)’ ;E = {’{0:N2}’ -f ($.Size/1MB)}},
@{N = ‘FreeSpace(MB)’ ;E = {’{0:N2}’ -f ($
.FreeSpace/1MB)}},
@{N = ‘Used(MB)’ ;E = {’{0:N2}’ -f (($.Size-$.FreeSpace)/1MB)}},
@{N = ‘PercentFree’ ;E = {’{0:P2}’ -f ($.FreeSpace/$.Size)}},
@{N = ‘PercentUsed’ ;E = {’{0:P2}’ -f (($.Size-$.FreeSpace)/$.Size)}},
@{N = ‘Drive Type’ ;E = {switch ($
.DriveType) { 0{’(unknown)’} 2{‘USB/FD’} 3{‘HDD’} 4{‘SSD’} 5{‘CD/DVD/BD’} 6{‘RAM disk’}}}},
@{N = ‘VM Drv’ ;E = {if($hvdrive) {$VhdFormat} else{‘no VM Disk’}}},
@{N = ‘Operating System’ ;E = {$OS}},
@{N = ‘Free Space (Warning)’ ;E = {if($
.FreeSpace/$_.Size -lt .10){‘Below 10%’}else{‘GOOD’}}} | Export-Csv -Path $CSV -NoTypeInformation -Encoding UTF8 | Format-Table -AutoSize Write-Output | Format-Table
#---------- Wright to Screen - END.

It’ll be bit difficult to read and understand unformatted code in the forums, can you format the code. You can edit the same post by following documentation in below link.

https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did/

Why over engineer this?
Why not just look at the hard drive extension?
Hyper-V are .vhd /.vhdx and VMware is .vmdx

[quote quote=160088]Why over engineer this?

Why not just look at the hard drive extension?

Hyper-V are .vhd /.vhdx and VMware is .vmdxhow would I do that without having to re-wright a bunch of the code? can you point me in the right direction or link me to an example?

 

Thank you for your reply.

[/quote]

As for this…

how would I do that without having to re-wright a bunch of the code?

… there is zero reason to do so. This is basic PowerShell filesystem lookup stuff.

Just use file system cmdlets. Each VM is just a file.
Just use Get-ChildItem and get the extension and use an if or try/catch statement to get the extension.

If ((Get-ChildItem -Path 'E:\Vhd').Extension -match 'vhd|vhdx')
{'Hyper-V disk found'}
Else {'VMware disk found'}

try 
{ 
    (Get-ChildItem -Path 'E:\Vhd').Extension -match 'vhd|vhdx' 
    'Hyper-V disk found'
}
catch {'VMware disk found'}

Or there are cmdlets from Microsoft and VMWare for this.

Get-VM
Get-VHD
Get-HardDisk # you need the VMWare cmdlets installed for this to work
             # https://pubs.vmware.com/vsphere-6-5/index.jsp?topic=%2Fcom.vmware.powercli.cmdletref.doc%2Findex.html

# Get a list of all functions for the specified name
Get-Command -Name '*vhd*' -CommandType Function | 
Out-GridView -PassThru -Title 'Available named functions'

# Get a list of all commandlets for the specified name
Get-Command -Name '*vhd**'  -CommandType Cmdlet | 
Out-GridView -PassThru -Title 'Available named cmdlet'

# get function / cmdlet details
Get-Command -Name Get-Vhd -Syntax
(Get-Command -Name Get-Vhd).Parameters.Keys

Get-help -Name Get-Vhd -Full
Get-help -Name Get-Vhd -Online
Get-help -Name Get-Vhd -Examples

# Results
 Get-VHD -Path c:\test\testvhdx.vhdx
 Get-VHD -DiskNumber 6
 Get-VM -VMName TestVM | Select-Object VMId | Get-VHD
 Get-VM -VMName testvm | Select-Object vmid | Get-VHD
 Get-ChildItem c:\test -Recurse |% {$_.FullName} | Get-VHD -ErrorAction SilentlyContinue

 
 # using VMWare PowerShell cmdlets
 Get-VM | Get-HardDisk | Get-Member
   TypeName: VMware.VimAutomation.ViCore.Impl.V1.VirtualDevice.FlatHardDiskImpl

Name             MemberType Definition
----             ---------- ----------
ConvertToVersion Method     T VersionedObjectInterop.ConvertToVersion[T]()
Equals           Method     bool Equals(System.Object obj)
GetHashCode      Method     int GetHashCode()
GetType          Method     type GetType()
IsConvertableTo  Method     bool VersionedObjectInterop.IsConvertableTo(type type)
LockUpdates      Method     void ExtensionData.LockUpdates()
ToString         Method     string ToString()
UnlockUpdates    Method     void ExtensionData.UnlockUpdates()
CapacityGB       Property   decimal CapacityGB {get;}
CapacityKB       Property   long CapacityKB {get;}
Client           Property   VMware.VimAutomation.ViCore.Interop.V1.VIAutomation Client {get;}
ConnectionState  Property   VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.ConnectInfo ConnectionState 
...