VMware Cloud Community
PBeebe
Contributor
Contributor

Foreach statement fails for new-datastore

Hi there,

Currently trying to attach NFS datastores to a freshly installed ESXi host using the following foreach statement below:

foreach ($Datastore in (Get-vmhost source-esx.domain.com | Get-Datastore | where {$_.Type -eq "nfs" -and $_.Accessible -eq "true"})) {New-Datastore -Nfs -VMHost dest-esx.domain.com -Name $Datastore.Name -Path $Datastore.RemotePath -NfsHost $Datastore.RemoteHost}

The command failes with "Value cannot be found for the mandatory parameter VMHost" within the "New-Datastore" portion of the command.  Running PowerCLI 5.5 R1 Build 1295336.

Any suggestions on how to get around this error would be much appreciated!

~Pete

Reply
0 Kudos
3 Replies
PBeebe
Contributor
Contributor

I'm unsure of why the original wouldn't work however breaking it down to the following allowed me to successfully run the script:

$Datastore = (Get-vmhost source-esx.domain.com | Get-Datastore | where {$_.Type -eq "nfs" -and $_.Accessible -eq "true"})

foreach ($objItem in $Datastore){New-Datastore -Nfs -VMHost dest-esx.domain.com-Name $objItem.Name -Path $objItem.RemotePath -NfsHost $objItem.RemoteHost}

~Pete

Reply
0 Kudos
LucD
Leadership
Leadership

Does this actually return a VMHost object ?

Get-VMHost -Name dest-esx.domain.com


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

Reply
0 Kudos
PBeebe
Contributor
Contributor

Hi LucD,

Yes, running just that snippet returns the VMhost information.  I just posted above this with my work-around that did the trick for me.

Thanks!

~Pete

Reply
0 Kudos