VMware Cloud Community
koskelax9
Contributor
Contributor
Jump to solution

get-view datastore and get-datastore in function

Hey, my first time posting here.

Creating  a function that takes input from pipeline and processes each input  through process in a function. I want the function to be able to take  both "get-view -viewtype datastore" and "get-datastore" as input.

Gotten as far as getting the get-view and the  get-datastore working separatly in the function, but when I want the  function to be able to take either one as input I can't make it work.

Here's the function as it is now, not really working for any of the inputs.

function Get-DatastoreSpace () {
[cmdletbinding()]
param (
[Parameter(Mandatory=$true,
ValueFromPipeline=$true)]
$Datastore
)

BEGIN {$ReportDatastore = @()}
      
PROCESS {       
        
         $row = "" | Select-Object Datastore_name, Datastore_Freespace_GB, Datastore_type, Datastore_Capacity_GB
         $row.Datastore_name = $_.name
         $row.Datastore_type = $_.type
         $row.Datastore_Capacity_GB = [math]::Round($_.Capacity/1GB,3)
         $row.Datastore_Freespace_GB = [math]::Round($_.Freespace/1GB,3)
        
         $ReportDatastore += $row

}

END {$ReportDatastore}

}

Any ideas on how to do this?

Thanks in advance

Eirik

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Check the type of the object in the $datastore variable.

Something like this

if($datastore -is [VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.VmfsDatastoreImpl]){
# From Get-Datastore
}
elseif($datastore -is [VMware.Vim.Datastore]){
# From Get-View -ViewType Datastore
}
else{
# Wrong object type
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Check the type of the object in the $datastore variable.

Something like this

if($datastore -is [VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.VmfsDatastoreImpl]){
# From Get-Datastore
}
elseif($datastore -is [VMware.Vim.Datastore]){
# From Get-View -ViewType Datastore
}
else{
# Wrong object type
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
koskelax9
Contributor
Contributor
Jump to solution

Thanks! works like a charm.

Changed the first line to this so it also takes NasDatastore, and it works perfectly.

if ($datastore -is [VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.VmfsDatastoreImpl] -or $datastore -is [VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.NasDatastoreImpl])

0 Kudos