Automating Datastore Creation with PowerShell

Creating Multiple Datastores Using PowerCLI Script

In one of our recent projects, we had a requirement to create multiple datastores using a PowerCLI script. We were presented with a list of approximately 60 LUNs, and we needed to create a datastore for each one on all ESXi hosts in the cluster. In this blog post, I will discuss how we achieved this using a PowerCLI script.

Requirements

————

Before we begin, let me outline the requirements of the script:

1. **InvalidCertificateAction**: We set the InvalidCertificateAction to Ignore to avoid any issues with certificate validation.

2. **Confirm**: We set Confirm to $false to suppress the confirmation prompt when creating the datastores.

3. **Connect-VIServer**: We connect to the vCenter server using the IP address or FQDN and the credentials of an account with appropriate permissions.

4. **Import-Csv**: We import a CSV file containing the list of datastore names and NAA IDs.

5. **Foreach-Object**: We loop through each object in the imported CSV file.

6. **New-Datastore**: We create a new datastore for each LUN, specifying the name, path, VMFS version, and other relevant details.

7. **Get-Cluster**: We get the list of all clusters in the environment.

8. **Get-VMhost**: We get the list of all ESXi hosts in the cluster.

9. **Get-VMHostStorage**: We rescan all HBA for each host to ensure that the newly created datastores are visible.

10. **Start-Sleep**: We wait for 15 seconds between each datastore creation operation to avoid overwhelming the vCenter server with too many requests at once.

Script

——

Here is the PowerCLI script that we used to create multiple datastores:

“`powershell

$datanames = Import-Csv ‘C:UsersAdminDesktopFile_with_datastore_name_NAA_Ids.csv’

foreach ($dataname in $datanames) {

$dataname.Datastore_Name

$dataname.Naa_Id

New-Datastore -VMHost ESXi-01.mycloud.lab -Name $dataname.Datastore_Name -Path $dataname.Naa_Id -Vmfs -FileSystemVersion 6

Get-Cluster -name “Cloud-Clu-01” | Get-VMhost | Get-VMHostStorage –RescanAllHBA

Start-Sleep -Seconds 15

}

Disconnect-VIServer -Confirm:$false

“`

Explanation

———–

Let’s go through each line of the script:

1. `$datanames = Import-Csv ‘C:UsersAdminDesktopFile_with_datastore_name_NAA_Ids.csv’` – We import the list of datastore names and NAA IDs from the CSV file.

2. `foreach ($dataname in $datanames)` – We loop through each object in the imported CSV file.

3. `$dataname.Datastore_Name` – We extract the datastore name from each object.

4. `$dataname.Naa_Id` – We extract the NAA ID from each object.

5. `New-Datastore -VMHost ESXi-01.mycloud.lab -Name $dataname.Datastore_Name -Path $dataname.Naa_Id -Vmfs -FileSystemVersion 6` – We create a new datastore for each LUN, specifying the name, path, VMFS version, and other relevant details.

6. `Get-Cluster -name “Cloud-Clu-01” | Get-VMhost | Get-VMHostStorage –RescanAllHBA` – We rescan all HBA for each host to ensure that the newly created datastores are visible.

7. `Start-Sleep -Seconds 15` – We wait for 15 seconds between each datastore creation operation to avoid overwhelming the vCenter server with too many requests at once.

8. `Disconnect-VIServer -Confirm:$false` – We disconnect from the vCenter server to avoid any issues with certificate validation.

Conclusion

———-

In this blog post, we discussed how we created multiple datastores using a PowerCLI script. We imported a list of datastore names and NAA IDs from a CSV file, looped through each object, and created a new datastore for each LUN on all ESXi hosts in the cluster. We also rescanned all HBA for each host to ensure that the newly created datastores were visible. You can use this script as a starting point for your own PowerCLI scripting needs. Happy scripting!

Leave a Reply