VMware Cloud Community
admin
Immortal
Immortal
Jump to solution

Return Datastore ID

I am trying to write a script that returns the datastore ID. For example

____________________________________________________________

$datastore = Get-Datastore | Where-Object {$_.Name -notlike 'local'}

ForEach ($ds in $datastore) {

$Id = $ds | select Id

Write-Host $Id

}

____________________________________________________________

The output is:

@{Id=Datastore-datastore-15}

I would like the output to be either the number 15 or datastore-15. I am sure there is some string manipulation I can do just need to figure that out, but what I was also wondering is there a way to return this information without having to manipulate the output?

0 Kudos
1 Solution

Accepted Solutions
ykalchev
VMware Employee
VMware Employee
Jump to solution

If you're using PowerCLI 4.1 you can do that :

$datastore = Get-Datastore | Where-Object {$_.Name -notlike '*local*'}
ForEach ($ds in $datastore) {
   $dsMoRef = New-Object VMware.Vim.ManagedObjectReference -ArgumentList $ds.Id
   Write-Host $dsMoRef.Value
}

Regards,

Yasen Kalchev

PowerCLI Dev Team

Yasen Kalchev, vSM Dev Team

View solution in original post

0 Kudos
4 Replies
ykalchev
VMware Employee
VMware Employee
Jump to solution

If you're using PowerCLI 4.1 you can do that :

$datastore = Get-Datastore | Where-Object {$_.Name -notlike '*local*'}
ForEach ($ds in $datastore) {
   $dsMoRef = New-Object VMware.Vim.ManagedObjectReference -ArgumentList $ds.Id
   Write-Host $dsMoRef.Value
}

Regards,

Yasen Kalchev

PowerCLI Dev Team

Yasen Kalchev, vSM Dev Team
0 Kudos
admin
Immortal
Immortal
Jump to solution

Thank you it worked just like I wanted.

0 Kudos
admin
Immortal
Immortal
Jump to solution

Yasen,

Thank you, it returned exactly what I was looking for.

0 Kudos
RyanGreeley
Contributor
Contributor
Jump to solution

You can also try

Get-Datastore | Where-Object {$_.Name -notlike 'local'} | select -ExpandProperty ID

0 Kudos