VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to get Datastore name

Hi,

I am trying to validate and get the name of the datastore, which has more free space, below script is not working,

I have two datastores, which has 800 GB and 400 GB free but I am not able to get datastore name with more free space from below.

please help

$intNewVMDiskSize = '250'

$oDatastoreWithMostFree = Get-Cluster MyClus | Get-Datastore | Sort-Object -Property {$_.FreeSpaceGB} -Descending:$true | where {$_.Name -NotMatch '^datastore1|^Datastore123|^MyDatastore3|^datastore_udev'} | Select-Object -ExpandProperty Name -First 1

if (($oDatastoreWithMostFree.FreespaceGB + 100) -lt $intNewVMDiskSize) {

    $($oDatastoreWithMostFree.Name)

}

else {

    "oh, no -- not enough freespace on datastore '$($oDatastoreWithMostFree.Name)' to provision new VM"

    return

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You only seem to select the Name property, while you use the FreespaceGB property as well later on.

Try like this

$intNewVMDiskSize = '250'

$oDatastoreWithMostFree = Get-Cluster MyClus |

    Get-Datastore |

    where {$_.Name -NotMatch '^datastore1|^Datastore123|^MyDatastore3|^datastore_udev'} |

    Sort-Object -Property {$_.FreeSpaceGB} -Descending:$true |

    Select-Object -First 1

  

if (($oDatastoreWithMostFree.FreespaceGB + 100) -lt $intNewVMDiskSize) {

    $($oDatastoreWithMostFree.Name)

}

else {

    "oh, no -- not enough freespace on datastore '$($oDatastoreWithMostFree.Name)' to provision new VM"

    return

}


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

You only seem to select the Name property, while you use the FreespaceGB property as well later on.

Try like this

$intNewVMDiskSize = '250'

$oDatastoreWithMostFree = Get-Cluster MyClus |

    Get-Datastore |

    where {$_.Name -NotMatch '^datastore1|^Datastore123|^MyDatastore3|^datastore_udev'} |

    Sort-Object -Property {$_.FreeSpaceGB} -Descending:$true |

    Select-Object -First 1

  

if (($oDatastoreWithMostFree.FreespaceGB + 100) -lt $intNewVMDiskSize) {

    $($oDatastoreWithMostFree.Name)

}

else {

    "oh, no -- not enough freespace on datastore '$($oDatastoreWithMostFree.Name)' to provision new VM"

    return

}


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

perfect...Thanks a lot as always... Smiley Happy

0 Kudos