VMware Cloud Community
Raghukalloor
Contributor
Contributor

I have a shared datastore which has to be mounted across all hosts

Normally I used to login to each esxi host and run the below command
esxcfg-volume -M xxxxx(datastorename). Is there any way to combine this command with powercli to run on clusters or all esxi hosts. Please assist

Reply
0 Kudos
18 Replies
LucD
Leadership
Leadership

Reply
0 Kudos
Raghukalloor
Contributor
Contributor

Hi Lucd,

Nice to see you back,
Sorry the link which you have posted doesn't seems to open. Could you please post the script here if its handy.. will be really helpful
1.first connect to esxi
2.run the command esxcfg-volume -M xxxx(datastorename)
3. exit from esxi

Reply
0 Kudos
LucD
Leadership
Leadership

That link works for me.

So you still want to use the esxcfg-volume command, but then from PowerCLI?


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

Reply
0 Kudos
Raghukalloor
Contributor
Contributor

Hi Lucd,

Am not sure could be my browser issue or some other..

yeah exactly the same.. need to use esxcfg-volume -M command with powercli for multiple hosts

Or even alternate commands equivalent to esxcfg-volume -M

Reply
0 Kudos
LucD
Leadership
Leadership

Then you could use the Posh-SSH module to set up an SSH session and via that session give the command.
See my Use Posh-SSH instead of PuTTY dive.


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

Reply
0 Kudos
Raghukalloor
Contributor
Contributor

I would try using POSH-SSH.
Can we use esxcli commands to do this activity

Reply
0 Kudos
LucD
Leadership
Leadership

Yes, you can.
Use Get-EsxCli and then the call the mount method

 

$dsName = 'MyDS'

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
    $esxcli = Get-EsxCli -VMHost $esx -V2
    $esxcli.storage.filesystem.mount.Invoke(@{volumelabel=$dsName})
}

 


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

Reply
0 Kudos
Raghukalloor
Contributor
Contributor

Hi Lucd,

When i used the below ESXCLI script for mounting the DS getting the below error

"Index( zero based) must be greater than or equal to zero and less than the size of the argument". Can this be sorted.

Reply
0 Kudos
LucD
Leadership
Leadership

How did you run that?


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

Reply
0 Kudos
Raghukalloor
Contributor
Contributor

 

$dsName = 'MyDS'

Get-VMHost -Name xxxx -PipelineVariable esx |
ForEach-Object -Process {
    $esxcli = Get-EsxCli -VMHost $_ -V2
    $esxcli.storage.filesystem.mount.Invoke(@{volumelable=$dsName})
}

 

Hi Lucd,

I ran in the above method by providing DS name and individual host name and save it as.ps1

Reply
0 Kudos
LucD
Leadership
Leadership

That should be volumelabel instead of volumelable


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

Reply
0 Kudos
Raghukalloor
Contributor
Contributor

i have corrected that as well and tried.. same result

Reply
0 Kudos
LucD
Leadership
Leadership

Can you show me the actual script you're using (not a copy of my code)?
And a screenshot of the run.


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

Reply
0 Kudos
Raghukalloor
Contributor
Contributor

I used the same script below and added datastore name and esx name and changed volume label..thats all

$dsName = 'MyDS'
Get-VMHost -Name xxxxx(esxname) -PipelineVariable esx |
ForEach-Object -Process {
$esxcli = Get-EsxCli -VMHost $esx -V2
$esxcli.storage.filesystem.mount.Invoke(@{volumelabel=$dsName})
}

 

below is the screenshot attached

Reply
0 Kudos
Raghukalloor
Contributor
Contributor

Even this command gives me the proper output with ds name which needs to be mapped.

$esxcli.storage.vmfs.snapshot.list.invoke()

Reply
0 Kudos
LucD
Leadership
Leadership

You don't seem to be running this from a .ps1 file but line-by-line from the PS prompt.
That would mean the pipeline variable is not working.
If you can't give me the complete run you did, I'm afraid I can't help you


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

Reply
0 Kudos
Raghukalloor
Contributor
Contributor

Hi Lucd,

Am running from a PS prompt. Initially I followed ur one of the blog which has the below command to pull the DS list which has to be mounted

$esxcli.storage.vmfs.snapshot.list.invoke() --> with this i was able to get the output of 2 DS which has to be mounted
$esxcli.storage.vmfs.snapshot.mount.invoke(@{volumelabel=$dsName}) --> post I tried with this and it worked mounting the DS to the host

Thanks for this..

I am having 100+ esxi host and am planning to run the below script. How can i add one more DS name in this.Pls comment or modify if anything needed.
=======

$esxcli = Get-EsxCli -VMHost (get-content c:\iso\hostnames.txt) - V2
$esxcli.storage.vmfs.snapshot.mount.invoke(@{volumelabel=$dsName})

and how can i pull the report which are the hosts got mapped in it

Reply
0 Kudos
LucD
Leadership
Leadership

That is basically the same as the earlier solution I gave.
It adds the creation of an object for each ESXi node, which is then at the end saved to a CSV file.
Run this in a .ps1 file, not line-by-line from a PS prompt.

$dsName = 'your-DS-name'

Get-VMHost -Name (Get-Content -Path c:\iso\hostnames.txt) -PipelineVariable esx |
ForEach-Object -Process {
    $esxcli = Get-EsxCli -VMHost $esx - V2
    $esxcli.storage.vmfs.snapshot.mount.invoke(@{volumelabel=$dsName})
    New-Object -TypeName PSObject -Property @{
        VMHost = $esx.Name
        Datastore = $dsName
    }
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

 


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

Reply
0 Kudos