With the below code, I can currently attach and create a new data store from the following code.
#Create a DataStore from the newly attach NetApp storage
#Get-VMHostStorage $currentHost -RescanAllHba -RescanVmfs
$lunpath = Get-VMHost | Select -First 1 | Get-ScsiLun | Where { $_.Vendor -match "NETAPP"} | Select -First 1
Get-VMHost | Select -first 1 | New-Datastore -Vmfs -Path $lunpath.CanonicalName -Name VMStorage
I have since decided to add a total of three datastores that are presented via iSCSI from my NetApp. I would like to name them ds1,ds2 and ds3. Could someone please help me with the logic on how to loop through these presented disks and create the datastores? I was thinking I could do something like..
#Create a DataStore from the newly attach NetApp storage
#Get-VMHostStorage $currentHost -RescanAllHba -RescanVmfs
$ds = "ds1"
$lunpath = Get-VMHost | Select -First 1 | Get-ScsiLun | Where { $_.Vendor -match "NETAPP"} | Select -First 3
for each ($lunpath)
Get-VMHost | Select -first 1 | New-Datastore -Vmfs -Path $lunpath.CanonicalName -Name $ds + 1
I know this is very rough...but I am not sure. I need to know how to increment the datastore name as well as loop through the presented disks.
Thanks in advance
There was a -WhatIf parameter on the New-Datastore cmdlet to show you what would be done.
To actually create the datastore you need to remove the -WHatIf parameter.
It seems the forum SW again had problems with the square brackets in the lines.
I attach the script.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Perhaps try something like this
#Create a DataStore from the newly attach NetApp storage $dsBaseName = "ds" $esx = Get-VMHost | Select -First 1 $lunpaths = $esx | Get-ScsiLun | where { $_.Vendor -match "NETAPP"} | Select -First 3 1..3 | %{ New-Datastore -VMHost $esx -Vmfs -Path $lunpaths[$_].CanonicalName -Name ($dsBaseName + $_) -WhatIf }
The .. operator produces a series from the first to last number. In this case 1,2,3
The name of a datastore is a string. We concatenate the base name with the number.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I have tried your suggestion and I am getting a path error. It seems to me that sometimes this works and other times it bombs out. Here is the input and output.
[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> 1.. | %{ New-Datastore -VMHost $esx -Vmfs -Path $lunpaths[$_].CanonicalName -Name $dsBaseName + $_) -WhatIf} What if: Performing operation 'Creating new datastore' on VMHost '192.168.120.5 ' What if: Performing operation 'Creating new datastore' on VMHost '192.168.120.5 ' New-Datastore : Cannot validate argument on parameter 'Path'. The argument is n ull or empty. Supply an argument that is not null or empty and then try the com mand again. At line:1 char:49 + 1..3 | %{ New-Datastore -VMHost $esx -Vmfs -Path <<<< $lunpaths[$_].Canonica lName -Name ($dsBaseName + $_) -WhatIf} + CategoryInfo : InvalidData: (:) [New-Datastore], ParameterBindi ngValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom ation.VimAutomation.Commands.Host.NewDatastore [vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI>
There was a -WhatIf parameter on the New-Datastore cmdlet to show you what would be done.
To actually create the datastore you need to remove the -WHatIf parameter.
It seems the forum SW again had problems with the square brackets in the lines.
I attach the script.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Thanks again for all your help. You have been a great resource for me. Your scripting is working for the first two datastores, but the third I am getting the path error. Please see below. Any help on this would be great. You can see on the bottom the disks I am trying to add.
[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> 1..3 | %{ New-Datastore -VMHost $esx -Vmfs -Path $lunpaths[$_].CanonicalName -Name ( $dsBaseName + $_) } Name FreeSpaceMB CapacityMB ---- ----------- ---------- ds1 613836 614400 ds2 613836 614400 New-Datastore : Cannot validate argument on parameter 'Path'. The argument is n ull or empty. Supply an argument that is not null or empty and then try the com mand again. At line:1 char:49 + 1..3 | %{ New-Datastore -VMHost $esx -Vmfs -Path <<<< $lunpaths[$_].Canonica lName -Name ($dsBaseName + $_) } + CategoryInfo : InvalidData: (:) [New-Datastore], ParameterBindi ngValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom ation.VimAutomation.Commands.Host.NewDatastore [vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> $lun paths CanonicalN ConsoleDeviceName LunType CapacityMB MultipathPolicy ame ---------- ----------------- ------- ---------- --------------- naa.60a... /vmfs/devices/disks/naa.60a... disk 614478 Fixed naa.60a... /vmfs/devices/disks/naa.60a... disk 614478 Fixed naa.60a... /vmfs/devices/disks/naa.60a... disk 614478 Fixed [vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI>
My bad, the loop 1..3 provides the values 1,2,3.
But the index into the $lunPaths array should be 0..2 or 0,1,2
Attached a corrected script
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference